使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化
實驗題目:聆心云心理健康服務平臺數據可視分析和可視化
實驗目的和要求:統計出在聆心云平臺做沙盤游戲的次數、根據各次沙盤游戲所使用的沙具和進行的操作數據進行詞云可視化,掌握Python詞云制作方法
實驗步驟:
1.定義函數getUserInfo(),獲取用戶輸入的聆心云平臺用戶名和密碼?
def getUserInfo(): #獲取用戶輸入(聆心云平臺用戶名和密碼)userInfo={}userInfo['mobile']=input("請輸入聆心云用戶名(注冊賬號的手機號碼):") userInfo['passwd']=input("請輸入密碼:") return userInfo2. 定義函數getIdList(userInfo),參數為步驟1獲取的用戶名和密碼,通過聆心云心理健康服務平臺API獲取所有沙盤游戲id,并統計出游戲次數:
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("共做了{}次沙盤游戲。".format(len(idList))) #打印做沙盤的次數return idListexcept:return "服務器異常!"效果如下:
?? 3. 定義函數generateTextFile(idList),入參為步驟2獲取的所有沙盤id,該函數的作用是通過這些id依次獲取所有沙盤游戲所使用的沙具和進行的操作數據,并輸出到文本文件data.txt。
def generateTextFile(idList): #獲取所有沙盤游戲所使用的沙具和進行的操作數據,輸出到文本文件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("獲取并輸出游戲數據成功!用時{}秒。".format(s))except:return "服務器異常!"運行結果如下:
生成的文件如下:
4.詞云生成前的準備工作,安裝第三方庫wordcloud、jieba、imageio,jieba庫用來對中文進行分詞處理,imageio庫用來讀取背景圖片,wordcloud庫用來生成詞云圖片,windows下分別在命令行輸入以下命令進行安裝:
pip install wordcloud pip install jieba pip install imageio準備一張背景圖放到目錄下,可根據背景圖生成詞云的形狀
5. 定義函數generateWordcloud(),根據文本文件和背景圖片生成詞云。
def generateWordcloud(): #根據文本文件生成詞云fo=open('data.txt',encoding='utf-8')t=fo.read() #讀取操作數據fo.close() words = jieba.lcut(t) # 使用jieba庫進行分詞txt = ' '.join(words) # 使用join()方法,將分詞生成的字符串以空格進行分割。因為在生成詞云時,字符串之間需要為空格background_image=imageio.imread('background.png') #讀取背景圖片 w=wc.WordCloud(background_color='white',# 設置背景顏色font_path = 'C:/Waedows/Fonts/simfang.ttf',# 設置字體mask=background_image # 設置背景圖片)w.generate(txt) #生成詞云 w.to_file('WordCloud.png') # 生成圖片print("生成詞云成功!")運行結果如下:
最后生成的詞云效果:
附完整代碼:
import requests import json import time import wordcloud as wc import jieba import imageiodef getUserInfo(): #獲取用戶輸入(聆心云平臺用戶名和密碼)userInfo={}userInfo['mobile']=input("請輸入聆心云用戶名(注冊賬號的手機號碼):") 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("共做了{}次沙盤游戲。".format(len(idList))) #打印做沙盤的次數return idListexcept:return "服務器異常!" def generateTextFile(idList): #獲取所有沙盤游戲所使用的沙具和進行的操作數據,輸出到文本文件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("獲取并輸出游戲數據成功!用時{}秒。".format(s))except:return "服務器異常!" def generateWordcloud(): #根據文本文件生成詞云fo=open('data.txt',encoding='utf-8')t=fo.read() #讀取操作數據fo.close() words = jieba.lcut(t) # 使用jieba庫進行分詞txt = ' '.join(words) # 使用join()方法,將分詞生成的字符串以空格進行分割。因為在生成詞云時,字符串之間需要為空格background_image=imageio.imread('background.png') #讀取背景圖片 w=wc.WordCloud(background_color='white',# 設置背景顏色font_path = 'C:/Waedows/Fonts/simfang.ttf',# 設置字體mask=background_image # 設置背景圖片)w.generate(txt) #生成詞云 w.to_file('WordCloud.png') # 生成圖片print("生成詞云成功!")userInfo=getUserInfo() idList=getIdList(userInfo) generateTextFile(idList) generateWordcloud()實驗總結:通過本次實驗加深了對數據可視化的理解,學到了文本數據可視化——詞云圖的制作方法,感受到用Python制作詞云的方便快捷,代碼簡潔,Python安裝第三方庫也快速高效,體現了Python語言的龐大的計算生態,對Python語言有了更深刻的認識。
總結
以上是生活随笔為你收集整理的使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机通讯录备份
- 下一篇: websocket python爬虫_p