要实现这个功能,可以按照以下步骤进行操作:

  1. 下载哈工大停用词表(stopwords.txt),保存在本地。

  2. 导入所需的库,包括jieba用于中文分词,wordcloud用于生成词云图。

  3. 读取评论内容,并进行分词。

  4. 加载停用词词典。

  5. 根据停用词词典,过滤掉干扰词。

  6. 统计词频,并生成词云图。

下面是完整的Python代码示例:

import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 读取评论内容
comment = "这个产品真的很好用啊,效果很棒的了。非常喜欢!"
# 进行中文分词
seg_list = jieba.cut(comment, cut_all=False)

# 加载停用词词典
stopwords_path = "stopwords.txt"
stopwords = set()
with open(stopwords_path, 'r', encoding='utf-8') as f:
    for line in f.readlines():
        stopwords.add(line.strip())

# 过滤掉干扰词
filtered_words = [word for word in seg_list if word not in stopwords]

# 统计词频
word_count = {}
for word in filtered_words:
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1

# 生成词云图
wordcloud = WordCloud(font_path="simhei.ttf", background_color="white").generate_from_frequencies(word_count)

# 显示词云图
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

在代码中,我们使用jieba.cut函数对评论内容进行分词,然后通过遍历分词结果,过滤掉停用词,统计词频。最后使用WordCloud类生成词云图,并使用matplotlib.pyplot库显示词云图。

注意:需要提前安装jiebawordcloud库,并将停用词词典(stopwords.txt)放在正确的路径下。另外,为了能够正确显示中文字符,需要下载并指定一个中文字体(如"simsun.ttc"或"simhei.ttf")。

运行代码后,会显示生成的词云图窗口

由于评论内容中存在很多啊、的、了等无意义的干扰词影响高频词的提取因此采用哈工大停用词表作为停用词词典对干扰词进行屏蔽并绘制词云图请用python代码实现

原文地址: http://www.cveoy.top/t/topic/iNrx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录