发布时间:2019-08-09 09:56:34编辑:auto阅读(1499)
pip install jieba
pip install matplotlib
pip install wordcloud
pip install snownlp
# -*- coding: utf-8 -*-
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 加载自定义分词字典
jieba.load_userdict("news.txt")
# 语料
corpos = "美媒称,鉴于全球石油市场过度供给的情况,中国原油需求下滑是其首要担忧之一。过量生产拉低了石油价格,但是中国过去一年左右的疲弱需求引发了缓慢的回弹。"
seg_list = jieba.cut(corpos)
seg_list2 = jieba.cut(corpos)
text = " ".join(seg_list)
# 词频统计
segStat = {}
for seg in seg_list2:
if seg in segStat:
segStat[seg] += 1
else:
segStat[seg] = 1
print segStat
# 创建词云
wordcloud = WordCloud(font_path="D:\\PDM\\2.1\\simhei.ttf", background_color="black").generate(text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
# -*- coding: utf-8 -*-
import jieba.analyse
# 语料
corpos = "美媒称,鉴于全球石油市场过度供给的情况,中国原油需求下滑是其首要担忧之一。过量生产拉低了石油价格,但是中国过去一年左右的疲弱需求引发了缓慢的回弹。"
# 设置停用词
jieba.analyse.set_stop_words("stop_words.txt")
# 提取关键词
#tags = jieba.analyse.extract_tags(corpos, topK=5)
tags = jieba.analyse.textrank(corpos, topK=5, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))
print(",".join(tags))
# -*- coding: utf-8 -*-
import jieba.analyse
from snownlp import SnowNLP
corpos = u"美媒称,鉴于全球石油市场过度供给的情况,中国原油需求下滑是其首要担忧之一。过量生产拉低了石油价格,但是中国过去一年左右的疲弱需求引发了缓慢的回弹。"
# 抽取文章关键词
tags = jieba.analyse.extract_tags(corpos, topK=5)
text1 = []
for tag in tags:
text1.append(tag)
print text1
# 文章列表[[doc1],[doc2],[doc3]...]
text = [text1,[u"文章",u"doc2"],[u"这是doc3"]]
text2 = text1
s = SnowNLP(text)
# 值越大越相似
print s.sim(text2)# [1.8325582915371863, 0, 0]
# -*- coding: utf-8 -*-
from snownlp import SnowNLP
text1 = u"美媒称,鉴于全球石油市场过度供给的情况,中国原油需求下滑是其首要担忧之一。过量生产拉低了石油价格,但是中国过去一年左右的疲弱需求引发了缓慢的回弹。"
s = SnowNLP(text1)
print s.summary(3)
上一篇: EIGRP笔记(3)
下一篇: 小甲鱼python视频教程
47494
45795
36793
34325
28968
25598
24443
19611
19111
17633
5466°
6048°
5570°
5638°
6573°
5376°
5378°
5885°
5855°
7171°