python实现中文词频统计
时间: 2023-07-31 19:10:21 浏览: 106
要实现中文词频统计,可以使用Python中的jieba库和collections库。
首先需要安装jieba库,可以使用以下命令进行安装:
```
pip install jieba
```
接下来,我们可以编写以下代码来进行中文文本词频统计:
```python
import jieba
from collections import Counter
# 读取文本文件
with open("text.txt", "r", encoding="utf-8") as f:
text = f.read()
# 分词
words = jieba.cut(text)
# 统计词频
word_counts = Counter(words)
# 输出前10个高频词
for word, count in word_counts.most_common(10):
print(word, count)
```
其中,text.txt是需要统计词频的文本文件,可以根据实际情况进行修改。执行以上代码后,即可输出前10个高频词及其出现次数。
相关问题
python 实现中文词频统计 源代码
当然可以,以下是一个简单的 Python 代码:
```
import jieba
from collections import Counter
# 将文本读入内存
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用 jieba 分词
words = list(jieba.cut(text))
# 统计词频并排序
freq_dict = Counter(words)
sorted_freq_dict = sorted(freq_dict.items(), key=lambda x: x[1], reverse=True)
# 输出前 10 个词及其出现次数
for word, freq in sorted_freq_dict[:10]:
print(word, freq)
```
注意,这里使用了 jieba 库进行中文分词。你可以将上面的代码保存为一个.py 文件并在命令行中运行,前提是已经安装了 jieba 库。
给我一个用python实现中文词频统计的代码,代码包括去除停用词,留用词和替换合并词等指令
这是一段python代码,可以用来统计中文词频,并去除停用词、留用词以及替换合并词:text = '''中国是一个伟大的国家,它的历史悠久,文化灿烂。它的人民勤劳、勇敢,热爱和平。'''# 定义停用词
stopwords = ['是', '的', '它']# 定义留用词
remainwords = ['伟大', '历史', '文化', '人民', '勤劳', '勇敢', '热爱', '和平']# 定义替换词
replace_words = {'中国': '祖国'}# 分词
words = jieba.lcut(text)# 统计词频
word_dict = {}
for word in words:
if word in stopwords:
continue
if word in remainwords:
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
if word in replace_words:
if replace_words[word] in word_dict:
word_dict[replace_words[word]] += 1
else:
word_dict[replace_words[word]] = 1# 打印统计结果
for word in word_dict:
print(word, word_dict[word])
阅读全文
相关推荐













