## 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")
)
使用Pycherts提取文本主题并绘制主题河流图
于 2023-11-29 22:40:30 首次发布