2024-04-24 04:16:59 发布
网友
我想知道,彼得·诺维格的拼写矫正器是怎么工作的。在
在他的jupyter笔记本的标题here中,他解释了如何在不使用空格分隔单词的情况下分割字符序列。当顺序中的所有单词都写正确时,它是正确的:
>>> segment("deeplearning") ['deep', 'learning']
但是当顺序中的单词(或某些单词)拼写错误时,它就不正确了:
不幸的是,我不知道如何解决这个问题,让segment()函数处理拼写错误的连词。在
有人知道如何处理这个问题吗?在
这可以通过彼得·诺维格的algorithm来实现,只需稍作改动。诀窍是在字母表中添加一个空格字符,并将所有由空格字符分隔的双元组作为一个唯一的单词处理。在
因为大.txt不包含deep learningbigram,我们将不得不在字典中再添加一点文本。我将使用wikipedia library(pip install wikipedia)来获取更多文本。在
deep learning
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
我将创建一个新的字典,包含所有的单字和双元组:
现在只需将space字符添加到edits1函数中的letters变量中,将big.txt更改为{},并更改此函数:
space
edits1
letters
big.txt
def words(text): return re.findall(r'\w+', text.lower())
为此:
def words(text): return text.split("\n")
现在correction("deeplerning")返回'deep learning'!在
correction("deeplerning")
'deep learning'
如果您需要特定域的拼写更正程序,此技巧将很好地执行。如果这个域很大,你可以试着把最常见的unigram/bigram添加到字典中。在
这个question也可能有帮助。在
这可以通过彼得·诺维格的algorithm来实现,只需稍作改动。诀窍是在字母表中添加一个空格字符,并将所有由空格字符分隔的双元组作为一个唯一的单词处理。在
因为大.txt不包含
deep learning
bigram,我们将不得不在字典中再添加一点文本。我将使用wikipedia library(pip install wikipedia
)来获取更多文本。在我将创建一个新的字典,包含所有的单字和双元组:
^{pr2}$现在只需将},并更改此函数:
space
字符添加到edits1
函数中的letters
变量中,将big.txt
更改为{为此:
现在
correction("deeplerning")
返回'deep learning'
!在如果您需要特定域的拼写更正程序,此技巧将很好地执行。如果这个域很大,你可以试着把最常见的unigram/bigram添加到字典中。在
这个question也可能有帮助。在
相关问题 更多 >
编程相关推荐