使用Pycherts提取文本主题并绘制主题河流图

## Pycherts
import re
import jieba
import pandas as pd
import openpyxl
import pyecharts.options as opts
from pyecharts.charts import ThemeRiver
	
def find(word,words,num):
	list = []
	sum_num = len(words)
	step = int(sum_num / num)
	for i in range(num):
	    total = 0
	    list_1=[i]
	    new_list = words [i*step:(i+1)*step]
	    for new_word in new_list:
	        if new_word==word :
	            total+=1
	    list_1.append(total)
	    list_1.append(word)
	    list.append(list_1)
	return list

txt=open(r"text.txt",'r',encoding='utf-8').read() #读取数据集
words=jieba.lcut(txt)
count={}
sum = len(words)
for word in words:
	if len(word)==1:
	    continue
	else:
	    count[word]=count.get(word,0)+1
items=list(count.items())
items.sort(key=lambda x:x[-1],reverse=True)
x_data=[]
y_data=[]
for word in range(5):
	x_data.append(items[word][0])
for ci in x_data:
	y = find(ci, words, 20)
	for i in y:
	    y_data.append(i)
(
	ThemeRiver(init_opts=opts.InitOpts(width="1600px", height="800px")).add(
	    series_name=x_data,
	    data=y_data,
	    label_opts=opts.LabelOpts(font_size = 18),
	    singleaxis_opts=opts.SingleAxisOpts(
	        pos_top="50", pos_bottom="50"
	    ),
	).set_global_opts(
	    tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"),
	    title_opts=opts.TitleOpts(title="River diagram of Theme", subtitle = "From 2019-12-01 to 2020-05-01",pos_bottom = "85%", pos_right = "20%")
	    ).render("theme_river.html")
)
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值