Reputation: 411
I am new to using the PyEnchant Library.
With PyEnchant, I want to write a custom code to :
Ignore certain words like "Internet Slangs" from the Spell Check - May be a Custom Filter can be helpful ???
Replace abbreviations like gonna with "going to"
IF anyone could help me on how can I go about it, would be great help. Thanks !
Upvotes: 2
Views: 716
Reputation: 9430
For 1) use a personal word list http://pythonhosted.org/pyenchant/tutorial.html#personal-word-lists add your words in here for a given language and they will be ignored.
for 2) see my answer to your other question PyEnchant : Replace internet friendly words with a english word but in this case the word is a dictionary word https://en.oxforddictionaries.com/definition/gonna so running the code:
import enchant
# Get the broker.
b = enchant.Broker()
# Set the ordering on the broker so aspell gets used first.
b.set_ordering("en_US","aspell,myspell")
# Print description of broker just to see what's available.
print (b.describe())
# Get an US English dictionary.
d=b.request_dict("en_US")
# Print the provider of the US English dictionary.
print (d.provider)
# A test string.
s = 'gonna'
# We will check the word is not in the dictionary not needed if we know it isn't.
print (d.check(s))
# Print suggestions for the string before we change anything.
print (d.suggest(s))
# Store a relacement for our string as "so".
d.store_replacement(s, 'going to')
# Print our suggestions again and see "so" appears at the front of the list.
print (d.suggest(s))
[<Enchant: Aspell Provider>, <Enchant: Ispell Provider>, <Enchant: Myspell Provider>, <Enchant: Hspell Provider>]
<Enchant: Aspell Provider>
True
['gonna', 'Gina', 'Ginny', 'Joanna', 'Conn', 'Gena', 'gone', 'goon', 'gown', 'Donna', 'Genoa', 'Ghana', 'Janna', 'Jenna', 'going', 'goner', 'gunny', 'gin', 'Nona', 'Gienah', 'Goiania', 'Guiana', 'Guinea', 'Jinnah', 'gonad', 'guinea', 'Joan', 'koan', 'Conan', 'Gino', 'Goa', 'Golan', 'Joann', 'Jonah', 'jinn', 'Glenna', 'goons', 'gowns', 'CNN', 'Gen', 'Jon', 'con', 'gen', 'gun', 'Conner', 'Connie', 'Joanne', 'Johnny', 'cornea', 'gong', 'gonk', 'gunner', 'johnny', 'Anna', 'Bonn', 'Dona', 'Donn', 'Goya', 'Mona', 'dona', 'Gene', 'Gwyn', 'Jana', 'John', 'Joni', 'coin', 'cone', 'cony', 'coon', 'corn', 'gain', 'gene', 'john', 'join', 'conga', 'ganja', 'gonzo', "goon's", "gown's"]
['gonna', 'going to', 'Gina', 'Ginny', 'Joanna', 'Conn', 'Gena', 'gone', 'goon', 'gown', 'Donna', 'Genoa', 'Ghana', 'Janna', 'Jenna', 'going', 'goner', 'gunny', 'gin', 'Nona', 'Gienah', 'Goiania', 'Guiana', 'Guinea', 'Jinnah', 'gonad', 'guinea', 'Joan', 'koan', 'Conan', 'Gino', 'Goa', 'Golan', 'Joann', 'Jonah', 'jinn', 'Glenna', 'goons', 'gowns', 'CNN', 'Gen', 'Jon', 'con', 'gen', 'gun', 'Conner', 'Connie', 'Joanne', 'Johnny', 'cornea', 'gong', 'gonk', 'gunner', 'johnny', 'Anna', 'Bonn', 'Dona', 'Donn', 'Goya', 'Mona', 'dona', 'Gene', 'Gwyn', 'Jana', 'John', 'Joni', 'coin', 'cone', 'cony', 'coon', 'corn', 'gain', 'gene', 'john', 'join', 'conga', 'ganja', 'gonzo', "goon's", "gown's"]
prints True for print (d.check(s)) not False.
This is important in this example, as it is a dictionary word, the word itself appears at the front of the list and our stored replacement is second. So if you want to automatically replace the word you will have to test to see if it is a word then if it is use the second item in the list or something along those lines.
Upvotes: 1