我想識別「sooooooooooooooo」這樣的詞,並在拼寫檢查中用「so」替換它們。我怎樣才能做到這一點?我寫什麼(意思是一個過濾器等),以及我在哪裏調整相同的代碼?PyEnchant:用英語單詞替換互聯網友好的詞
謝謝!
我想識別「sooooooooooooooo」這樣的詞,並在拼寫檢查中用「so」替換它們。我怎樣才能做到這一點?我寫什麼(意思是一個過濾器等),以及我在哪裏調整相同的代碼?PyEnchant:用英語單詞替換互聯網友好的詞
謝謝!
您可以使用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']
是的。 Apell實現了store_replacement()...但是挑戰在大多數時候,Aspell不會建議我的設置詞(例如:使用store_replacement(「soooooo」,「so」)作爲第一條建議。在這種情況下,有沒有一種方法可以增加我的設置建議的WEIGHTAGE,這樣它總是會出現在建議中的第一個? –
以我的經驗,如果單詞在字典中,則建議的替換將排在第二,否則它會先到達。我剛剛測試過store_replacement(「soooooo」,「so」),它是第一個。你能給我一個例子,說明什麼時候它是第二個,而不是在字典中,即print(d.check(s))是False並且建議的替換是次要的?另一個測試是何時被測試的字不是第一個建議,並且建議的替換是第二個? –
理想我會寫一個正則表達式來回回相同,但我在哪裏調整代碼? –