问题描述
print(a)
输出是
myy nameq is xyz i am fromm abc cityy mycty is butful
我想知道上面这句话有没有检查拼写错误并返回拼写错误次数的代码。
我尝试了以下代码
from spellchecker import SpellChecker
spell = SpellChecker()
misspelled = spell.unkNown(a)
for word in misspelled:
print(spell.correction(word))
print(spell.candidates(word))
但我得到的输出如下所示
i
{'cy','uc','ca','y','u','co','cu','ac','o','i','a','ec','ce','ci','e','oc','ic'}
i
{'ul','il','ly','el','al','le','ol','li','lu','lo','la','yl'}
i
{'ex','ox','xy','ix','xu','xe','xi','xa','xo','ax'}
i
{'ab','by','be','ub','bi','bu','bo','ba','ib','eb','ob','e'}
i
{'or','ur','ry','yr','er','ir','ro','ar','ra','ru','ri','re','e'}
i
{'i','e'}
i
{'si','us','sa','sy','so','ys','as','es','os','su','is','se'}
i
{'ny','en','on','in','nu','un','no','na','ne','yn','an','ni','e'}
i
{'ot','ta','at','ti','to','et','te','it','ty','ut','tu','e'}
i
{'fa','ef','fe','fu','of','if','fy','af','uf','fi','fo'}
i
{'my','ym','mo','um','mu','ma','em','am','im','mi','me','om','e'}
i
{'oz','zi','ez','za','ze','az','zo','zu','iz'}
i
{'qo','iq','aq','qe','qa','eq','qu','qi'}
我的预期输出如下例所示
number of spelling mistakes :- 6
我该怎么做请建议
解决方法
所以在做了一些事情之后我终于得到了一个解决方案,我们将使用 textblob 库而不是你使用的拼写检查器库 所以让我们看看代码 -
from textblob import TextBlob
#function to convert string to list
def convert(lst):
return ([i for item in lst for i in item.split()])
#add your string instead yor stng
lst = ['yor stng']
#here we convert string to list using the function
lst = convert(lst)
#initislising the mistakes variable
mistakes = 0
#printing the list to show if text is correct
print(lst)
#here we take each item from list and correct it if it was not equal to original text that means that it has a mistake and if it is equal to old word then it does not have mistake
for x in lst:
a = TextBlob(x)
if (a.correct() != x):
mistakes = mistakes + 1
#printing the number of mistakes
print(mistakes)
我得到的结果 -
['yor','stng']
2