在enchant中是否可以传递多个字典?

9
有没有办法在 enchant 中使用多个字典呢? 这是我所做的,
import enchant
d = enchant.Dict("en_US")
d.check("materialise")
>> False

但如果我使用enchant.Dict("en_UK"),我会得到True。最好的方法是如何组合多个词典,使其无论输入参数是materialise还是materialize都会返回True

3个回答

3

我可能晚了,但这个问题也引起了我的兴趣。

因此,在Python的enchant中使用多种英语方言的解决方案如下:

    import enchant
    '''
    Use "en" simply to cover all available dialects and word usages of the English language
    '''
    d = enchant.Dict("en")
    d.check("materialise")  # UK (en_GB)
    >>> True
    
    d.check("materialize")  # USA (en_US)
    >>> True

希望这对我们未来的读者有所帮助 :)

2

@Mass17,实际上那不是正确的。表达式"en_US" and "en_UK"是对两个字符串进行逻辑AND运算的结果为"en_UK"。以下是上述表达式中AND运算符的工作原理:(1)首先,任何非空字符串都被认为是True,(2)如果左侧字符串为True,则检查并返回右侧字符串。阅读有关Python短路求值的文章可以了解其工作方式。

所以:

>>> "en_US" and "en_UK"
'en_UK'

请注意,如果您交换字符串的顺序:
>>> "en_UK" and "en_US"
'en_US'

你的"en_UK"字典中均包含"materialise"和"materialize"这两个单词,因此你得到了这样的结果。但你尚未真正"合并"这两个词典。

1
为了完整回答,您可能希望展示如何将其与您的答案结合。 (审核结束)。 - ZF007

1
对于Hunspell词典,如果两个词典共享相同的.aff文件,则有一个解决方法,我想en_US和en_GB满足该条件。
作者是Sergey Kurakin,Bash脚本如下(dic_combine.sh):
#!/bin/bash

# Combines two or more hunspell dictionaries.
# (C) 2010 Sergey Kurakin <kurakin_at_altlinux_dot_org>

# Attention! All source dictionaries MUST share the same affix file.

# Usage: dic_combine source1.dic source2.dic [source3.dic...] > combined.dic

TEMPFILE=`mktemp`

cat $@ | sort --unique | sed -r 's|^[0123456789]*$||;/^$/d' > $TEMPFILE

cat $TEMPFILE | wc -l
cat $TEMPFILE
rm -f $TEMPFILE
rm -f $TEMPFILE 

因此,您需要将这些字典文件放在一个目录中并运行:

$ dic_combine en_US.dic en_GB.dic > en.dic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接