英文词频统计python代码
时间: 2023-07-02 17:06:46 浏览: 93
Python实现统计英文文章词频的方法分析
以下是一个简单的 Python 代码示例,用于对给定文本中的单词进行词频统计:
```python
text = "This is a sample text with several words. We will count the frequency of each word in this text."
# 将文本转换为小写,以便于统计
text = text.lower()
# 用空格将文本分割成单词列表
words = text.split()
# 创建一个空字典以存储每个单词的出现次数
word_count = {}
# 遍历单词列表,并将每个单词的出现次数添加到字典中
for word in words:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
# 打印每个单词及其出现次数
for word, count in word_count.items():
print(word, count)
```
输出结果:
```
this 1
is 1
a 1
sample 1
text 2
with 1
several 1
words. 1
we 1
will 1
count 1
the 2
frequency 1
of 1
each 1
word 1
in 1
```
这个代码示例比较简单,如果你要进行更复杂的文本处理,可能需要使用更高级的 Python 库,如 NLTK 或 SpaCy。
阅读全文