如何在Python中有效地使用大型文本语料库的拼写更正
考虑以下拼写更正:如何在Python中有效地使用大型文本语料库的拼写更正,python,text-processing,spell-checking,spelling,Python,Text Processing,Spell Checking,Spelling,考虑以下拼写更正: from autocorrect import spell import re WORD = re.compile(r'\w+') def reTokenize(doc): tokens = WORD.findall(doc) return tokens text = ["Hi, welcmoe to speling.","This is jsut an exapmle, but cosnider a veri big cop
from autocorrect import spell
import re
WORD = re.compile(r'\w+')
def reTokenize(doc):
tokens = WORD.findall(doc)
return tokens
text = ["Hi, welcmoe to speling.","This is jsut an exapmle, but cosnider a veri big coprus."]
def spell_correct(text):
sptext = []
for doc in text:
sptext.append(' '.join([spell(w).lower() for w in reTokenize(doc)]))
return sptext
print(spell_correct(text))
以下是上述代码的输出:
如何停止在jupyter笔记本中显示输出?特别是如果我们有大量的文本文档,它将是大量的输出
我的第二个问题是:在应用于大数据时,如何提高代码的速度和准确性(例如,请检查输出中的“veri”一词)?有没有更好的办法?我感谢您的快速响应和(替代)解决方案。正如@khelwood在评论中所说,您应该使用
自动更正。拼写器
:
from autocorrect import Speller
import re
spell=Speller(lang="en")
WORD = re.compile(r'\w+')
def reTokenize(doc):
tokens = WORD.findall(doc)
return tokens
text = ["Hi, welcmoe to speling.","This is jsut an exapmle, but cosnider a veri big coprus."]
def spell_correct(text):
sptext = []
for doc in text:
sptext.append(' '.join([spell(w).lower() for w in reTokenize(doc)]))
return sptext
print(spell_correct(text))
#Output
#['hi welcome to spelling', 'this is just an example but consider a veri big corpus']
作为替代方案,您可以使用列表理解来提高速度,也可以使用库来提高单词
'veri'
的准确性,在这种情况下:
from spellchecker import SpellChecker
import re
WORD = re.compile(r'\w+')
spell = SpellChecker()
def reTokenize(doc):
tokens = WORD.findall(doc)
return tokens
text = ["Hi, welcmoe to speling.","This is jsut an exapmle, but cosnider a veri big coprus."]
def spell_correct(text):
sptext = [' '.join([spell.correction(w).lower() for w in reTokenize(doc)]) for doc in text]
return sptext
print(spell_correct(text))
输出:
['hi welcome to spelling', 'this is just an example but consider a very big corpus']
显然
自动更正。拼写已被弃用。如果您改用autocorrect.Speller
,您可能不会再收到这些消息了。谢谢MrNobody33,您的第一个功能很好,但第二个功能需要索引器模块。Python 3.7;似乎不支持“索引器”。您是否像安装拼写检查器一样安装了它?我用这种方法得到了与“索引器”相同的错误。请尝试使用pip安装pyspellchecker
,这是文档中推荐的方法。谢谢,现在可以使用第二种方法,但速度较慢。可以尝试使用''。join(map(lambda x:spell.correction(x).lower(),reTokenize(doc))
或idk,如果可能是库,可以使用第一个选项,但使用列表