使用Python语言实现1、实现对英文文本文件hamlet.txt的读取; 2、实现对英文文本文件的单词抽取; 3、实现对英文文本文件中出现频次最高的前10个单词的抽取,并降序输出; 4、(选做)实现对英文文本文件中出现频次最高的前10个名词的抽取,并排序输出(可根据单词的出现频次进行排序,然后除去相关的代词、连词和形容词等);
时间: 2023-12-10 08:37:56 浏览: 125
好的,以下是实现过程:
1、读取英文文本文件hamlet.txt
```python
with open('hamlet.txt', 'r') as file:
text = file.read()
```
2、对英文文本文件的单词抽取
```python
import re
# 使用正则表达式匹配英文单词
words = re.findall(r'\b[A-Za-z]+\b', text)
# 将所有单词转换为小写
words = [word.lower() for word in words]
```
3、对英文文本文件中出现频次最高的前10个单词的抽取,并降序输出
```python
from collections import Counter
# 计算每个单词的出现次数
word_counts = Counter(words)
# 获取出现频次最高的前10个单词
top_words = word_counts.most_common(10)
# 输出结果
for word, count in top_words:
print(f'{word}: {count}')
```
4、实现对英文文本文件中出现频次最高的前10个名词的抽取,并排序输出
```python
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# 下载nltk需要的数据
nltk.download('punkt')
nltk.download('stopwords')
# 获取英文停用词
stop_words = set(stopwords.words('english'))
# 过滤掉停用词和非名词
nouns = []
for word, pos in nltk.pos_tag(words):
if pos.startswith('NN') and word not in stop_words:
nouns.append(word)
# 计算每个名词的出现次数
noun_counts = Counter(nouns)
# 获取出现频次最高的前10个名词
top_nouns = noun_counts.most_common(10)
# 输出结果
for noun, count in top_nouns:
print(f'{noun}: {count}')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)