【Cesium】自定义材质,添加带有方向的滚动路线
- 🍖 前言
- 🎶一、实现过程
- ✨二、代码展示
- 🏀三、运行结果
- 🏆四、知识点提示
🍖 前言
Python基于jieba和wordcloud绘制词云图
🎶一、实现过程
- 读取文本信息,采用jieba进行分词统计
- wordcloud配合matplotlib进行词云绘制(可以选择一张词云的背景轮廓图,本实例选择一张动物轮廓)
- 将词云图保存到本地、展示出来
✨二、代码展示
以下是组件源码(未包含自定义的材质类):
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
# 读取文本文件
def readText(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
return text
# 对文本进行分词
def segmentText(text):
# 使用jieba进行分词,精确模式
words = jieba.cut(text)
# 过滤掉单个字符的词
filtered_words = [word for word in words if len(word) > 1]
return filtered_words
# 统计词频
def countWords(words):
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
# 按词频降序排序
word_counts = dict(sorted(word_counts.items(), key=lambda x: x[1], reverse=True))
return word_counts
# 生成词云
def generateWordcloud(word_counts, background_image_path):
# 读取背景图片
background_image = Image.open(background_image_path)
mask = np.array(background_image)
# 创建WordCloud对象
wordcloud = WordCloud(
font_path='simhei.ttf',
background_color='white',
max_words=200,
mask=mask
)
wordcloud.generate_from_frequencies(word_counts)
return wordcloud
# 显示词云
def showWordcloud(wordcloud):
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.savefig('wordcloud.png')
plt.show()
# 输出词频前十名
def outputTop10(word_counts):
top10 = dict(list(word_counts.items())[:10])
for word, count in top10.items():
print(f'{word}: {count}')
if __name__ == "__main__":
text = readText('yourTxt.txt')
words = segmentText(text)
word_counts = countWords(words)
background_image_path = 'yourImg.jpg'
wordcloud = generateWordcloud(word_counts, background_image_path)
showWordcloud(wordcloud)
outputTop10(word_counts)
🏀三、运行结果
运行成功展示:
🏆四、知识点提示
1.采用jieba进行分词,去除长度为1的分词
2.wordcloud配合matplotlib进行词云图的绘制
3.文本路径和图片路径需要替换掉你自己的