You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

"""
生成基于弹幕数据的词云图
"""
import pandas as pd
import numpy as np
import wordcloud
from matplotlib.image import imread
import jieba
def blue_color_func(_random_state=None, **_kwargs):
"""
Generates a color in the HSL format with a random lightness value.
Parameters:
_random_state (None or int): Used to seed the random number generator.
**_kwargs: Additional arguments (ignored in this function).
Returns:
str: A string representing the color in HSL format.
"""
return f"hsl(210, 100%, {np.random.randint(50, 90)}%)"
def wordcloud_generation(danmu_data):
"""生成词云图并保存"""
dm_list = danmu_data['danmu'].dropna().astype(str).tolist()
dm_string = ' '.join(dm_list)
dmreal_string = ' '.join(jieba.lcut(dm_string))
img = imread("E:/Crawler/output/OIP.jpg")
my_stopwords = {'', '', '', '', '', '', '', '', '', '', '', '', '', '', ''}
wc = wordcloud.WordCloud(
stopwords=my_stopwords,
width=1920,
height=1200,
background_color='white',
font_path='msyhl.ttc',
mask=img,
max_words=100,
color_func=blue_color_func,
).generate(dmreal_string)
wc.to_file('E:/Crawler/output/danmu_dwordcloud.png')
def main():
"""加载数据并生成词云"""
dm = pd.read_excel('E:/Crawler/output/Top8_Danmu.xlsx', sheet_name='Sheet1')
wordcloud_generation(dm)
if __name__ == '__main__':
main()