python怎么对文本进行词频统计

   2025-02-13 2410
核心提示:使用Python对文本进行词频统计可以使用下面的步骤:打开文本文件并读取文本内容。with open(text.txt, r) as file:text = file.r

使用Python对文本进行词频统计可以使用下面的步骤:

打开文本文件并读取文本内容。
with open("text.txt", "r") as file:    text = file.read()
对文本进行分词。
import re# 去除标点符号和空白字符text = re.sub(r'[^\w\s]', '', text)# 将文本拆分为单词列表words = text.split()
统计每个单词的出现次数。
from collections import Counterword_count = Counter(words)
排序并输出词频结果。
for word, count in word_count.most_common():    print(word, count)

完整的代码如下:

import refrom collections import Counterwith open("text.txt", "r") as file:    text = file.read()text = re.sub(r'[^\w\s]', '', text)words = text.split()word_count = Counter(words)for word, count in word_count.most_common():    print(word, count)

请确保将代码中的"text.txt"替换为实际的文本文件路径。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言