其他分享
首页 > 其他分享> > 统计水浒传出场人物

统计水浒传出场人物

作者:互联网

一.实验目标

(1)   掌握第三方库的安装和使用

(2)   掌握python内置模块的使用

(3)   了解jieba模块的使用

二.实验内容

import jieba

jieba.load_userdict('dict.txt')
stopwords = [line.strip() for line in open('stopwords.txt','r',encoding='utf-8').readlines()]
stopwords.append('')
article = open("shuihu60.txt",encoding = 'utf-8').read()
words = jieba.lcut(article)
word_freq = {}
for word in words:
    if len(word) == 1 or (word in stopwords):
        continue
    else
:
        word_freq[word] = word_freq.get(word,0)+1

freq_word = []
for word, freq in word_freq.items():
    freq_word.append((word, freq))
freq_word.sort(key = lambda x:x[1], reverse = True)
max_number = eval(input("显示前多少位高频词?"))

for word, freq in freq_word[:max_number]:
        print(word,freq)

四.测试数据及结果

 

 

 

 

 

 

 

标签:jieba,word,人物,水浒传,stopwords,出场,freq,txt,words
来源: https://www.cnblogs.com/effortday/p/14990233.html