日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打开文件要wordcloud吗,使用python创建wordcloud

發布時間:2023/12/10 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打开文件要wordcloud吗,使用python创建wordcloud 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在嘗試在清理文本文件后在python中創建wordcloud,

我得到了所需的結果,即大多數在文本文件中使用但無法繪制的單詞.

我的代碼:

import collections

from wordcloud import WordCloud

import matplotlib.pyplot as plt

file = open('example.txt', encoding = 'utf8' )

stopwords = set(line.strip() for line in open('stopwords'))

wordcount = {}

for word in file.read().split():

word = word.lower()

word = word.replace(".","")

word = word.replace(",","")

word = word.replace("\"","")

word = word.replace("“","")

if word not in stopwords:

if word not in wordcount:

wordcount[word] = 1

else:

wordcount[word] += 1

d = collections.Counter(wordcount)

for word, count in d.most_common(10):

print(word , ":", count)

#wordcloud = WordCloud().generate(text)

#fig = plt.figure()

#fig.set_figwidth(14)

#fig.set_figheight(18)

#plt.imshow(wordcloud.recolor(color_func=grey_color, random_state=3))

#plt.title(title, color=fontcolor, size=30, y=1.01)

#plt.annotate(footer, xy=(0, -.025), xycoords='axes fraction', fontsize=infosize, color=fontcolor)

#plt.axis('off')

#plt.show()

編輯:

用以下代碼繪制wordcloud:

wordcloud = WordCloud(background_color='white',

width=1200,

height=1000

).generate((d.most_common(10)))

plt.imshow(wordcloud)

plt.axis('off')

plt.show()

但是得到TypeError:預期的字符串或緩沖區

當我用.generate(str(d.most_common(10))嘗試上述代碼時

形成的單詞云在幾個單詞之后顯示’trotrophe(‘)符號

using Jupyter Notebook | python3 | Ipython

解決方法:

首先將此文件Symbola.ttf下載到以下腳本的當前文件夾中.

架構文件:

file.txt Symbola.ttf my_word_cloud.py

file.txt的:

foo buzz bizz foo buzz bizz foo buzz bizz foo buzz bizz foo buzz bizz

foo foo foo foo foo foo foo foo foo foo bizz bizz bizz bizz foo foo

my_word_cloud.py:

import io

from collections import Counter

from os import path

import matplotlib.pyplot as plt

from wordcloud import WordCloud

d = path.dirname(__file__)

# It is important to use io.open to correctly load the file as UTF-8

text = io.open(path.join(d, 'file.txt')).read()

words = text.split()

print(Counter(words))

# Generate a word cloud image

# The Symbola font includes most emoji

font_path = path.join(d, 'Symbola.ttf')

word_cloud = WordCloud(font_path=font_path).generate(text)

# Display the generated image:

plt.imshow(word_cloud)

plt.axis("off")

plt.show()

結果:

Counter({'foo': 17, 'bizz': 9, 'buzz': 5})

請參閱許多其他示例,在這里我為您創建了一個簡單示例:

標簽:word-cloud,python,matplotlib,plot

來源: https://codeday.me/bug/20191013/1905740.html

總結

以上是生活随笔為你收集整理的python打开文件要wordcloud吗,使用python创建wordcloud的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。