问题描述
我想识别sooooooooooooooo这样的单词,并在拼写检查中用so替换它们。我怎样才能做到这一点?我写什么(意思是一个过滤器等)和我在哪里调整相同的代码?
谢谢!
您可以使用store_replacement, store_replacement需要由底层提供者来实现。如果你使用实现它的提供者Aspell,你可以看到它的工作方式如下:(注意,你需要安装Aspell和它的字典才能看到这个工作)
pre > 导入附魔
#获取代理。
b = enchant.Broker()
#设置代理的顺序,使得首先使用aspell。
b.set_ordering(en_US,aspell,myspell)
#打印broker的描述,以查看可用的内容。
print(b.describe())
#获取美国英语词典。
d = b.request_dict(en_US)
#打印美国英语词典的提供者。
print(d.provider)
#一个测试字符串。
s ='sooooooooooooooo'
#如果我们知道它不是不需要的字典,我们将检查不在字典中。
print(d.check(s))
#在我们改变任何东西之前,打印字符串的建议。
print(d.suggest(s))
#将字符串的关系存储为so。
d.store_replacement(s,'so')
#再次打印我们的建议,并在列表的前面看到so。 (Enchant:Aspell Provider>,< Enchant:Ispell Provider>,< Enchant:Myspell Provider>,< Enchant: Hspell Provider>]
< Enchant:Aspell Provider>
假
['SO','so','spoor','sou','sow','soy','zoo','Soho','Soto','solo', '快','soot','shoo','翱翔','酸','shoos','sooth','sooty','si','sootier','sough','SOP','sop 's'''poo's''''''''''''''''''''''''''''''''''''''''''''''''''''肥皂,土壤,汤,SA,SE,SS,SW,Se,安抚奶嘴,SOB,SOS,SO,SRO ','索','索','儿','sob','soc','sod','sol','son','sot','boo','coo','foo', 'goo','loo','moo','ooh','too','woo','CEO','S's','SSA','SSE','SSS','SSW','Sue '索','索萨','嘘','说','海','看','缝','起诉','异或' 'bozo','coo ,'ooze','ooze','oozy','orzo','ouzo','sago','scow','sloe','slow','snow' 'so','so','spoor','sou','sow','soy','zoo','Soho','Soto','solo','soo' '快','soot','shoo','翱翔','酸','shoos','sooth','sooty','si','sootier','sough','SOP','sop 's'''poo's''''''''''''''''''''''''''''''''''''''''''''''''''''肥皂,土壤,汤,SA,SE,SS,SW,Se,安抚奶嘴,SOB,SOS,SO,SRO ','索','索','儿','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']
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 !
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']
这篇关于PyEnchant:用英文单词替换互联网友好的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!