使用PyEnchant创建自定义过滤器

时间:2017-06-19 09:34:28

标签: python dictionary filter pyenchant

我是使用PyEnchant库的新手。

使用PyEnchant,我想写一个自定义代码:

  1. 忽略某些词语,例如" Internet Slangs"从拼写检查 - 可能是自定义过滤器可以有用???

  2. 替换缩写,例如"转到"

  3. 如果有人能帮我解决这个问题,那将会很有帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

对于1)使用个人单词列表http://pythonhosted.org/pyenchant/tutorial.html#personal-word-lists在这里为给定语言添加您的单词,它们将被忽略。

for 2)查看我对您的其他问题PyEnchant : Replace internet friendly words with a english word的回答,但在这种情况下,该字词是字典词https://en.oxforddictionaries.com/definition/gonna,因此运行代码:

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 = 'gonna'
# 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, 'going to')
# 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>
True
['gonna', 'Gina', 'Ginny', 'Joanna', 'Conn', 'Gena', 'gone', 'goon', 'gown', 'Donna', 'Genoa', 'Ghana', 'Janna', 'Jenna', 'going', 'goner', 'gunny', 'gin', 'Nona', 'Gienah', 'Goiania', 'Guiana', 'Guinea', 'Jinnah', 'gonad', 'guinea', 'Joan', 'koan', 'Conan', 'Gino', 'Goa', 'Golan', 'Joann', 'Jonah', 'jinn', 'Glenna', 'goons', 'gowns', 'CNN', 'Gen', 'Jon', 'con', 'gen', 'gun', 'Conner', 'Connie', 'Joanne', 'Johnny', 'cornea', 'gong', 'gonk', 'gunner', 'johnny', 'Anna', 'Bonn', 'Dona', 'Donn', 'Goya', 'Mona', 'dona', 'Gene', 'Gwyn', 'Jana', 'John', 'Joni', 'coin', 'cone', 'cony', 'coon', 'corn', 'gain', 'gene', 'john', 'join', 'conga', 'ganja', 'gonzo', "goon's", "gown's"]
['gonna', 'going to', 'Gina', 'Ginny', 'Joanna', 'Conn', 'Gena', 'gone', 'goon', 'gown', 'Donna', 'Genoa', 'Ghana', 'Janna', 'Jenna', 'going', 'goner', 'gunny', 'gin', 'Nona', 'Gienah', 'Goiania', 'Guiana', 'Guinea', 'Jinnah', 'gonad', 'guinea', 'Joan', 'koan', 'Conan', 'Gino', 'Goa', 'Golan', 'Joann', 'Jonah', 'jinn', 'Glenna', 'goons', 'gowns', 'CNN', 'Gen', 'Jon', 'con', 'gen', 'gun', 'Conner', 'Connie', 'Joanne', 'Johnny', 'cornea', 'gong', 'gonk', 'gunner', 'johnny', 'Anna', 'Bonn', 'Dona', 'Donn', 'Goya', 'Mona', 'dona', 'Gene', 'Gwyn', 'Jana', 'John', 'Joni', 'coin', 'cone', 'cony', 'coon', 'corn', 'gain', 'gene', 'john', 'join', 'conga', 'ganja', 'gonzo', "goon's", "gown's"]

打印True表示打印(d.check(s))而不是False。

这在这个例子中很重要,因为它是一个字典单词,单词本身出现在列表的前面,而我们存储的替换是第二个。因此,如果你想自动替换单词,你必须测试它是否是一个单词然后是否使用列表中的第二项或沿着这些行。

相关问题