Thursday 10 September 2020

Spell Checker with Python

 We can use two different packages to create spell checker

1. Using textblob

2. Using pyspellchecker

Video Explanation:


 

 

 Source Code:

 Using textblob:

 
from textblob import TextBlob
misspelled_word= "Incorrct"
corrected_word= TextBlob(misspelled_word).correct()
print("Misspelled Word:",misspelled_word)
print("Corrected Word: ",corrected_word)

# you can also use list of misspelled words
print("========================================================")
misspelled_word_list=['Incorrct','Mang','Summay','Watr','Appl']
for word in misspelled_word_list:
    correct_word=TextBlob(word).correct()
    print("Misspelled Word:",misspelled_word_list)
    print("Corrected Word: ",correct_word) 
 

Using pyspellchecker:

 
from spellchecker import SpellChecker
spell=SpellChecker()
misspelled_word=spell.unknown(['Incorrct', 'Summry','Mondy','Spellng','Pyhon'])
print(type(misspelled_word))
for word in misspelled_word:
    print("Corrected word is ", spell.correction(word))
    print("Candidate words:",spell.candidates(word)) 
 
 


You can play around this code and make changes according to your need!

You can also read a text file and use this code to check the spellings in the file


from textblob import TextBlob
from spellchecker import SpellChecker

f=open('demo.txt','r')
data=str(f.read())
print(data)
# you can also use list of misspelled words
print("\n\n**************************Using textblob**************************************")
misspelled_word_list= data.split()
for word in misspelled_word_list:
    correct_word=TextBlob(word).correct()
    print("\nWord: ",word)
    print('------------------------------')
    print("Corrected Word: ",correct_word)
    print("\n================================")

print('\n***************************Using pyspellchecker***********************************')
spell=SpellChecker()
misspelled_word=spell.unknown(misspelled_word_list)
print(type(misspelled_word))
for word in misspelled_word:
    print("\nword: ",word)
    print('--------------------------------------------')
    print("Corrected word is ", spell.correction(word))
    print('--------------------------------------------')
    print("Candidate words:",spell.candidates(word))
    print("\n================================")

f.close()

  


output:

Artifcial intelligence (AI), someties called macine intelligence, is intellience demonstated by machines, unlike the
naural intelligence dislayed by humans and animls.



**************************Using textblob**************************************

Word:  Artifcial
------------------------------
Corrected Word:  Artificial

================================

Word:  intelligence
------------------------------
Corrected Word:  intelligence

================================

Word:  (AI),
------------------------------
Corrected Word:  (of),

================================

Word:  someties
------------------------------
Corrected Word:  sometimes

================================

Word:  called
------------------------------
Corrected Word:  called

================================

Word:  macine
------------------------------
Corrected Word:  machine

================================

Word:  intelligence,
------------------------------
Corrected Word:  intelligence,

================================

Word:  is
------------------------------
Corrected Word:  is

================================

Word:  intellience
------------------------------
Corrected Word:  intelligence

================================

Word:  demonstated
------------------------------
Corrected Word:  demonstrated

================================

Word:  by
------------------------------
Corrected Word:  by

================================

Word:  machines,
------------------------------
Corrected Word:  machines,

================================

Word:  unlike
------------------------------
Corrected Word:  unlike

================================

Word:  the
------------------------------
Corrected Word:  the

================================

Word:  naural
------------------------------
Corrected Word:  natural

================================

Word:  intelligence
------------------------------
Corrected Word:  intelligence

================================

Word:  dislayed
------------------------------
Corrected Word:  displayed

================================

Word:  by
------------------------------
Corrected Word:  by

================================

Word:  humans
------------------------------
Corrected Word:  humans

================================

Word:  and
------------------------------
Corrected Word:  and

================================

Word:  animls.
------------------------------
Corrected Word:  animals.

================================

***************************Using pyspellchecker***********************************
<class 'set'>

word:  (ai),
--------------------------------------------
Corrected word is  (ai),
--------------------------------------------
Candidate words: {'(ai),'}

================================

word:  someties
--------------------------------------------
Corrected word is  sometimes
--------------------------------------------
Candidate words: {'sometimes'}

================================

word:  machines,
--------------------------------------------
Corrected word is  machines
--------------------------------------------
Candidate words: {'machines'}

================================

word:  intelligence,
--------------------------------------------
Corrected word is  intelligence
--------------------------------------------
Candidate words: {'intelligences', 'intelligence'}

================================

word:  demonstated
--------------------------------------------
Corrected word is  demonstrated
--------------------------------------------
Candidate words: {'demonstrated'}

================================

word:  naural
--------------------------------------------
Corrected word is  natural
--------------------------------------------
Candidate words: {'natural', 'aural', 'neural'}

================================

word:  dislayed
--------------------------------------------
Corrected word is  displayed
--------------------------------------------
Candidate words: {'displayed', 'dismayed'}

================================

word:  animls.
--------------------------------------------
Corrected word is  animals
--------------------------------------------
Candidate words: {'animist', 'animus', 'animals', 'animism'}

================================

word:  macine
--------------------------------------------
Corrected word is  machine
--------------------------------------------
Candidate words: {'machine', 'maine', 'racine', 'maxine', 'marine'}

================================

word:  intellience
--------------------------------------------
Corrected word is  intelligence
--------------------------------------------
Candidate words: {'intelligence'}

================================

word:  artifcial
--------------------------------------------
Corrected word is  artificial
--------------------------------------------
Candidate words: {'artifical', 'artificial'}

================================

 


No comments:

Post a Comment