資源簡介
使用第三方庫wordcloud將中英文混合的文本轉(zhuǎn)換成詞云圖片,這是我在學(xué)習(xí)中將筆記轉(zhuǎn)換成詞云的程序,方便直觀地復(fù)習(xí)

代碼片段和文件信息
#know_cloud.py
‘‘‘
作者:星宇
功能:從.txt格式的文件中獲取文本,將其轉(zhuǎn)換成詞云
時間:2018/5/11
tip:程序文件和文本文件必須是在同一目錄下
‘‘‘
import?jieba?
import?wordcloud?as?wc?
????#獲取文本并做分詞處理
def?gettxt(filename):
????file?=?open(filename‘rt‘)
????txt?=?file.read()
????fuhao?=?“““,。?‘“”’:?<‘“>()()/!*\n? 12345678910123456789“““
????for?word?in?txt:
????????if?word?in?fuhao:
????????????txt?=?txt.replace(word‘‘)
????
????file.close()
????str_list?=?jieba.lcut(txt)
????return?‘?‘.join(str_list)
#處理文本,將文本轉(zhuǎn)換成詞云
def?dealtext(text):
????#本例的font_path是linux下的字體格式,如果是在windows
????#下運行,font_path需改成windows下的字體
????w?=?wc.WordCloud(width?=?750height?=?650\
???????????????font_path=‘NotoSansCJK.ttc‘\
????????????background_color=‘white‘)
????w.generate(text)
????w.to_file(“out.png“)
def?main():
????text?=?gettxt(file_name)
????dealtext(text)
file_name?=?input(“請輸入文本名稱:“)
main()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????I.A....??????1099??2018-05-16?17:15??know_cloud.py
-----------?---------??----------?-----??----
?????????????????1099????????????????????1
- 上一篇:百度遷徙自動腳本
- 下一篇:lstm實現(xiàn)時間序列一維預(yù)測
評論
共有 條評論