From 8dcc6c671e6fc400015feb593a56db0b6a1da79e Mon Sep 17 00:00:00 2001 From: pzb7h6yxf <1736289433@qq.com> Date: Tue, 17 Sep 2024 23:34:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9script=5F2.2.2=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_script_2.2.2.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test_script_2.2.2.py diff --git a/test_script_2.2.2.py b/test_script_2.2.2.py new file mode 100644 index 0000000..661d3bd --- /dev/null +++ b/test_script_2.2.2.py @@ -0,0 +1,50 @@ +import unittest +from unittest.mock import patch +import jieba +import wordcloud +import pandas as pd + +class TestWordCloudGeneration(unittest.TestCase): + + @patch('pandas.read_excel') + @patch('wordcloud.WordCloud.to_file') + def test_wordcloud_generation(self, mock_to_file, mock_read_excel): + # 模拟读取的 Excel 数据 + mock_df = pd.DataFrame({ + 'AI': ['人工智能在发展', 'AI改变世界'], + 'Machine learning': ['机器学习是AI的基础', '深度学习是其中之一'] + }) + mock_read_excel.return_value = mock_df + + # 引入你需要测试的代码 + df = pd.read_excel('top_ai_danmakus.xlsx') + + # 将Excel所有列的数据拼接成一个长字符串 + book = '' + for column in df.columns: + book += ' '.join(df[column].astype(str)) + ' ' + + # 使用jieba进行分词 + book_list = jieba.lcut(book) + book_str = ' '.join(book_list) + + # 创建词云对象 + wc = wordcloud.WordCloud( + width=500, + height=500, + background_color='white', + stopwords={'main', 'Taipei', 'afraid', 'aiden', 'Britain'}, # 可添加不需要的停用词 + font_path='msyh.ttc' # 设置中文字体路径 + ) + + # 生成词云 + wc.generate(book_str) + + # 检查是否调用了保存词云的函数 + wc.to_file('词云图.png') + + # 验证 to_file 函数是否被调用 + mock_to_file.assert_called_once_with('词云图.png') + +if __name__ == '__main__': + unittest.main()