Live data from Hacker News

How to Write a Spelling Corrector

norvig.com

31–40 of 133 posts

Re: How to Write a Spelling Corrector

#31
post #15

The date on the top says February 2007 to August 2016. Does anyone know which parts are new in August 2016? I've read this before and it isn't sticking out to me.

Here's the history, sorry there is no easy way to see diffs.

https://web.archive.org/web/*/http://norvig.com/spell-correc...

Re: How to Write a Spelling Corrector

#32
post #15

The date on the top says February 2007 to August 2016. Does anyone know which parts are new in August 2016? I've read this before and it isn't sticking out to me.

The code has been updated stylistically, and I'm not sure what else has changed. Here's the old (Python 2.5) version from http://web.archive.org/web/20160110135619/http://norvig.com/...:

    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):
       splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
       deletes    = [a + b[1:] for a, b in splits if b]
       transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
       replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
       inserts    = [a + c + b     for a, b in splits 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):
        candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
        return max(candidates, key=NWORDS.get)

Re: How to Write a Spelling Corrector

#35
post #3

Spell chequer Martha Snow Eye halve a spelling chequer It came with my pea sea It plainly marques four my revue Miss steaks eye kin knot sea. Eye strike a quay and type a word And weight four it two say Weather eye am wrong oar write It shows me strait a weigh. As soon as a mist ache is maid It nose bee fore two long And eye can put the error rite It's rare lea ever wrong. Eye have run this poem threw it I am shore y…

You sound like a character from a Dickens novel.

Re: How to Write a Spelling Corrector

#37
post #18
post #3

Spell chequer Martha Snow Eye halve a spelling chequer It came with my pea sea It plainly marques four my revue Miss steaks eye kin knot sea. Eye strike a quay and type a word And weight four it two say Weather eye am wrong oar write It shows me strait a weigh. As soon as a mist ache is maid It nose bee fore two long And eye can put the error rite It's rare lea ever wrong. Eye have run this poem threw it I am shore y…

In a way, I'm surprised I can read this so easily. Do our brains read by converting text to sounds, and then parsing the sounds?

I was pondering the same thing; also, I wonder whether not being a native English speaker makes it easier or harder to read it - English is my third language and I found I could read it without any problems.

I think I've read somewhere that reading is basically done by the brain registering the first and last few letters in a word, then just checking whether the ones in the middle are more or less what you expect and where you'd expect them. (In other words - if the start and/or end of a word is altered, your reading speed and comprehension should take a nosedive compared to just messing with the letters in the middle)

If there is some truth to that, it would go a long way towards explaining why we can read it with as little trouble as we do.

Re: How to Write a Spelling Corrector

#38
post #28

They don't seem to address keyboard layouts.

this is a good point I think.

sometimes, people type wrong words, because certain keys are too close on the keyboard.

another problem I found about the naive spelling corrector is, it doesn't take the pronunciation into account. certain wrong spelling looks different from the correct version by edit distance. but they sound similar.

Re: How to Write a Spelling Corrector

#39
post #34

His python code styling is really awesome. So concise. Probably inspired by all the LISP he wrote in the past. Although he does seem to be using doc strings incorrectly

What is wrong with his docstrings?

Nothing's wrong with them. Though it is more usual to use triple quotes for docstrings even if they fit on one line (see PEP 257, "Triple quotes are used even though the string fits on one line. This makes it easy to later expand it.").
Post reply on HN