【问题标题】:PyEnchant : Replace internet friendly words with a english wordPyEnchant : 用英文单词替换互联网友好的单词
【发布时间】:2017-11-21 19:40:54
【问题描述】:

我想在拼写检查中识别“sooooooooooooooooo”之类的单词并用“so”替换它们。我怎样才能做到这一点?我要写什么(意思是过滤器等),我在哪里调整代码?

谢谢!

【问题讨论】:

  • 理想情况下我会写一个相同的正则表达式,但是我在哪里调整代码?

标签: filter pyenchant pel


【解决方案1】:

您可以使用 store_replacement,但我的理解是 store_replacement 需要由底层提供者实现。如果您使用实现它的提供程序 Aspell,您可以看到它的工作方式如下:(请注意,您需要安装 Aspell 和它的字典才能看到它的工作)

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") 作为第一个建议。它可能会排在第二个。在这种情况下,有没有一种方法可以增加我的 Set 建议的 WEIGHTAGE,这样它总是出现在建议的首位?
  • 根据我的经验,如果单词在字典中,建议的替换将排在第二位,否则它将排在第一位。我刚刚用 store_replacement("soooooo", "so") 进行了测试,它首先出现。你能给我一个例子,说明它什么时候排在第二位并且不在字典中,即 print (d.check(s)) 是 False 并且建议的替换排在第二位?另一个测试是当被测试的词不是第一个建议并且建议的替换出现在第二个时?
猜你喜欢
  • 1970-01-01
  • 2014-02-10
  • 2020-03-27
  • 2015-03-02
  • 1970-01-01
  • 2021-12-29
  • 2020-01-06
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多