下面提到的代码返回预期的输出。
[('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/