返回一个空列表,而不是bigrams

2024-05-18 14:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

下面提到的代码返回预期的输出

[('the', 23135851162), ('of', 13151942776), ('and', 12997637966), ('to', 12136980858), ('a', 9081174698)]

from itertools import islice
import pkg_resources
from symspellpy import SymSpell

sym_spell = SymSpell()
dictionary_path = pkg_resources.resource_filename(
    "symspellpy", "frequency_dictionary_en_82_765.txt")
sym_spell.load_dictionary(dictionary_path, 0, 1)

# Print out first 5 elements to demonstrate that dictionary is
# successfully loaded
print(list(islice(sym_spell.words.items(), 5)))

但是下一个代码块返回一个空列表

from itertools import islice
import pkg_resources
from symspellpy import SymSpell

sym_spell = SymSpell()
dictionary_path = pkg_resources.resource_filename(
    "symspellpy", "frequency_dictionary_en_82_765.txt")
sym_spell.load_bigram_dictionary(dictionary_path, 0, 2)

# Print out first 5 elements to demonstrate that dictionary is
# successfully loaded
print(list(islice(sym_spell.bigrams.items(), 5)))

预期产出为:

[('abcs of', 10956800), ('aaron and', 10721728), ('abbott and', 7861376), ('abbreviations and', 13518272), ('aberdeen and', 7347776)]

根据本页:

https://symspellpy.readthedocs.io/en/latest/examples/dictionary.html

我想知道我在第二段代码中犯的错误


Tags: andtopath代码fromimportdictionarypkg
1条回答
网友
1楼 · 发布于 2024-05-18 14:31:11

链接页面和您的问题中给出的第二个示例引用了错误的数据文件。您必须参考附带的bigram数据文件

解释示例的文档显示了每个示例的预期数据格式,并且格式不同。然而,这两个示例引用的是同一个数据文件。这在某个地方肯定是错误的,第二个例子应该引用bigram数据文件,这是错误的

以下是正确运行的完整代码:

from itertools import islice
import pkg_resources
from symspellpy import SymSpell

sym_spell = SymSpell()
dictionary_path = pkg_resources.resource_filename(
    "symspellpy", "frequency_bigramdictionary_en_243_342.txt") # << - fixed to refer to the bigram data file
sym_spell.load_bigram_dictionary(dictionary_path, 0, 2)

# Print out first 5 elements to demonstrate that dictionary is
# successfully loaded
print(list(islice(sym_spell.bigrams.items(), 5)))

结果:

[('abcs of', 10956800), ('aaron and', 10721728), ('abbott and', 7861376), ('abbreviations and', 13518272), ('aberdeen and', 7347776)]

相关问题 更多 >

    热门问题