jieba工具安装:打开命令行,输入“pip install jieba”;
jieba分词的三种模式:
1、精确模式:将句子最精确地切分,适合文本分析;
2、全模式:把句子中所有的可以成词的词语都扫描出来,但不能解决起义问题;
3、搜索引擎模式:在精确模式的基础上,对长词在切分,提高召回率,适用于搜索引擎分词。
jieba分词的主要方法:
1、jieba.cut(str,cut_all=,HMM=):str是需要分词的字符串,cut_all表示是否使用全模式,HMM表示是否使用HMM模型。
2、jieba.cut_for_search(str,HMM):str是需要分词的字符串,HMM表示是否使用HMM模型.适用于搜索引擎构建倒排索引。
3、jieba.load_userdict(fliename):载入自定义词典,文件必须是utf-8编码。
无载入词典的可选择三种模式的jieba分词的代码
"""
测试jieba分词的三种模式
"""
import jieba
def jieba_cut_words(words_input,model):
cut_words_list = []
#三个参数选项,第一个是需要分词的字符串,cut_all参数是否使用全模式,HMM参数是否使用HMM模型
if model == "1":
cut_words_list = jieba.cut(words_input)#默认为精确模式
elif model == "2":
cut_words_list = jieba.cut(words_input,cut_all=True)
elif model == "3":
cut_words_list = jieba.cut_for_search