How to Write a Spelling Corrector
norvig.com
norvig.com
http://news.ycombinator.com/item?id=4609321
Not complaining though; this article is definitely worth a read for those that missed it the first few times around.
Kernighan, M., Church, K., Gale, W (1990) “A Spelling Correction Program Based on a Noisy Channel Model,” Coling, Helsinki , Finland. http://acl.ldc.upenn.edu/C/C90/C90-2036.pdf
> set(e2 for e1 in edits1(word) for e2 in edits1(e1))
I understand how you can say something like "f(x) for x in edits1(word)", but the way it's used above with multiple fors is making my brain hurt.
s = set()
for e1 in edits1(word):
for e2 in edits1(e1):
s.add(e2) >>> [a for a in range(3) for b in range(2)]
[0, 0, 1, 1, 2, 2]
It's just that for every iteration of the inner loop, it is evaluated and added to the result.Once you keep in mind that their order has the same meaning as with normal for loops it's not so tricky. You can even indent them that way to make that more obvious.
[Edit: is was 503ing but it works for me now. Also, (2007)]
1. http://norvig.com.nyud.net/spell-correct.html?
2. http://web.archive.org/web/20110717135116/http://norvig.com/...
3. http://webcache.googleusercontent.com/search?q=cache:q9g8wF3...
Though languages with constraint systems built in have even nicer solutions (e.g. Oz)
This was solved by using an algorithm similar to the described in the OP, like a spelling corrector: for each content on the CMS (e.g., article), get the search terms and, using the entire database of search terms as a dictionary (adjusted for frequency), spill out the likely candidates as normalized "tags". That is, for a blog post where the search terms was a string like "batman, batman the dark knight rises, batman dc comics", it would be normalized to an array like ['batman', 'the dark knight rises', 'd.c. comics'].
I also used the same approach on a previous project, to normalize addresses in a real estate database (Levensthein distance). Yes, it's that useful :)