统计中文文章词频,并去掉一些无用的词语,并按照词频高低进行排序,最后输出
时间: 2023-10-10 11:12:54 浏览: 39
好的,我可以为您提供一个简单的中文文章词频统计的程序,并去掉一些常用的无意义词语,然后按照词频高低进行排序。
以下是一个示例程序:
```python
import jieba
from collections import Counter
# 读取文章
with open('article.txt', 'r', encoding='utf-8') as f:
article = f.read()
# 分词
words = jieba.lcut(article)
# 去掉无用词语
stop_words = ['的', '了', '是', '在', '和', '有', '不', '也', '等', '为', '就', '这', '个']
words = [word for word in words if word not in stop_words]
# 统计词频
word_count = Counter(words)
# 按照词频高低排序
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for word, count in sorted_words:
print(f'{word}: {count}')
```
在上面的程序中,我们使用了 `jieba` 库来进行中文分词,然后使用 `Counter` 类来统计词频,并使用 `sorted` 函数按照词频高低进行排序。在分词后,我们还去掉了一些常用的无意义词语,如“的”、“了”、“是”等。
您只需要将上述程序中的 `article.txt` 替换为您要统计词频的文章路径即可。