一.实验目标
(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)
四.测试数据及结果