问题描述:现在有两篇英文电子书(含中文行),统计他们各自的单词出现次数并进行加和,结果以字典形式呈现:
{'the': 2154, 'and': 1394, 'to': 1080, 'of': 871, 'a': 861, 'his': 639, 'The': 637, 'in': 515, 'he': 461, 'with': 310, 'that': 308, 'you': 295, 'for': 280, 'A': 269, 'was': 258, 'him': 246, 'I': 234, 'had': 220, 'as': 217, 'not': 215, 'by': 196, 'on': 189, 'it': 178, 'be': 164, 'at': 153, 'from': 149, 'they': 149, 'but': 149, 'is': 144, 'her': 144, 'their': 143, 'who': 131, 'all': 121, 'one': 119, 'which': 119,}#部分结果展示
借助python强大的标准库,解决方法的实现只需要10行代码:(本文需要用到的两篇文档下载:http://pan.baidu.com/s/1pKuO7fP)
import re,collections
def get_words(file):
with open (file) as f:
words_box=[]
for line in f:
if re.match(r'[a-zA-Z0-9]*',line):#避免中文影响
words_box.extend(line.strip().split())
return collections.Counter(words_box)
print(get_nums('emma.txt')+get_nums('伊索寓言.txt'))
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
a=Counter(words)
>>> a
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2,
"you're": 1, "don't": 1, 'under': 1, 'not': 1})
Counter的实例最帅气的特性是它可以进行数学加减,开头两篇文章词频的黏合正是借助了这个功能。
>>> b
Counter({'eyes': 1, 'looking': 1, 'are': 1, 'in': 1, 'not': 1, 'you': 1,
'my': 1, 'why': 1})
>>> # Combine counts
>>> c = a + b
>>> c
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2,
'around': 2, "you're": 1, "don't": 1, 'in': 1, 'why': 1,
'looking': 1, 'are': 1, 'under': 1, 'you': 1})
import re,collections
def get_words(file):
with open (file) as f:
words_box=[]
for line in f:
if re.match(r'[a-zA-Z0-9]',line):
words_box.extend(line.strip().split())
return collections.Counter(words_box)
a=get_nums('emma.txt')+get_nums('伊索寓言.txt')
print(a.most_common(10))
打印结果如下:
[('the', 2154), ('and', 1394), ('to', 1080), ('of', 871), ('a', 861), ('his', 639), ('The', 637), ('in', 515), ('he', 461), ('with', 310)]