使用某些 letter/numbers 时 PyEnchant 出错

Error in PyEnchant when certain letter/numbers are used

PyEnchant 在某些 letter/number 组合上似乎有奇怪的行为:

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

并非每个 letter/number 组合都会产生此问题。更多例子是:

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

有没有办法测试 "critical" 消息何时显示并通过不要求拼写建议来消除消息?

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

add(word)

Add a word to the associated personal word list.

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

Pyenchant 是一个基于 ctypes 的 enchant C 库包装器。我的理解是 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 文件(~/.config/enchant/en_US.dic on Linux)并使用:

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

我强烈怀疑您看到的错误消息是由底层 C 库 (enchant) 抛出的,而不是直接由 pyenchant 抛出的,因此我不知道有什么方法可以捕获它们或阻止它们被显示。但是如果你使用 DictWithPWL() 它们永远不会被抛出。