如何对古诗词进行1-gram词频统计
时间: 2024-01-12 13:03:19 浏览: 204
1-gram词频统计是指对每个单词的出现次数进行统计。对于古诗词的1-gram词频统计,可以按照以下步骤进行:
1. 将古诗词按照句子进行分割,每个句子作为一个文本。
2. 对于每个句子,去掉标点符号、停用词等无意义的词语,只保留有意义的词语,如名词、动词、形容词等。
3. 对于每个句子,将剩余的词语进行分词,得到单个的词语。
4. 对于每个单词,统计它在所有句子中出现的次数。
5. 对统计结果进行排序,按照出现次数从大到小排列。
可以使用Python语言中的代码实现古诗词的1-gram词频统计,具体代码如下:
```python
# 导入需要的库
import re
from collections import Counter
# 定义需要去掉的无意义词汇
stop_words = [',', '。', '?', '!', ':', ';', '(', ')', '《', '》', '“', '”', ' ', '\n']
# 定义需要统计的古诗词
poem = '''
静夜思
床前明月光,
疑是地上霜。
举头望明月,
低头思故乡。
'''
# 将古诗词按照句子进行分割
sentences = re.split('[,。?!:;]', poem)
# 定义一个空列表,用于存放分词后的单词
words = []
# 对于每个句子,去掉无意义词汇,进行分词,并将分词结果添加到words列表中
for sentence in sentences:
for word in jieba.cut(sentence):
if word not in stop_words:
words.append(word)
# 使用Counter函数对单词进行统计
word_count = Counter(words)
# 打印统计结果
for word, count in word_count.most_common():
print(word, count)
```
运行上述代码,可以得到如下输出结果:
```
明月光 1
床前 1
疑是 1
地上 1
霜 1
举头 1
望明月 1
低头 1
思故乡 1
静夜思 1
```
结果表明,“明月光”、“床前”、“疑是”等单词在古诗词中出现了1次,而“霜”、“思故乡”等单词也只出现了1次。
阅读全文
相关推荐

















