python中文词频统计,去除标点
时间: 2023-11-17 16:14:36 浏览: 100
可以使用Python的re模块和collections模块来实现中文词频统计,并去除标点符号。
以下是一个示例代码:
```python
import re
from collections import Counter
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 去除标点符号
text = re.sub('[^\u4e00-\u9fa5]', '', text)
# 分词并统计词频
words = list(text)
word_count = Counter(words)
# 输出前10个出现频率最高的词语
for word, count in word_count.most_common(10):
print(word, count)
```
解释:
1. 使用`open`函数读取文本文件,以`utf-8`编码方式打开。
2. 使用正则表达式`[^\u4e00-\u9fa5]`匹配非中文字符,并使用`re.sub`将其替换为空字符串,实现去除标点符号的功能。
3. 将处理后的文本转化为字符列表,使用`collections.Counter`函数统计词频。
4. 使用`most_common`方法获取出现频率最高的前10个词语,并输出结果。
注:以上代码中的`text.txt`为要统计词频的文本文件名,需要根据实际情况进行修改。
相关问题
python 词频统计 标点符号
python词频统计是一种通过统计文本中单词出现的次数来分析单词在文本中的重要性的方法。在给定的代码中,通过使用jieba库进行分词处理,然后利用字典统计每个单词出现的次数。在统计之前,代码会对文本进行预处理,去除标点符号并将文本转换为小写。最后,代码会按照单词出现的频率进行排序,并输出每个单词和对应的出现次数。
Python哈姆雷特词频统计
以下是Python哈姆雷特词频统计的代码和解释:
```python
import string
# 读取文件内容
with open('hamlet.txt', 'r') as f:
content = f.read()
# 去除标点符号和换行符
content = content.translate(str.maketrans('', '', string.punctuation + '\n'))
# 将所有单词转换为小写
content = content.lower()
# 将文本内容按空格分割成单词列表
words = content.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)
# 输出前10个出现次数最多的单词
for i in range(10):
print(sorted_word_count[i][0], sorted_word_count[i][1])
```
代码解释:
1. 首先使用`with open()`语句读取文件内容,并将其存储在`content`变量中。
2. 使用`translate()`方法去除标点符号和换行符。
3. 使用`lower()`方法将所有单词转换为小写。
4. 使用`split()`方法将文本内容按空格分割成单词列表。
5. 使用字典`word_count`统计每个单词出现的次数。
6. 使用`sorted()`函数按照单词出现次数从大到小排序,并将结果存储在`sorted_word_count`变量中。
7. 使用`for`循环输出前10个出现次数最多的单词及其出现次数。