3.《哈姆雷特》是莎士比亚的一部经典悲剧作品。请编写程序统计hamlet.txt 中出现频率最 高的前10个单词,并将结果用文件名"hamlet 词频txt”保存在同目录下。中
时间: 2023-06-14 21:06:51 浏览: 177
以下是 Python 代码实现:
```python
# 打开文件并读取文本内容
with open('hamlet.txt', 'r') as f:
text = f.read()
# 将文本转换为小写并分割成单词列表
words = text.lower().split()
# 统计每个单词的出现次数
word_count = {}
for word in words:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
# 按照单词出现次数排序,取出前10个单词
top_words = sorted(word_count.items(), key=lambda x:x[1], reverse=True)[:10]
# 将结果保存到文件
with open('hamlet词频.txt', 'w') as f:
for word, count in top_words:
f.write(f'{word}: {count}\n')
```
注意,这里只是简单地将文本按照空格分割成单词,因此可能存在一些标点符号或特殊字符被误认为是单词的情况。如果需要更精确的处理,可以使用正则表达式或其他文本处理工具。
相关问题
《哈姆雷特》是啥事毕业的一部经典悲剧作品,该作品对应的hamlet.txt,编写程序统计hamlet.txt中出现频率最高的前10个单词,并将结果用文件名"hamlet_词频统计.txt"保存,最后用词云图展示。编写程序的参考步骤如下: (1)读取hamlet.txt (2)对文本进行预处理,将文本中的字母全变为小写,然后去除标点符号等特殊符号,例如 " , ? - :\"等等 (3)将文本分词,统计单词出现的频率 (4)排除掉大多数冠词、代词、连接词等词语,例如 the and to of a be等 (5)按照单词出现的频率的降序排列,输出前10个单词机器出现的频率,将结果写入文件hamlet_词频统计.txt中 (6)用词云图展示词频。
好的,我已经明白你的问题了。下面是Python代码实现上述步骤,需要安装jieba和wordcloud库:
```python
import jieba
import re
from collections import Counter
from wordcloud import WordCloud
# 读取文件hamlet.txt
with open('hamlet.txt', 'r', encoding='UTF-8') as f:
text = f.read()
# 将文本中的字母全变为小写,然后去除标点符号等特殊符号
text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
# 分词并统计单词出现频率
words = jieba.lcut(text)
exclude_words = ['the', 'and', 'to', 'of', 'a', 'be'] # 排除掉一些常见词
words = [word for word in words if word not in exclude_words]
word_counts = Counter(words)
# 按照单词出现的频率的降序排列,输出前10个单词及出现频率
word_freqs = word_counts.most_common(10)
with open('hamlet_词频统计.txt', 'w', encoding='UTF-8') as f:
f.write('单词\t\t频率\n')
for word, freq in word_freqs:
f.write(f'{word}\t\t{freq}\n')
# 用词云图展示词频
wordcloud = WordCloud(font_path='simhei.ttf', width=800, height=600, background_color='white')
wordcloud.generate_from_frequencies(word_counts)
wordcloud.to_file('hamlet_词云图.png')
```
运行上述代码,会生成一个名为"hamlet_词频统计.txt"的文件,里面记录了出现频率最高的前10个单词及其出现次数;同时还会生成一个名为"hamlet_词云图.png"的文件,展示了单词出现频率的词云图。
编写程序统计hamlet.txt中出现频率最高的前10个单词,并将结果用文件名hamlet_cipin.txt保存在同目录下。 要求: the 、and 、to 、 of、a 、be、 it、 is、 not、 but此类单词不计入其中
以下是Python代码实现:
```python
import re
# 定义要排除的单词
exclude_words = ["the", "and", "to", "of", "a", "be", "it", "is", "not", "but"]
# 读取文本文件并统计单词出现次数
word_count = {}
with open("hamlet.txt", "r") as f:
for line in f:
words = re.findall(r'\b\w+\b', line.lower())
for word in words:
if word not in exclude_words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按照单词出现次数排序并取前10个
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:10]
# 将结果写入文件
with open("hamlet_cipin.txt", "w") as f:
for word, count in sorted_word_count:
f.write(word + " " + str(count) + "\n")
```
在上述代码中,我们首先定义了要排除的单词列表。然后读取文本文件,并使用正则表达式找出所有单词。对于每个单词,如果它不在排除列表中,则将其出现次数加入到一个字典中。最后,我们使用`sorted`函数对字典进行排序,取前10个并将结果写入文件。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)