Skip to content

Instantly share code, notes, and snippets.

@mango314
Forked from robertgreiner/spellcheck.py
Last active December 11, 2015 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mango314/4615416 to your computer and use it in GitHub Desktop.
Save mango314/4615416 to your computer and use it in GitHub Desktop.
[FORK] spell checker by Peter Norvig

Fork, of Peter Norvig's spell checker example.

Added a front-end. Deployed at http://mrcactu5.herokuapp.com/spell

Starting ground for even more interesting spell-checking. Very interesting use of conditional probability. A friend asks if it can be used to predict slang.

<!DOCTYPE html>
<html>
<head>
<title>Spell-Checker</title>
<!-- Bootstrap -->
<link href="/static/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="row"><div class="span12"><h1>Spell Checker</h1></div></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/static/bootstrap.min.js"></script>
<div class="row">
<div class="span3">
<form class="navbar-form pull-left" action="/spell" method="POST">
<input type="text" class="span2" name="word">
<button type="submit" class="btn">Submit</button>
</form>
</div>
<div class="span1">
<table>
%if word:
%for wd in word:
<tr><td>{{wd}}</td></tr>
%else:
</table>
</div>
</div>
</body>
</html>
from bottle import route, run, template, static_file, get, post, request
import spellcheck
@route('/spell')
def spell_form():
return template('spell',word=None)
@post('/spell')
def spell_submit():
word = spellcheck.correct(request.forms.get('word'))
return template('spell', word=word)
run(host='localhost', port=8080)
import re, collections
def words(text): return re.findall('[a-z]+', text.lower())
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
NWORDS = train(words(file('big.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits1(word):
s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in s if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)>1]
replaces = [a + c + b[1:] for a, b in s for c in alphabet if b]
inserts = [a + c + b for a, b in s for c in alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
def known(words): return set(w for w in words if w in NWORDS)
def correct(word):
c = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
return max(c, key=NWORDS.get)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment