walden


英文文本中单词统计


f = open(r’C:\Users\Administrator\Desktop\Walden.txt’,‘r’)
lines=f.readlines()
a = []
for line in lines:
tmp_list = line.split(" “)
for word in tmp_list:
a.append(word.replace(’,’,’’).replace(’.’,’’).replace(’”’,’’).replace(’-’,’’))
a

dict = {}
word_set = set(a)
for word in word_set:
count_num = a.count(word)
dict[word] = count_num
dict
sorted(dict.items(),key=lambda item:item[1],reverse = True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您完成这个任务。 首先,我们需要读取Walden文本,然后将其分词并统计词频。您可以使用Python中的nltk库来完成此任务。请确保您已经安装了nltk库。 以下是完成此任务的代码: ```python import nltk from nltk.tokenize import word_tokenize from nltk.probability import FreqDist from wordcloud import WordCloud import matplotlib.pyplot as plt # 读取文件 with open('Walden.txt', 'r') as file: text = file.read() # 分词 tokens = word_tokenize(text) # 统计词频 fdist = FreqDist(tokens) # 按频次排序 sorted_fdist = sorted(fdist.items(), key=lambda x: x[1], reverse=True) # 打印前20个高频词 print(sorted_fdist[:20]) # 制作词云 wordcloud = WordCloud(width=800, height=800, background_color='white', contour_width=5, contour_color='steelblue') wordcloud.generate_from_frequencies(fdist) plt.figure(figsize=(8, 8), facecolor=None) plt.imshow(wordcloud) plt.axis("off") plt.tight_layout(pad=0) plt.show() ``` 在这里,我们首先读取Walden.txt文件,然后使用word_tokenize函数将其分为单词。接下来,我们使用FreqDist函数计算每个单词出现的次数,并将其存储在fdist变量中。然后,我们按频率对单词进行排序,并使用sorted_fdist变量存储结果。最后,我们使用WordCloud库制作词云,并在图形中显示出来。 您可以将Walden.txt文件放在与代码相同的文件夹中,并在代码中指定文件名。运行代码后,您将获得一个包含前20个高频词的列表,并且还会显示一个词云。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值