【问题标题】:Peter Norvig's word segmentation issue: how can I segment words with misspellings inside?Peter Norvig 的分词问题:如何分词里面有拼写错误?
【发布时间】:2018-01-09 12:50:58
【问题描述】:

我试图了解,Peter Norvig 的拼写校正器是如何工作的。

在他的 jupyter-notebook 标题here 中,他解释了如何在没有空格分隔单词的情况下分割字符序列。当顺序中的所有单词都写正确时,它就可以正常工作:

>>> segment("deeplearning")
['deep', 'learning']

但是当序列中的单词(或某些单词)拼写错误时,它会出现错误:

>>> segment("deeplerning")
['deep', 'l', 'erning']

不幸的是,我不知道如何解决这个问题并让 segment() 函数可以处理拼写错误的连接词。

有人知道如何处理这个问题吗?

【问题讨论】:

  • 我的意思是...这是一个难题。对此进行了大量研究。
  • 你知道关于这个问题研究的文章吗?

标签: python nlp spell-checking spelling


【解决方案1】:

Peter Norvig 的algorithm 只需稍作改动即可实现。诀窍是在字母表中添加一个空格字符,并将所有由空格字符分隔的二元组视为唯一单词。

由于 big.txt 不包含 deep learning bigram,我们将不得不在字典中添加更多文本。我将使用wikipedia library (pip install wikipedia) 来获取更多文本。

import re
import wikipedia as wiki
import nltk
from nltk.tokenize import word_tokenize
unigrams =  re.findall(r"\w+", open("big.txt").read().lower())
for deeplerning in wiki.search("Deep Learning"):
    try:
        page = wiki.page(deeplerning).content.lower()
        page = page.encode("ascii", errors="ignore")
        unigrams = unigrams + word_tokenize(page)
    except:
        break

我将创建一个包含所有 unigrams 和 bigrams 的新字典:

fo = open("new_dict.txt", "w")
for u in unigrams:
    fo.write(u + "\n")
bigrams = list(nltk.bigrams(unigrams))
for b in bigrams:
    fo.write(" ".join(b)+ "\n")
fo.close()

现在只需在edits1 函数中的letters 变量中添加一个space 字符,将big.txt 更改为new_dict.txt 并更改此函数:

def words(text): return re.findall(r'\w+', text.lower())

到这里:

def words(text): return text.split("\n")

现在correction("deeplerning") 返回'deep learning'

如果您需要特定域的拼写校正器,此技巧将非常有效。如果这个域很大,您可以尝试只将最常见的一元/二元添加到您的字典中。

这个question 也可能有帮助。

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2015-05-06
    • 2010-10-16
    • 2018-11-25
    相关资源
    最近更新 更多