python之红楼梦词频统计并生成图云

一共三个文件

  • Hlm.py
  • stop_words.txt
  • 红楼梦.txt

(tips: txt文件已放在文章最后)

废话不多说,直接上效果图和代码:

  1. 效果图python之红楼梦词频统计并生成图云_第1张图片
  2. Hlm.py 代码部分:
import matplotlib.pyplot as plt
import jieba
import wordcloud
import matplotlib

matplotlib.rcParams['font.sans-serif'] = ['simple'] #设置绘图字体
def wordFreq(filepath,text,topn):
    #jieba分词库分词
    words = jieba.lcut(text.strip())
    counts = {}
    #列表生成式获取停用词
    stopwords = [line.strip() for line in open('stop_words.txt', 'r', encoding='utf-8').readlines()] 
    word_clear = [] #用于生成词云的词语列表,避免重复分词,节约运行时间
    
    #统计词频
    for word in words:
        if(len(word) == 1):
            continue
        elif word not in stopwords:
            if word == "凤姐儿":
                word = "凤姐"
            elif word == "林黛玉" or word == "林妹妹" or word == "黛玉笑":
                word == "黛玉"
            elif word == "宝二爷":
                word == "宝玉"
            elif word == "袭人道":
                word == "袭人"
            word_clear.append(word)
            counts[word] = counts.get(word, 0) + 1

    items = list(counts.items())   
    items.sort(key = lambda x:x[1], reverse=True)
    for i in range(topn):
        word, count = items[i]
        print(f"{word}:{count}")
    return word_clear

def gen_cloudword(txt):
    wcloud=wordcloud.WordCloud(font_path = r'C:\Windows\Fonts\simhei.ttf', width=1000,  max_words = 100, height = 860, margin = 2).generate(txt)
    wcloud.to_file("红楼梦cloud_star.png")      #  保存图片
    #显示词云图片 
    plt.imshow(wcloud) 
    plt.axis('off') 
    plt.show()

text = open('红楼梦.txt', "r",encoding='utf-8').read() 
words_clear = wordFreq('红楼梦.txt',text,10)

gen_cloudword(' '.join(words_clear)) 

txt文本文件
链接:https://pan.baidu.com/s/1ZPvhT0rJddGPS4YUoYzzjQ?pwd=1234
提取码:1234

你可能感兴趣的:(python,python,list)