一.jieba分词基本用法
1.一般都采用精确分词模式,直接jieba.cut(str1),
2.识别新词默认开启。
3.词性标注jieba.posseg,需要才用
- 全分词
- 精确分词
- paddle模式
- 搜索引擎模式分词
二.精确度调整
1.字典调整
(1)使用自定义词典:load_userdict()函数
#1.使用用户自定义词典
jieba.load_userdict('hello.txt')
seg_list=jieba.cut(str1)
print('使用自定义词典分词结果:','/'.join(seg_list))
(2) 动态修改词典
#2.动态修改词典 add_word() del_word() jieba.load_userdict('hello.txt') jieba.add_word('我来到了') jieba.del_word('发现这儿') seg_list=jieba.cut(str1) print('动态修改词典分词结果:','/'.join(seg_list))
2.调节词频
#调节词频 suggest_freq()函数
jieba.suggest_freq(('真','不错'),True)
jieba.suggest_freq('我来到了',True)
seg_list=jieba.cut(str1)
print('调节词频结果:','/'.join(seg_list))
3.停用词过滤
with open('cn_stopwords.txt','r+',encoding='utf-8')as fp:
stopwords=fp.read().split('\n')
word_list=[]
text='商务部4月23号发布的数据显示,一季度,全国农产品网络零售额达936.8亿元,增长31.0%;电商直播超过400万场。电商给农民带来了新的机遇。'
seg_list=jieba.cut(text)
for seg in seg_list:
if seg not in stopwords:
word_list.append(seg) #list append方法
print(word_list)
print('启用停用词过滤时的分词结果:\n','/'.join(word_list))
四.关键词提取
TF-IDF和TextRank
# 1.基于TF-IDF算法的关键词提取 extract_tags()
from jieba import analyse
keywords=analyse.extract_tags(str1,topK=2,withWeight=True,allowPOS=('n','v'))
print(keywords)
#2.基于TextRank 算法的关键词提取:textrank()函数
#textrank()函数能基于TextRank算法提取关键字
keywords=analyse.textrank(str1,topK=2,withWeight=True,allowPOS=('n','v'))
print(keywords)
词语概率统计
五.词频统计
text='蒸馍馍锅锅蒸馍馍,馍馍蒸了一锅锅,馍馍隔上桌桌,桌桌上面有馍馍。'
with open('cn_stopwords.txt','r+',encoding='utf-8')as fp:
stopwords=fp.read().split('\n')
word_list={}
jieba.suggest_freq(('桌桌'),True)
seg_list=jieba.cut(text)
for seg in seg_list:
if seg not in stopwords:
if seg in word_list.keys():
word_list[seg]+=1
else:
word_list[seg]=1
print(word_list)