统计水浒传出场人物

一.实验目标

(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)

四.测试数据及结果

 统计水浒传出场人物

 

 统计水浒传出场人物

 

 

 

 

上一篇:【马上NLP】jieba结巴分词工具常用功能详细介绍 笔记


下一篇:python基础篇-jieba库的使用