Reputation: 53
So basically I would like to use a spell checker for different places, countries, types of foods in my program google search engine does a pretty good job in identifying mistakes in the sentences. Am trying to get the suggestion as a text i have no earthly idea how to do this but here is what I did and it doesn't work.
from googlesearch import search
ip="What would you like to search for? "
for url in search(ip, stop=1):
print(url)
This gives me the url I am not looking for the url am looking a list with suggestion what it could be.
Upvotes: 0
Views: 394
Reputation: 936
To use a spell checker for different places, countries, and types of foods in your python program, you can use the pyenchant library. This library provides a wrapper for the Enchant spell checking library, which supports multiple languages and dictionaries.
The pyenchant library allows you to create a SpellChecker object that can check the spelling of words and suggest corrections. You can also add or remove words from the dictionary, or use a custom dictionary file.
To use the pyenchant library, you need to install it using the command:
pip install pyenchant
Then, you can import it in your python program and create a SpellChecker object with the desired language code. For example, to create a spell checker for English, you can use:
import enchant
speller = enchant.SpellChecker("en_US")
To check the spelling of a word, you can use the check method, which returns True if the word is correct, or False if it is not. For example:
speller.check("pizza") # True
speller.check("piza") # False
To get a list of suggested corrections for a misspelled word, you can use the suggest method, which returns a list of strings. For example:
speller.suggest("piza") # ['pizza', 'pita', 'pia', 'pica', 'pisa']
To add a word to the dictionary, you can use the add method, which takes a string as an argument. For example:
speller.add("sushi") # adds sushi to the dictionary
speller.check("sushi") # True
To remove a word from the dictionary, you can use the remove method, which also takes a string as an argument. For example:
speller.remove("sushi") # removes sushi from the dictionary
speller.check("sushi") # False
To use a custom dictionary file, you can use the enchant.DictWithPWL class, which takes a language code and a file name as arguments. The file should contain one word per line. For example, if you have a file called mydict.txt with the following content:
Paris
London
Tokyo
New York
pizza
sushi
pasta
You can create a custom spell checker using:
import enchant
speller = enchant.DictWithPWL("en_US", "mydict.txt")
This spell checker will use the words from the file as well as the default English dictionary.
Here are some examples of using the pyenchant library to check the spelling of different places, countries, and types of foods in your python program.
import enchant
speller = enchant.SpellChecker("en_US")
sentence = "I love to travel to diferent places and try new cuisines."
words = sentence.split()
for word in words:
if not speller.check(word):
print(f"{word} is misspelled. Suggestions: {speller.suggest(word)}")
Output:
diferent is misspelled. Suggestions: ['different', 'deferent', 'deterrent', 'decent', 'deficient']
cuisines is misspelled. Suggestions: ['cuisine', 'cuisines', 'cousins', 'quinces', 'quinsies']
import enchant
speller = enchant.SpellChecker("en_US")
speller.add("Mumbai")
speller.add("kimchi")
sentence = "I visited Mumbai last year and enjoyed the kimchi there."
words = sentence.split()
for word in words:
if not speller.check(word):
print(f"{word} is misspelled. Suggestions: {speller.suggest(word)}")
Output:
No output, since all words are correct.
import enchant
speller = enchant.DictWithPWL("en_US", "mydict.txt")
sentence = "My favorite cities are Paris, London, and Tokyo."
words = sentence.split()
for word in words:
if not speller.check(word):
print(f"{word} is misspelled. Suggestions: {speller.suggest(word)}")
Output:
No output, since all words are in the custom dictionary file.
Upvotes: 1