Python制作樱花摘要和关键词
时间: 2023-12-17 10:09:09 浏览: 40
您好!感谢您的提问。要制作樱花摘要和关键词,可以使用Python中的自然语言处理库NLTK和关键词提取库RAKE。以下是一个简单的示例代码:
```python
import nltk
from nltk.corpus import stopwords
from rake_nltk import Rake
# 读取文本
with open('cherry_blossom.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分句
sentences = nltk.sent_tokenize(text)
# 分词、去除停用词
stop_words = set(stopwords.words('english'))
words = []
for sentence in sentences:
words.extend([word.lower() for word in nltk.word_tokenize(sentence) if word.lower() not in stop_words])
# 提取关键词
r = Rake()
r.extract_keywords_from_text(text)
keywords = r.get_ranked_phrases()
# 计算词频
freq_dist = nltk.FreqDist(words)
# 输出摘要
summary = freq_dist.most_common(10)
print('摘要:')
for word, frequency in summary:
print(word, end=' ')
# 输出关键词
print('\n关键词:')
for keyword in keywords[:10]:
print(keyword, end=' ')
```
这段代码会读取名为`cherry_blossom.txt`的文本文件,分句、分词、去除停用词,提取关键词,并计算词频。最后,会输出摘要和关键词,分别为出现频率最高的前10个词和RAKE提取的前10个关键词。
请注意,这段代码是一个简单示例,实际情况可能会更加复杂。例如,如果文本是中文的,需要使用中文分词工具,如jieba。同时,关键词提取方法也可以用其他的算法,如TF-IDF。
相关推荐
![](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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)