0

I want to identify words like "sooooooooooooooo" and replace them with "so" in Spell Check. How can I achieve this ? What do I write (meaning a Filter, etc.) and Where do I tweak the code for the same ?

Thanks !

Gauri Padbidri
  • 371
  • 4
  • 15

1 Answers1

0

You could use store_replacement, however my understanding is that store_replacement needs to be implemented by the underlying provider. If you use the provider Aspell which implements it you can see it working like so: (Note you will need to install Aspell and it's dictionaries to see this working)

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 = 'sooooooooooooooo'
# 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, 'so')
# 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>
False
['SO', 'so', 'spoor', 'sou', 'sow', 'soy', 'zoo', 'Soho', 'Soto', 'solo', 'soon', 'soot', 'shoo', 'soar', 'sour', 'shoos', 'sooth', 'sooty', 'Si', 'sootier', 'sough', 'SOP', 'sop', 'S', 'poo', 's', 'sooner', 'soothe', 'sorrow', 'Sir', 'Sui', 'sci', 'sir', 'poos', 'silo', 'soap', 'soil', 'soup', 'SA', 'SE', 'SS', 'SW', 'Se', 'soother', 'SOB', 'SOS', 'SOs', 'SRO', 'Soc', 'Sol', 'Son', 'sob', 'soc', 'sod', 'sol', 'son', 'sot', 'boo', 'coo', 'foo', 'goo', 'loo', 'moo', 'ooh', 'too', 'woo', 'CEO', "S's", 'SSA', 'SSE', 'SSS', 'SSW', 'Sue', 'Zoe', 'saw', 'say', 'sea', 'see', 'sew', 'sue', 'xor', 'Snow', 'Sony', 'Sosa', 'boos', 'bozo', 'coos', 'loos', 'moos', 'oohs', 'ooze', 'oozy', 'orzo', 'ouzo', 'sago', 'scow', 'sloe', 'slow', 'snow', 'soak']
['so', 'SO', 'spoor', 'sou', 'sow', 'soy', 'zoo', 'Soho', 'Soto', 'solo', 'soon', 'soot', 'shoo', 'soar', 'sour', 'shoos', 'sooth', 'sooty', 'Si', 'sootier', 'sough', 'SOP', 'sop', 'S', 'poo', 's', 'sooner', 'soothe', 'sorrow', 'Sir', 'Sui', 'sci', 'sir', 'poos', 'silo', 'soap', 'soil', 'soup', 'SA', 'SE', 'SS', 'SW', 'Se', 'soother', 'SOB', 'SOS', 'SOs', 'SRO', 'Soc', 'Sol', 'Son', 'sob', 'soc', 'sod', 'sol', 'son', 'sot', 'boo', 'coo', 'foo', 'goo', 'loo', 'moo', 'ooh', 'too', 'woo', 'CEO', "S's", 'SSA', 'SSE', 'SSS', 'SSW', 'Sue', 'Zoe', 'saw', 'say', 'sea', 'see', 'sew', 'sue', 'xor', 'Snow', 'Sony', 'Sosa']
Dan-Dev
  • 8,957
  • 3
  • 38
  • 55
  • True. Apell implements the store_replacement()...But the challenge is most of the times, Aspell will NOT suggest my set word (Eg : using store_replacement("soooooo", "so") as the FIRST Suggestion. It may come as second. In that case, is there a way I can increase the WEIGHTAGE of my Set suggestion, that way it always appears FIRST in the suggestions ? – Gauri Padbidri Jun 28 '17 at 06:44
  • In my experience if the word is in the dictionary the suggested replacement will come second otherwise it will come first. I have just tested with store_replacement("soooooo", "so") and it came first. Can you give me an example of when it comes second and is not in the dictionary i.e. print (d.check(s)) is False and the suggested replacement comes second? Another test is when the word being tested is not the first suggestion and the suggested replacement comes second? – Dan-Dev Jun 28 '17 at 20:25