2017-06-19 83 views
0

我想识别“sooooooooooooooo”这样的词,并在拼写检查中用“so”替换它们。我怎样才能做到这一点?我写什么(意思是一个过滤器等),以及我在哪里调整相同的代码?PyEnchant:用英语单词替换互联网友好的词

谢谢!

+0

理想我会写一个正则表达式来回回相同,但我在哪里调整代码? –

回答

0

您可以使用store_replacement,但我的理解是store_replacement需要由底层提供者实现。如果您使用的提供安博泰它实现它,你可以看到它像这样的工作:(请注意,您需要安装安博泰和它的字典,看看这个工作)

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

是的。 Apell实现了store_replacement()...但是挑战在大多数时候,Aspell不会建议我的设置词(例如:使用store_replacement(“soooooo”,“so”)作为第一条建议。在这种情况下,有没有一种方法可以增加我的设置建议的WEIGHTAGE,这样它总是会出现在建议中的第一个? –

+0

以我的经验,如果单词在字典中,则建议的替换将排在第二,否则它会先到达。我刚刚测试过store_replacement(“soooooo”,“so”),它是第一个。你能给我一个例子,说明什么时候它是第二个,而不是在字典中,即print(d.check(s))是False并且建议的替换是次要的?另一个测试是何时被测试的字不是第一个建议,并且建议的替换是第二个? –