hanlp云服务器接口使用和pyecharts词云可视化
这次是我跟一位小伙伴一起做得,现在让我把他艾特出来:maxoyed
本次应用的数据是老人与海的部分数据(old.txt)
领取方式:
公众号:YOLO的学习进阶日常
因为现在网盘现在不太方便了嘛,所以我把我的联系方式放在了我得公众号里面,需要数据的可以直接私聊我
# 请求HanLP云服务接口
import requests
import json
with open("./old.txt",'r', encoding='utf-8') as txt:
text=txt.read()
hanlp云服务器:http://27.221.81.242:8848/HanLP1.0/dyyfc-crf.html#
如何找到我们想要的接口:
第一步:找到产品中心
第二步:选择我们想实现的功能,这次我选择是CRF分词
第三步:把接口和API的写法写成这个样子
第四步:通过requests和json去解析我们的数据
url = "http://27.221.81.242:3333/HanlpApi?apiKey=030e5ecf943f49bc8e218135ffa511bf"
payload = {'text': text}
response = requests.post(url, data = payload)
res = json.loads(response.text)["data"]
res_list = list(map(lambda item: item["word"], res))
res_list
停用词
停用词处理,停用词的获取方式就是关注公众号然后回复停用词
#停用词
stop_words = []
with open("./cn_stopwords.txt", 'r', encoding='utf-8') as txt:
stop_words = txt.read().split('\n')
**
词频统计
**
# 词频统计
counter = {}
for word in res_list:
if len(word) < 2:
continue
if word in stop_words:
continue
try:
counter[word] += 1
except:
counter[word] = 1
wordcloud_data = list(map(lambda item : (item, counter[item]), counter))
wordcloud_data
pyecharts词云
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
# 添加词频数据
words = wordcloud_data[:20]
# WordCloud模块,链式调用配置,最终生成html文件
c = (
WordCloud()
.add("", words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-shape-diamond"))
.render("wordcloud_diamond.html")
)