Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (200 points)

Hello, I just want to say that I'm totally new to Python and the Natural language ToolKit (NLTK). Right now, I'm working on an application which can perform spell checks. What exactly it does was, it replaces the words which are incorrectly spelled with the correct word, and  Now, In python 2.7 I'm using Enchant library, PyEnchant, and NLTK library. Check the below code, there's a class which handles the replacement/ correction.

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 also initialised a function, which takes in a list of words,after that it executes replace() on every words, atlast it will returns a list of those words with correct spelling.

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']

Now, I don't want this above code. I'm just looking for a code (an accurate one) that will achieve spelling checks as well as replacement on words? So any suggestions, guys?

1 Answer

0 votes
by (26.4k points)

We can use a library to spell check in python. The name of that lib is autocorrect

from autocorrect import Speller
spell = Speller(lang='en')
print spell('caaaar')
print spell('mussage')
print spell('survice')
print spell('hte')
Output :
caesar
message
service
the
Wanna be a Python Expert? Come and join this python course
To know more about this topic, you can also look at the following video tutorial:

Related questions

0 votes
1 answer
asked Feb 24, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...