我是使用PyEnchant库的新手。

使用PyEnchant,我想将自定义代码编写为:


忽略拼写检查中的某些词,例如“ Internet lang语”-可能是“自定义过滤器”会有所帮助?
用“ going to”代替诸如gonna的缩写


如果有人可以帮我解决这个问题,那将是很大的帮助。谢谢 !

最佳答案

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

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"]


打印为真(d.check)不为False。

在此示例中,这很重要,因为它是一个字典单词,该单词本身出现在列表的最前面,而我们存储的替换单词是第二个。因此,如果要自动替换单词,则必须进行测试以查看它是否是单词,然后使用列表中的第二项或类似的内容。

10-08 02:18