生成一個(gè)包含多個(gè)文本文件的詞云错森,可以將所有文本文件合并為一個(gè)字符串,然后將該字符串傳遞給WordCloud類生成詞云對象惹想。下面是一個(gè)示例代碼:
# 導(dǎo)入必要的庫
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import os
# 讀取多個(gè)文本文件并合并為一個(gè)字符串
text = ''
for file_name in os.listdir('text_files'):
if file_name.endswith('.txt'):
with open(os.path.join('text_files', file_name), 'r', encoding='utf-8') as f:
text += f.read()
# 生成詞云對象
wordcloud = WordCloud(background_color="white").generate(text)
# 顯示詞云圖
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
使用os.listdir()函數(shù)獲取text_files目錄下的所有文件名问词,并循環(huán)遍歷這些文件。
以.txt結(jié)尾的文件嘀粱,將其內(nèi)容讀取到字符串變量text中激挪。
使用WordCloud類創(chuàng)建了一個(gè)詞云對象wordcloud,并使用Matplotlib庫將其顯示出來锋叨。
需要注意的是垄分,在合并文本文件時(shí),需要確保文本文件的編碼方式相同娃磺。
如果文件使用不同的編碼方式薄湿,可能會(huì)導(dǎo)致合并后的文本亂碼。
.txt文件中讀取文本數(shù)據(jù)偷卧,然后使用WordCloud類創(chuàng)建了一個(gè)詞云對象wordcloud豺瘤。通過generate()方法,我們將文本數(shù)據(jù)傳遞給詞云對象听诸,并生成詞云圖坐求。最后,使用Matplotlib庫將詞云圖顯示出來
例取前文獲取的許嵩歌詞經(jīng)處理后數(shù)據(jù)集晌梨,應(yīng)用詞云如下:
# 導(dǎo)入必要的庫
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import os
import imageio # 圖像模塊
import jieba # 中文分詞庫
img = imageio.imread('D:/cat.png')
# 讀取多個(gè)文本文件并合并為一個(gè)字符串
text = ''
for file_name in os.listdir('G:/lyric/xs/new'):
if file_name.endswith('.txt'):
with open(os.path.join('G:/lyric/xs/new', file_name), 'r', encoding='utf-8') as f:
txt= f.read()
words = " ".join(jieba.cut(txt))
text +=words
# 生成詞云對象
wordcloud = WordCloud(background_color='#ccccff',mask=img,font_path='msyh.ttc',
max_words=800,
stopwords=set([line.strip() for line in open('cn_stopwords2.txt', mode='r',
encoding='utf-8').readlines()])
).generate(text)
# 顯示詞云圖
# 保存詞云圖像
wordcloud.to_file("G:/newcat.png")
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
最大詞數(shù)3000下部分文本的詞云
3000
2000
1500
1000
500
300
800
newcat8.png
newcat28.png
#停用詞自定義
[
【
]
】
'
:
:
桥嗤;
;
1须妻、
2、
3泛领、
4荒吏、
5、
6渊鞋、
7绰更、
8、
9锡宋、
10动知、
、
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
rn
br
strong
&
li
em
style
ol
text
!
"
#
$
%
&
'
(
)
*
+
,
-
--
.
..
...
......
...................
./
.一
/
//
0
1
2
3
4
5
6
7
8
9
:
://
::
;
<
=
>
>>
?
@
A
Lex
[
\
]
^
_
`
exp
sub
sup
|
}
~
~~~~
·
×
×××
Δ
Ψ
γ
μ
φ
φ.
В
—
——
———
‘
’
’‘
“
”
”员辩,
…
……
…………………………………………………③
′∈
′|
℃
Ⅲ
↑
→
∈[
∪φ∈
≈
①
②
②c
③
③]
④
⑤
⑥
⑦
⑧
⑨
⑩
──
■
▲
、
鸵鸥。
〈
〉
《
》
》)奠滑,
」
『
』
【
】
〔
〕
〕〔
㈧
一
一.
一
哼
哼唷
唉
唯有
啊
啊呀
啊哈
啊喲
啐
啥
啦
啪達(dá)
啷當(dāng)
喀
喂
喏
喔唷
嘍
嗡
嗡嗡
嗬
嗯
噯
嘎
嘎嘎
嘎登
噓
嘛
嘻
嘿
嘿嘿
嘟
你
我
他
也
我們
什么
有
這
去
了
還
沒有
不
又
為
對
上
自己
下
吧
把
能
像
那些
那
誰
人
卻
一個(gè)
沒
讓
著
人
就
到
已
很
會(huì)
做
她
它
和
在
要
都
的
是
里
︿
!
#
$
%
&
'
(
)
)÷(1-
)妒穴、
*
+
+ξ
++
宋税,
,也
-
-β
--
-[*]-
.
/
0
0:2
1
1.
12%
2
2.3%
3
4
5
5:0
6
7
8
9
:
讼油;
<
<±
<Δ
<λ
<φ
<<
=
=″
=☆
=(
=-
=[
={
>
>λ
杰赛?
@
A
LI
R.L.
ZXFITL
[
[①①]
[①②]
[①③]
[①④]
[①⑤]
[①⑥]
[①⑦]
[①⑧]
[①⑨]
[①A]
[①B]
[①C]
[①D]
[①E]
[①]
[①a]
[①c]
[①d]
[①e]
[①f]
[①g]
[①h]
[①i]
[①o]
[②
[②①]
[②②]
[②③]
[②④
[②⑤]
[②⑥]
[②⑦]
[②⑧]
[②⑩]
[②B]
[②G]
[②]
[②a]
[②b]
[②c]
[②d]
[②e]
[②f]
[②g]
[②h]
[②i]
[②j]
[③①]
[③⑩]
[③F]
[③]
[③a]
[③b]
[③c]
[③d]
[③e]
[③g]
[③h]
[④]
[④a]
[④b]
[④c]
[④d]
[④e]
[⑤]
[⑤]]
[⑤a]
[⑤b]
[⑤d]
[⑤e]
[⑤f]
[⑥]
[⑦]
[⑧]
[⑨]
[⑩]
[*]
[-
[]
]
]∧′=[
][
_
a]
b]
c]
e]
f]
ng昉
{
{-
|
}
}>
~
~±
~+
¥