2

我是使用 PyEnchant 库的新手。

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

  1. 从拼写检查中忽略某些单词,例如“Internet 俚语” - 自定义过滤器可能会有所帮助 ???

  2. 用“going to”代替“going to”等缩写词

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

4

1 回答 1

1

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

对于 2)请参阅我对您的其他问题PyEnchant 的回答:用英语单词替换互联网友好单词,但在这种情况下,该单词是字典单词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"]

print (d.check(s)) 不是 False 时打印 True。

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

于 2017-06-26T21:39:06.873 回答