词频统计 python 代码
时间: 2024-10-29 09:05:04 浏览: 66
在Python中,词频统计是一种常见的文本处理任务,通常用于分析文本数据中各个单词的出现频率。我们可以使用内置的collections模坓中的Counter类来快速完成这项工作。以下是一个简单的示例:
```python
from collections import Counter
def word_frequency(text):
# 将文本转换为小写并分割成单词列表
words = text.lower().split()
# 使用Counter计算每个单词的出现次数
frequency = Counter(words)
return frequency
# 示例文本
text = "这是一段示例文本,它包含了一些重复的词语,例如 示例 和 文本"
word_counts = word_frequency(text)
# 打印结果
for word, count in word_counts.most_common(): # .most_common()返回按频率降序排列的单词及其计数
print(f"{word}: {count}")
相关问题
词频统计python代码
以下是一个简单的Python代码用于词频统计:
```python
text = "This is a sample text with several words. This is another sample text with some different words."
# 将所有大写字母转换为小写字母,以便词频统计更加准确
text = text.lower()
# 将文本分割成单词
words = text.split()
# 创建一个空字典来保存每个单词的出现次数
word_count = {}
# 遍历每个单词并统计它们的出现次数
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按照单词出现次数从高到低对字典进行排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 打印每个单词及其出现次数
for word, count in sorted_word_count:
print(word, count)
```
输出:
```
sample 2
is 2
this 2
with 2
words. 1
several 1
another 1
text 1
some 1
different 1
```
英文词频统计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。
阅读全文