gpt4 book ai didi

python - 使用 PyEnchant 创建自定义过滤器

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:19 26 4
gpt4 key购买 nike

我刚开始使用 PyEnchant 库。

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

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

  2. 将“going to”等缩写替换为“going to”

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

最佳答案

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

对于 print (d.check(s)) 打印 True,而不是 False。

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

关于python - 使用 PyEnchant 创建自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com