使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化
實(shí)驗(yàn)題目:聆心云心理健康服務(wù)平臺(tái)數(shù)據(jù)可視分析和可視化
實(shí)驗(yàn)?zāi)康暮鸵?#xff1a;統(tǒng)計(jì)出在聆心云平臺(tái)做沙盤游戲的次數(shù)、根據(jù)各次沙盤游戲所使用的沙具和進(jìn)行的操作數(shù)據(jù)進(jìn)行詞云可視化,掌握Python詞云制作方法
實(shí)驗(yàn)步驟:
1.定義函數(shù)getUserInfo(),獲取用戶輸入的聆心云平臺(tái)用戶名和密碼?
def getUserInfo(): #獲取用戶輸入(聆心云平臺(tái)用戶名和密碼)userInfo={}userInfo['mobile']=input("請輸入聆心云用戶名(注冊賬號(hào)的手機(jī)號(hào)碼):") userInfo['passwd']=input("請輸入密碼:") return userInfo2. 定義函數(shù)getIdList(userInfo),參數(shù)為步驟1獲取的用戶名和密碼,通過聆心云心理健康服務(wù)平臺(tái)API獲取所有沙盤游戲id,并統(tǒng)計(jì)出游戲次數(shù):
def getIdList(userInfo): #獲取所有沙盤id idList=[]url='https://lingxinyun.cn/sp/getIdByAuth'try:r=requests.get(url,params=userInfo,timeout=30)r.raise_for_status() ls=json.loads(r.text) for e in ls:idList.append(e['id'])print("共做了{(lán)}次沙盤游戲。".format(len(idList))) #打印做沙盤的次數(shù)return idListexcept:return "服務(wù)器異常!"效果如下:
?? 3. 定義函數(shù)generateTextFile(idList),入?yún)椴襟E2獲取的所有沙盤id,該函數(shù)的作用是通過這些id依次獲取所有沙盤游戲所使用的沙具和進(jìn)行的操作數(shù)據(jù),并輸出到文本文件data.txt。
def generateTextFile(idList): #獲取所有沙盤游戲所使用的沙具和進(jìn)行的操作數(shù)據(jù),輸出到文本文件begin=time.time()text=""url='https://lingxinyun.cn/sp/getSandPlay?id='try:fo=open("data.txt","w",encoding='utf-8')for id in idList:r=requests.get(url+str(id),timeout=30)r.raise_for_status()r.encoding='utf-8'fo.writelines(r.text+"\n")fo.close()end=time.time()s=end-beginprint("獲取并輸出游戲數(shù)據(jù)成功!用時(shí){}秒。".format(s))except:return "服務(wù)器異常!"運(yùn)行結(jié)果如下:
生成的文件如下:
4.詞云生成前的準(zhǔn)備工作,安裝第三方庫wordcloud、jieba、imageio,jieba庫用來對中文進(jìn)行分詞處理,imageio庫用來讀取背景圖片,wordcloud庫用來生成詞云圖片,windows下分別在命令行輸入以下命令進(jìn)行安裝:
pip install wordcloud pip install jieba pip install imageio準(zhǔn)備一張背景圖放到目錄下,可根據(jù)背景圖生成詞云的形狀
5. 定義函數(shù)generateWordcloud(),根據(jù)文本文件和背景圖片生成詞云。
def generateWordcloud(): #根據(jù)文本文件生成詞云fo=open('data.txt',encoding='utf-8')t=fo.read() #讀取操作數(shù)據(jù)fo.close() words = jieba.lcut(t) # 使用jieba庫進(jìn)行分詞txt = ' '.join(words) # 使用join()方法,將分詞生成的字符串以空格進(jìn)行分割。因?yàn)樵谏稍~云時(shí),字符串之間需要為空格background_image=imageio.imread('background.png') #讀取背景圖片 w=wc.WordCloud(background_color='white',# 設(shè)置背景顏色font_path = 'C:/Waedows/Fonts/simfang.ttf',# 設(shè)置字體mask=background_image # 設(shè)置背景圖片)w.generate(txt) #生成詞云 w.to_file('WordCloud.png') # 生成圖片print("生成詞云成功!")運(yùn)行結(jié)果如下:
最后生成的詞云效果:
附完整代碼:
import requests import json import time import wordcloud as wc import jieba import imageiodef getUserInfo(): #獲取用戶輸入(聆心云平臺(tái)用戶名和密碼)userInfo={}userInfo['mobile']=input("請輸入聆心云用戶名(注冊賬號(hào)的手機(jī)號(hào)碼):") userInfo['passwd']=input("請輸入密碼:") return userInfodef getIdList(userInfo): #獲取所有沙盤id idList=[]url='https://lingxinyun.cn/sp/getIdByAuth'try:r=requests.get(url,params=userInfo,timeout=30)r.raise_for_status() ls=json.loads(r.text) for e in ls:idList.append(e['id'])print("共做了{(lán)}次沙盤游戲。".format(len(idList))) #打印做沙盤的次數(shù)return idListexcept:return "服務(wù)器異常!" def generateTextFile(idList): #獲取所有沙盤游戲所使用的沙具和進(jìn)行的操作數(shù)據(jù),輸出到文本文件begin=time.time()text=""url='https://lingxinyun.cn/sp/getSandPlay?id='try:fo=open("data.txt","w",encoding='utf-8')for id in idList:r=requests.get(url+str(id),timeout=30)r.raise_for_status()r.encoding='utf-8'fo.writelines(r.text+"\n")fo.close()end=time.time()s=end-beginprint("獲取并輸出游戲數(shù)據(jù)成功!用時(shí){}秒。".format(s))except:return "服務(wù)器異常!" def generateWordcloud(): #根據(jù)文本文件生成詞云fo=open('data.txt',encoding='utf-8')t=fo.read() #讀取操作數(shù)據(jù)fo.close() words = jieba.lcut(t) # 使用jieba庫進(jìn)行分詞txt = ' '.join(words) # 使用join()方法,將分詞生成的字符串以空格進(jìn)行分割。因?yàn)樵谏稍~云時(shí),字符串之間需要為空格background_image=imageio.imread('background.png') #讀取背景圖片 w=wc.WordCloud(background_color='white',# 設(shè)置背景顏色font_path = 'C:/Waedows/Fonts/simfang.ttf',# 設(shè)置字體mask=background_image # 設(shè)置背景圖片)w.generate(txt) #生成詞云 w.to_file('WordCloud.png') # 生成圖片print("生成詞云成功!")userInfo=getUserInfo() idList=getIdList(userInfo) generateTextFile(idList) generateWordcloud()實(shí)驗(yàn)總結(jié):通過本次實(shí)驗(yàn)加深了對數(shù)據(jù)可視化的理解,學(xué)到了文本數(shù)據(jù)可視化——詞云圖的制作方法,感受到用Python制作詞云的方便快捷,代碼簡潔,Python安裝第三方庫也快速高效,體現(xiàn)了Python語言的龐大的計(jì)算生態(tài),對Python語言有了更深刻的認(rèn)識(shí)。
總結(jié)
以上是生活随笔為你收集整理的使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机通讯录备份
- 下一篇: 需求管理之需求优先级的排序-需求优先级分