首页 > 解决方案 > 使用 symspellpy 的拼写检查器

问题描述

我想制作一个拼写检查器,如果有任何错误,它可以更正用户输入。我使用 symspellpy 和海关数据库作为字典

from symspellpy import SymSpell

sym_spell = SymSpell()
corpus_path = 'D:\Pursuit soft\dict\words2.txt'
sym_spell.create_dictionary(corpus_path)

print(sym_spell.words)

现在当我使用以下代码时

from symspellpy import SymSpell, Verbosity
input_term='cmments'
suggestions = sym_spell.lookup(input_term, Verbosity.CLOSEST,
                               max_edit_distance=2, include_unknown=True)

for suggestion in suggestions:
    print(suggestion)

结果是:“cmments”变成了“comments, 1, 8”..这是正确的..但我只是单词而不是距离和频率..如何只提取单词?

标签: python

解决方案


lookup 的返回值是一个SuggestItems 的列表。您可以通过以下方式获得建议的术语:

print(suggestion.term)

推荐阅读