python - 返回一个空列表而不是二元组

标签 python spell-checking

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

[('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

我想知道我在第二部分代码中犯的错误。

最佳答案

链接页面上给出的第二个示例以及您的问题中给出的第二个示例引用了错误的数据文件。您必须引用包含的二元组数据文件。

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

这是正确运行的完整代码:

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

关于python - 返回一个空列表而不是二元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65757224/

相关文章:

python - Pandas xlrd 引擎传递了静态值错误

python - 使用 doxygen 记录 Python 包

html - 通过 HTML/css 关闭 Chrome/Safari 拼写检查

python - 多索引数据帧的基于整数的 (K,N) 索引

python - Pandas 数据框不根据 csv 中的逗号分隔列

Python Flask - request.json 返回无类型而不是 json 字典

latex - 如何对多种语言/语言变体的 LaTeX 进行拼写检查

c# - 如何替换 richtextbox 中插入符号位置下的单词? - 实现拼写检查建议

reactjs - 为 react 应用程序添加拼写检查的最佳方法?

java - 构建solr字典索引