[python]使用Counter統計文章中出現頻率最高的單詞

利用collections中的Counter來統計一篇文章中出現次數最多的單詞:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 4/21/16

@author: [email protected]

Blog: jiezhi.github.io

Reference: https://docs.python.org/2/library/collections.html#counter-objects
"""
import re
from collections import Counter


file_path = '/Users/jiezhi/Downloads/Scenes from a Courtesan_s Life.txt'

words = re.findall(r'\w+', open(file_path).read().lower())

print Counter(words).most_common(100)



可以得到結果:

[('the', 12405), ('of', 6185), ('to', 5939), ('a', 4931), ('and', 4601), ('in', 3568), ('you', 2759), ('he', 2283), ('is', 2253), ('i', 2224), ('that', 1993), ('as', 1837), ('his', 1834), ('for', 1639), ('was', 1556), ('with', 1395), ('it', 1387), ('said', 1376), ('had', 1366), ('on', 1337), ('at', 1303), ('have', 1267), ('her', 1250), ('by', 1244), ('be', 1230), ('s', 1184), ('this', 1158), ('she', 1109), ('not', 1092), ('de', 987), ('him', 924), ('will', 915), ('my', 891), ('are', 891), ('but', 890), ('who', 890), ('which', 817), ('me', 799), ('all', 693), ('an', 691), ('lucien', 690), ('so', 673), ('your', 669), ('man', 668), ('monsieur', 651), ('no', 599), ('one', 593), ('if', 585), ('from', 583), ('what', 541), ('they', 520), ('has', 511), ('madame', 495), ('or', 481), ('were', 469), ('would', 459), ('when', 457), ('esther', 457), ('out', 450), ('do', 438), ('jacques', 434), ('collin', 428), ('we', 428), ('been', 413), ('there', 406), ('like', 396), ('can', 378), ('up', 370), ('more', 365), ('must', 358), ('two', 351), ('such', 348), ('well', 346), ('their', 340), ('some', 331), ('them', 326), ('only', 322), ('woman', 313), ('la', 313), ('into', 311), ('shall', 309), ('baron', 309), ('could', 305), ('know', 302), ('than', 294), ('see', 292), ('these', 289), ('very', 285), ('old', 281), ('thousand', 275), ('himself', 273), ('camusot', 272), ('am', 272), ('never', 270), ('now', 270), ('where', 263), ('come', 262), ('then', 260), ('made', 260), ('may', 255)]


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章