這是沒有洗文本的結果,本文的目標,就是把人名給洗出來~~~
前置背景
基于這個程式需要用到下面三個庫
import jieba
import wordcloud
from imageio import imread
所以,需要先在命令列視窗(cmd)安裝第三方庫
pip install jieba
pip install wordcloud
pip install imageio
- jieba是中文分詞第三方庫
- wordcloud是優秀的詞云展示第三方庫
步驟分析
- 從《左耳》小說檔案讀入文本資訊
- 處理文本,拿我所需
- 對文本內容進行排序
- 繪制詞云
讀入文本資訊
這里采用一次性讀取檔案內容的方式,得到的是一個長字串,
t = open("左耳.txt", "r").read()
處理文本資訊
這里我試了三種方法,
- 定義一個
excludes的集合,來放我們需要排除的資訊,為了獲得這個集合內容,首先就要找出小說中出現的高頻詞,來看下面代碼,
import jieba
import wordcloud
from imageio import imread
t = open("左耳.txt", "r").read()
words = jieba.lcut(t) # 中文分詞,回傳串列型別
counts = {} # 空字典:記錄人名出現次數
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0)+1 # word數為0時,補充字典
# 把字典轉換成串列型別,進行排序
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
# 篩選文本資訊
# 輸出100個方便排除非人名文本
for i in range(100):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
輸出了前100個出現高頻詞:
一個 442
沒有 352
我們 344
知道 333
時候 288
什么 253
自己 250
許弋 248
張漾 223
看著 193
起來 191
看到 184
已經 175
蔣皎 153
那個 149
這樣 142
這個 141
然后 140
不是 140
黑人 133
可以 129
忽然 124
還是 124
一下 123
怎么 119
喜歡 118
有些 117
真的 115
一直 113
不會 113
眼睛 112
就是 109
地說 108
一樣 102
手機 102
這里 101
這么 101
電話 100
樣子 98
開始 93
出來 92
女生 92
他們 91
告訴 88
海生 87
只是 86
夏米 84
不要 83
李珥 82
小耳朵 82
心里 79
終于 78
北京 78
學校 76
可是 75
不過 75
那里 72
應該 71
回來 71
哪里 70
一起 70
好像 69
晚上 69
于是 68
離開 67
推開 67
過去 67
過來 65
一定 65
為什么 64
覺得 62
面前 62
聲音 61
男生 61
因為 61
兩個 61
那天 60
是不是 60
微笑 60
愛情 59
酒吧 59
如果 58
很快 58
一切 58
媽媽 57
下來 57
一次 57
其實 57
一把 57
東西 57
所以 56
感覺 56
永遠 56
相信 55
到底 55
很多 54
發現 54
現在 54
跟著 53
說完 53
一看,事情不太對,100個詞里面的人名寥寥無幾,所以我戰術性放棄這種方法,
- 采用人名篩選的方法,定義了一個
includes集合,存放小說里出現的人物,
李珥(小耳朵)
張漾
黎吧啦(吧啦)
許弋(許帥)
夏米米
夏吉吉
蔣皎 (蔣雅希)
直接根據人物特點進行查找,但是又發現了一個問題(是我學藝太淺了hhh),對不在includes集合里的詞塊進行洗掉,是件很繞的事情,還出現了下標越界的錯誤,
所以,轉變思路,again!
我只統計我要的詞塊不就好了嗎~
- 取我所需
使用字典進行統計,
當然,一個人總有昵稱啥的,所以我進行了名字關聯,
elif word == "李珥" or word == "珥" or word == "小耳朵" or word == "耳朵":
rword = "李珥"
elif word == "張漾" or word == "漾":
rword = "張漾"
elif word == "許弋" or word == "許帥" or word == "帥哥" or word == "弋":
rword = "許弋"
elif word == "黎吧啦" or word == "黎" or word == "吧啦" or word == "吧" or word == "啦":
rword = "黎吧啦"
elif word == "夏米米" or word == "夏米" or word == "米米" or word == "米":
rword = "夏米米"
elif word == "夏吉吉" or word == "夏吉" or word == "吉吉" or word == "吉":
rword = "夏吉吉"
elif word == "蔣皎" or word == "蔣雅希" or word == "雅希":
rword = "蔣皎"
文本排序
處理文本資訊完畢后,得到的是一個字典,為了呼叫串列庫里的sort排序函式,使用list()把字典轉為串列型別,
# 把字典轉換成串列型別,進行排序
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
這里先對串列內容進行列印,好像有那么點味道了~
for i in range(7):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
運行結果
張漾 9917
許弋 8674
李珥 8490
蔣皎 5580
夏米米 3252
夏吉吉 2890
黎吧啦 157
繪制詞云
對于這個程式,我們處理的是一個中文字串,所以中文需要先分詞并組成空格分隔字串,
s = ""
for i in range(len(counts)):
word, count = items[i]
s += (str(word)+" ") * count
使用這段代碼創建詞塊間有空格的長字串,
接下來是經典的詞云操作三部曲,
w = wordcloud.WordCloud(font_path="msyh.ttc",\
background_color="white")
w.generate(s)
w.to_file("左耳8.png")
運行結果

詞語重復輸出問題解決
生成的圖片一看,怎么詞語會出現兩次?
查詢了一下,與collocations引數有關,默認collocations=True,會統計搭配詞,比如你的text是“我在拜訪客戶”,當collocations為True時,就會把“拜訪客戶”也當作一個詞進行統計,所以會出現重復,
wcd=WordCloud(font_path='simsun.ttc', collocations=False,width=900,height=400,background_color='white',max_words=100,scale=1.5).generate(text)
更改之后輸出

總代碼
import jieba
import wordcloud
from imageio import imread
t = open("左耳.txt", "r").read()
words = jieba.lcut(t) # 中文分詞,回傳串列型別
counts = {} # 空字典:記錄人名出現次數
rword = "黎吧啦"
for word in words:
# 名字關聯
if len(word) == 1:
continue
elif word == "李珥" or word == "珥" or word == "小耳朵" or word == "耳朵":
rword = "李珥"
elif word == "張漾" or word == "漾":
rword = "張漾"
elif word == "許弋" or word == "許帥" or word == "帥哥" or word == "弋":
rword = "許弋"
elif word == "夏米米" or word == "夏米" or word == "米米" or word == "米":
rword = "夏米米"
elif word == "夏吉吉" or word == "夏吉" or word == "吉吉" or word == "吉":
rword = "夏吉吉"
elif word == "蔣皎" or word == "蔣雅希" or word == "雅希":
rword = "蔣皎"
elif word == "黎吧啦" or word == "黎" or word == "吧啦" or word == "吧" or word == "啦":
rword = "黎吧啦"
counts[rword] = counts.get(rword, 0)+1 # word數為0時,補充字典
counts["左耳"] = 500
counts["情話"] = 400
counts["離心臟最近的地方"] = 400
counts["傷痛文學"] = 300
# print(counts.keys())
# 把字典轉換成串列型別,進行排序
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
# 創建詞云
s = ""
for i in range(len(counts)):
word, count = items[i]
s += (str(word)+" ") * count
w = wordcloud.WordCloud(font_path="simsun.ttc",collocations=False, \
background_color="white", width = 300, height = 150)
w.generate(s)
w.to_file("左耳.png")
文章結束,作者深鞠躬!
有錯誤或者有優化的地方,歡迎指出!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/305464.html
標籤:python
