0

PyEnchant 似乎对某些字母/数字组合有奇怪的行为:

>>> import enchant
>>> d=enchant.Dict("en_US")
>>> d.add("def")
>>> d.add("abc")
>>> d.suggest("P92")

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed
['ᾈ\t_us', 'Def', 'Abc']

并非每个字母/数字组合都会产生此问题。更多的例子是:

>>> d.suggest("A92")
['Abc']
>>> d.suggest("92P")

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed

** (python.exe:15036): CRITICAL **: enchant_is_title_case: assertion `word && *word' failed

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed
['', 'DEF', 'ABC']

A92 产生了一些东西,92P 给出了 3 个关键响应。

在 PyEnchant 中,关键错误(它们是错误吗?)会打印到屏幕上,但似乎没有一种机制来捕获它。我尝试try/except阻止失败

有没有办法测试何时显示“关键”消息并通过不要求拼写建议来消除该消息?

4

1 回答 1

1

来自http://pythonhosted.org/pyenchant/api/enchant.html

添加(字)

Add a word to the associated personal word list.

因此我的理解是你需要一个个人单词表(PWL)。

Pyenchant 是 enchant C 库的基于 ctypes 的包装器。我的理解是 ctypes 缓存对象以供重用。因此,从一个新的终端开始,或者如果在 Windows 上需要清除 ctypes 缓存的任何内容(如果有疑问,可能重新启动 Windows?):

然后使用这样的个人单词列表:

import enchant
d = enchant.DictWithPWL("en_US","mywords.txt")
d.add("def")
d.add("abc")
print d.suggest("P92")
print d.suggest("92P")
print d.suggest("Helo")

输出:

['Abc', 'Def']
['ABC', 'DEF']
['He lo', 'He-lo', 'Hole', 'Help', 'Helot', 'Hello', 'Halo', 'Hero', 'Hell', 'Held', 'Helm', 'Heel', 'Loathe', 'Def']

如果您在 mywords.txt 中找到空白行(您没有正确清理 ctypes 缓存),则删除关闭终端的内容或您需要在 Widows 上执行的任何操作,然后重试。

如果您想在内存中使用 PWL,请删除或截断(绝对删除您之前创建的任何空白行)默认 PWL 文件(Linux 上为 ~/.config/enchant/en_US.dic)并使用:

d=enchant.DictWithPWL("en_US", None)

我强烈怀疑您看到的错误消息是由底层 C 库(附魔)而不是 pyenchant 直接抛出的,所以我不知道如何捕获它们或阻止它们被显示。但是,如果您使用 DictWithPWL() ,它们一开始就不会被抛出。

于 2016-11-06T18:41:22.277 回答