使用Python进行英文单词分割

  由于在一些场景中,所有的字母都连在了一起,所以我们需要将字母分割成单词的形式。

1. 安装

pip install -U symspellpy

2. 下载词典

curl -LJO https://raw.githubusercontent.com/mammothb/symspellpy/master/symspellpy/frequency_dictionary_en_82_765.txt
curl -LJO https://raw.githubusercontent.com/mammothb/symspellpy/master/symspellpy/frequency_bigramdictionary_en_243_342.txt

  如果下载不了的话,也可以在CSDN上进行下载,链接为https://download.csdn.net/download/herosunly/12326704。

3. 单词分割

import pkg_resources
from symspellpy.symspellpy import SymSpell

sym_spell = SymSpell(max_dictionary_edit_distance=0, prefix_length=7)
dictionary_path = pkg_resources.resource_filename(
    "symspellpy", "frequency_dictionary_en_82_765.txt")

sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)

# a sentence without any spaces
input_term = "thequickbrownfoxjumpsoverthelazydog"
result = sym_spell.word_segmentation(input_term)
print("{}, {}, {}".format(result.corrected_string, result.distance_sum,
                          result.log_prob_sum))
发布了178 篇原创文章 · 获赞 389 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/herosunly/article/details/105513582
今日推荐