擅长:python、mysql、java
<p>有一篇很棒的文章有一个完整的源代码(21行)由彼得诺维格提供的拼写更正。</p>
<p><a href="http://norvig.com/spell-correct.html" rel="noreferrer">http://norvig.com/spell-correct.html</a></p>
<p>我们的想法是建立所有可能的文字编辑</p>
<pre><code>hello - helo - deletes
hello - helol - transpose
hello - hallo - replaces
hello - heallo - inserts
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)
</code></pre>
<p>现在,在列表中查找这些编辑。</p>
<p>彼得的文章读得很好,值得一读。</p>