Python中文文本分析词频代码
时间: 2023-11-05 09:18:35 浏览: 37
以下是一个简单的Python中文文本分析词频的代码示例:
```python
import jieba
from collections import Counter
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用jieba分词
words = jieba.cut(text)
# 统计词频
word_counts = Counter(words)
# 打印出现频率最高的前10个词语
for word, count in word_counts.most_common(10):
print(word, count)
```
这个代码示例中,我们使用了jieba分词库来将文本分成一个个词语,并使用Counter类统计每个词语出现的次数。最后,我们打印出现频率最高的前10个词语,可以根据需要调整打印数量。
相关问题
Python文件 文本词频统计代码
以下是Python实现文本词频统计的代码:
```python
import re
# 定义函数,传入文件名和要统计的单词个数
def word_count(file_name, num):
# 打开文件
with open(file_name, 'r') as f:
# 读取文件内容
content = f.read()
# 将单词转换为小写,并去掉标点符号
words = re.findall(r'\b[a-z]+\b', content.lower())
# 统计单词出现的次数
word_dict = {}
for word in words:
if word not in word_dict:
word_dict[word] = 1
else:
word_dict[word] += 1
# 排序并输出结果
sorted_word_dict = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
for i in range(num):
print(sorted_word_dict[i][0], sorted_word_dict[i][1])
# 调用函数进行测试
word_count('test.txt', 10)
```
代码解释:
1. `re.findall(r'\b[a-z]+\b', content.lower())` 用正则表达式匹配文件内容中的单词,`\b` 表示单词的边界,`[a-z]+` 表示一个或多个小写字母。
2. `word_dict` 用字典来保存单词出现的次数,如果单词不存在则添加到字典中,否则将出现次数加一。
3. `sorted_word_dict` 将字典按照值进行排序,`key=lambda x: x[1]` 表示按照值进行排序,`reverse=True` 表示降序排列。
4. `for i in range(num)` 只输出前面 num 个单词和出现次数。
python 文本词频统计代码
以下是 Python 实现文本词频统计的代码示例:
```python
import re # 导入正则表达式模块
# 定义一个函数,用于统计文本中每个单词出现的次数
def word_count(text):
# 将文本中的标点符号、数字、空格等替换为空格
text = re.sub(r'[^\w\s]',' ',text)
# 将文本中的大写字母转换为小写字母
text = text.lower()
# 将文本中的换行符替换为空格
text = text.replace('\n', ' ')
# 将文本中的多个空格替换为一个空格
text = re.sub('\s+', ' ', text)
# 将文本中的单词按照空格进行分割
words = text.split(' ')
# 定义一个字典,用于存储单词出现的次数
word_count = {}
# 遍历单词列表,统计每个单词出现的次数
for word in words:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
# 返回单词出现次数的字典
return word_count
# 测试函数
text = 'The quick brown fox jumps over the lazy dog. The dog slept over the verandah.'
print(word_count(text))
```
输出结果如下:
```
{'the': 3, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 2, 'lazy': 1, 'dog': 2, 'slept': 1, 'verandah': 1, '': 1}
```
其中,字典中的键为单词,值为单词出现的次数。