Spell Checker for Python

0 votes

I am creating an application that can perform spell checks (replaces an incorrectly spelled word with the correct one). I am using the Enchant library on Python 2.7, PyEnchant, and the NLTK library for this task.
The below code is a class that handles the correction/replacement.

from nltk.metrics import edit_distance

class SpellingReplacer:
    def __init__(self, dict_name='en_GB', max_dist=2):
        self.spell_dict = enchant.Dict(dict_name)
        self.max_dist = 2

    def replace(self, word):
        if self.spell_dict.check(word):
            return word
        suggestions = self.spell_dict.suggest(word)

        if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist:
            return suggestions[0]
        else:
            return word

I wrote this function that takes in a list of words and executes replace() on each word and then returns a list of those words.

def spell_check(word_list):
    checked_list = []
    for item in word_list:
        replacer = SpellingReplacer()
        r = replacer.replace(item)
        checked_list.append(r)
    return checked_list

>>> word_list = ['car', 'colour']
>>> spell_check(words)
['car', 'color']

Is there any better way to do this? I want something like, what google does.

Apr 25, 2022 in Python by Kichu
• 19,040 points
885 views

1 answer to this question.

0 votes

This function, in particular, has the ideas that you now need to make your spell checker more sophisticated: splitting, deleting, transposing, and inserting the irregular words to 'correct' them.

def edits1(word):

   splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]

   deletes = [a + b[1:] for a, b in splits if b]

   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]

   replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]

   inserts = [a + c + b for a, b in splits for c in alphabet]

   return set(deletes + transposes + replaces + inserts)

Follow this post by Peter Norvig for a complete guide on the spell checker.

answered Apr 28, 2022 by narikkadan
• 63,600 points

Related Questions In Python

0 votes
1 answer

platform support for pytype python checker

Platform support: Pytype is currently developed and tested ...READ MORE

answered Jun 7, 2019 in Python by Kiranmai
632 views
0 votes
1 answer

Unique identification for data items in Python

Try the UUID module of Python. For example, ...READ MORE

answered Apr 17, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,152 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
3,934 views
0 votes
1 answer

Slice notation in Python for string reversal

The slice notation is [start:end:step]. Step = ...READ MORE

answered Apr 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
681 views
0 votes
1 answer

Section postgresql not found in the database.ini file

Python doesn't know what $FILEDIR is. Try an absolute path ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,020 points
3,686 views
0 votes
1 answer

Iterating over dictionaries using 'for' loops

key is just a variable name. for key ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,300 points
991 views
0 votes
1 answer

Conflicting dependencies of pypyodbc and blpapi

I figured out that pypyodbc only works ...READ MORE

answered Oct 9, 2018 in Python by Priyaj
• 58,020 points
751 views
0 votes
1 answer

Programmatically generate video or animated GIF in Python?

You can use  ImageMagick. Save your frames ...READ MORE

answered Apr 25, 2022 in Python by narikkadan
• 63,600 points
977 views
0 votes
1 answer

ln (Natural Log) in Python

Use math.log it is a natural logarithm. For ...READ MORE

answered Apr 25, 2022 in Python by narikkadan
• 63,600 points
1,074 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP