问题描述:
提供若干个需要分析的源文件,每行都是如下的形式:
journals/cl/SantoNR90:::Michele Di Santo::Libero Nigro::Wilma Russo:::Programmer-Defined Control Abstractions in Modula-2.
代表:
paper-id:::author1::author2::…. ::authorN:::title
需要计算出每个作者对应的文章标题每个词项的数量,例如:
作者Alberto Pettorossi的结果为: program:3, transformation:2, transforming:2, using:2, programs:2, logic:2.
注意:每个文档id对应与多个作者,每个作者对应多个词项。词项不包含停用词,单个字母、连字符同样不计。
源代码如下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import glob
import mincemeat
text_files=glob.glob('E:\\hw3data\\/*')
def file_contents(file_name):
f=open(file_name)
try:
return f.read()
finally:
f.close()
source=dict((file_name,file_contents(file_name))
for file_name in text_files)
# setup map and reduce functions
def mapfn(key, value):
stop_words=['all', 'herself', 'should', 'to', 'only', 'under', 'do', 'weve',
'very', 'cannot', 'werent', 'yourselves', 'him', 'did', 'these',
'she', 'havent', 'where', 'whens', 'up', 'are', 'further', 'what',
'heres', 'above', 'between', 'youll', 'we', 'here', 'hers', 'both',