“诗仙”李白和“诗圣”杜甫是我国唐代著名的诗人,他们为后人留下了许多不朽 的诗歌。虽然李白和杜甫同样热爱祖国,关心国家的命运与人民的疾苦,但是他们在诗歌的 风格以及表现意象上还是有所不同的。编写程序对这两位诗人的诗歌进行词频分析,体会李 白的浪漫洒脱以及杜甫的悲悯写实。
时间: 2023-12-03 08:42:26 浏览: 35
好的,我可以帮您实现这个功能。首先,需要获取李白和杜甫的诗歌文本数据。有了数据之后,我们可以使用Python中的nltk库来进行词频分析。
以下是代码示例:
```python
import nltk
from nltk.corpus import PlaintextCorpusReader
# 读取李白的诗歌数据
li_bai_corpus = PlaintextCorpusReader('path/to/li_bai/corpus', '.*\.txt')
li_bai_words = li_bai_corpus.words()
# 读取杜甫的诗歌数据
du_fu_corpus = PlaintextCorpusReader('path/to/du_fu/corpus', '.*\.txt')
du_fu_words = du_fu_corpus.words()
# 进行词频统计
li_bai_freq = nltk.FreqDist(li_bai_words)
du_fu_freq = nltk.FreqDist(du_fu_words)
# 打印出现频率最高的前10个词语
print('李白:', li_bai_freq.most_common(10))
print('杜甫:', du_fu_freq.most_common(10))
```
其中,`path/to/li_bai/corpus`和`path/to/du_fu/corpus`需要替换为实际的诗歌数据路径。
运行以上代码后,可以得到李白和杜甫诗歌中出现频率最高的前10个词语。通过比较两位诗人的词频分析结果,可以体会到李白的浪漫洒脱以及杜甫的悲悯写实。