简体   繁体   中英

How to check spelling mistakes in sentence using python

I want to check the number of spelling mistake. in the sentence

print(a)

output is

myy nameq is xyz i am fromm abc cityy mycty is butful

I want to know is there a code that check the spelling error and return the number of spelling error in the above sentence.

I tried the following code

from spellchecker import SpellChecker
spell = SpellChecker()
misspelled = spell.unknown(a)
for word in misspelled:
    print(spell.correction(word))
    print(spell.candidates(word))

but the output i am getting is as shown below

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', 'i', 'y', 'u', 'ol', 'li', 'o', 'lu', 'a', 'lo', 'la', 'e', 'yl'}
i
{'ex', 'ox', 'xy', 'ix', 'y', 'u', 'xu', 'i', 'o', 'xe', 'xi', 'a', 'xa', 'xo', 'e', 'ax'}
i
{'ab', 'by', 'be', 'ub', 'bi', 'i', 'bu', 'y', 'u', 'bo', 'ba', 'o', 'ib', 'eb', 'a', 'ob', 'e'}
i
{'or', 'ur', 'ry', 'yr', 'i', 'er', 'y', 'u', 'ir', 'ro', 'ar', 'o', 'ra', 'ru', 'a', 'ri', 're', 'e'}
i
{'i', 'u', 'y', 'o', 'a', 'e'}
i
{'si', 'us', 'sa', 'sy', 'so', 'ys', 'as', 'es', 'y', 'os', 'u', 'su', 'i', 'o', 'is', 'a', 'e', 'se'}
i
{'ny', 'en', 'on', 'in', 'nu', 'un', 'no', 'na', 'i', 'y', 'ne', 'yn', 'u', 'o', 'an', 'a', 'ni', 'e'}
i
{'ot', 'ta', 'at', 'ti', 'to', 'et', 'y', 'u', 'te', 'it', 'i', 'o', 'a', 'ty', 'ut', 'tu', 'e'}
i
{'fa', 'ef', 'i', 'u', 'y', 'fe', 'o', 'fu', 'of', 'if', 'fy', 'a', 'af', 'uf', 'fi', 'e', 'fo'}
i
{'my', 'ym', 'mo', 'um', 'mu', 'i', 'y', 'u', 'ma', 'em', 'am', 'o', 'im', 'mi', 'me', 'a', 'om', 'e'}
i
{'oz', 'e', 'zi', 'ez', 'za', 'i', 'y', 'u', 'o', 'ze', 'az', 'a', 'zo', 'zu', 'iz'}
i
{'qo', 'iq', 'i', 'y', 'u', 'o', 'aq', 'qe', 'a', 'qa', 'eq', 'qu', 'e', 'qi'}

my expected output is shown in below example

 number of spelling mistakes :- 6

How can I do this Kindly suggest

so after doing some things i finally got a solution first we will use textblob library instead spellchecker library which you used so lets see the code -

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)

the result i get -

['yor', 'stng']
2

Note : If the sentence contains names of people or places it will add a mistake in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM