python - 如何在 Python 中有效地对大型文本语料库使用拼写校正
问题描述
考虑以下拼写纠正:
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 notebook 中显示输出?特别是如果我们有大量的文本文档,就会有很多输出。
我的第二个问题是:在大数据上应用时,如何提高代码的速度和准确性(例如,请检查输出中的“veri”一词)?有没有更好的方法来做到这一点?感谢您以更快的速度响应和(替代)解决方案。
解决方案
正如@khelwood 在评论中所说,您应该使用autocorrect.Speller
:
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']
作为替代方案,您可以使用列表推导来提高速度,也可以使用 library ,在这种情况下pyspellchecker
提高单词的准确性:'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']
推荐阅读
- .net - 如何从命令行界面使用 Microsoft.CodeAnalysis.NetAnalyzers 为 .NET 应用程序运行代码分析
- postgresql - 如何遍历表动态生成case语句
- javascript - 为什么 forEach 不会遍历整个数组?
- ruby-on-rails - 如何将记录关系与同一模型的另一个模型关系结合起来?
- broadcast - 我正在尝试通过 OBS 在 YouTube 上开始直播,但我无法创建直播并开始直播
- sql - SSIS 使用派生列动态添加源中不存在的列
- android - 使用 React Native Animated 在 Android 上消失的 React Native 动画图像
- html - 显示:flex 采用图像高度但在添加文本时溢出
- reactjs - npm install 给我们一个依赖树错误
- kotlin - 过滤器kotlin内的条件最大值