如何在Python中有效地使用大型文本语料库的拼写更正

2024-10-01 17:34:18 发布

您现在位置: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)) 

以下是上述代码的输出:

enter image description here

如何停止在jupyter笔记本中显示输出?特别是如果我们有大量的文本文档,它将是大量的输出

我的第二个问题是:在应用于大数据时,如何提高代码的速度和准确性(例如,请检查输出中的“veri”一词)?有没有更好的办法?我感谢您的反应和(替代)更快的解决方案


Tags: textinimportrefordocreturndef
1条回答
网友
1楼 · 发布于 2024-10-01 17:34:18

正如@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']

作为替代方案,您可以使用列表理解来提高速度,也可以使用库^{},在这种情况下,它可以提高单词'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']

相关问题 更多 >

    热门问题