使用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 Counter
word_count = Counter(words)
for word, count in word_count.most_common():
print(word, count)
完整的代碼如下:
import re
from collections import Counter
with 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"替換為實際的文本文件路徑。