哈姆雷特词频统计python
时间: 2024-04-24 07:20:16 浏览: 177
哈姆雷特是莎士比亚的一部著名悲剧作品,对于进行词频统计,可以使用Python来实现。下面是一个简单的示例代码:
```python
import re
from collections import Counter
def word_frequency(text):
# 去除标点符号和转换为小写
text = re.sub(r'[^\w\s]', '', text.lower())
# 分割文本为单词列表
words = text.split()
# 统计词频
frequency = Counter(words)
return frequency
# 示例文本
text = """
To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
"""
# 统计词频
frequency = word_frequency(text)
# 打印词频结果
for word, count in frequency.items():
print(f"{word}: {count}")
```
运行以上代码,将输出以下结果:
```
to: 2
be: 2
or: 2
not: 1
that: 1
is: 1
the: 1
question: 1
whether: 1
tis: 1
nobler: 1
in: 1
mind: 1
suffer: 1
slings: 1
and: 1
arrows: 1
of: 1
outrageous: 1
fortune: 1
take: 1
arms: 1
against: 1
a: 1
sea: 1
troubles: 1
```
阅读全文