你好,游客 登录 注册 搜索
背景:
阅读新闻

利用mincemeat编写简单的MapReduce程序

[日期:2013-04-15] 来源:Linux社区  作者:zdw12242 [字体:大 中 小]

提供若干个需要分析的源文件,每行都是如下的形式:

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',
            'my', 'ill', 'against', 'arent', 'thats', 'from', 'would', 'been',
            'whos', 'whom', 'themselves', 'until', 'more', 'an', 'those', 'me',
            'myself', 'theyve', 'this', 'while', 'theirs', 'didnt', 'theres',
            'ive', 'is', 'it', 'cant', 'itself', 'im', 'in', 'id', 'if', 'same',
            'how', 'shouldnt', 'after', 'such', 'wheres', 'hows', 'off', 'i',
            'youre', 'well', 'so', 'the', 'yours', 'being', 'over', 'isnt',
            'through', 'during', 'hell', 'its', 'before', 'wed', 'had', 'lets',
            'has', 'ought', 'then', 'them', 'they', 'not', 'nor', 'wont',
            'theyre', 'each', 'shed', 'because', 'doing', 'some', 'shes',
            'our', 'ourselves', 'out', 'for', 'does', 'be', 'by', 'on',
            'about', 'wouldnt', 'of', 'could', 'youve', 'or', 'own', 'whats',
            'dont', 'into', 'youd', 'yourself', 'down', 'doesnt', 'theyd',
            'couldnt', 'your', 'her', 'hes', 'there', 'hed', 'their', 'too',
            'was', 'himself', 'that', 'but', 'hadnt', 'shant', 'with', 'than',
            'he', 'whys', 'below', 'were', 'and', 'his', 'wasnt', 'am', 'few',
            'mustnt', 'as', 'shell', 'at', 'have', 'any', 'again', 'hasnt',
            'theyll', 'no', 'when','other', 'which', 'you', 'who', 'most',
            'ours ', 'why', 'having', 'once','a','-','.',',']
    for line in value.splitlines():
        word=line.split(':::')
        authors=word[1].split('::')
        title=word[2]
        for author in authors:
            for term in title.split():
                if term not in stop_words:
                    if term.isalnum():
                        yield author,term.lower()
                    elif len(term)>1:
                        temp=''
                        for ichar in term:
                            if ichar.isalpha() or ichar.isdigit():
                                temp+=ichar
                            elif ichar=='-':
                                temp+=' '
                        yield author,temp.lower()

def reducefn(key, value):
    terms = value
    result={}
    for term in terms:
        if term in result:
            result[term]+=1
        else:
            result[term]=1
    return result

# start the server

s = mincemeat.Server()
s.datasource = source
s.mapfn = mapfn
s.reducefn = reducefn

results = s.run_server(password="changeme")
#print results

result_file=open('hw3_result','w')
for result in results:
    result_file.write(result+' : ')
    for term in results[result]:
        result_file.write(term+':'+str(results[result][term])+',')
    result_file.write('\n')
result_file.close()

mapfn为Map部分,读入文档文件,分析出(作者,词项),并传递给Reduce部分。reducefn为Reduce,计算出作者的所有词项频次。

 

课程中运行程序的截图如下:

 

附件:

需要处理的源文档

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2013年资料/4月/15日/利用mincemeat编写简单的MapReduce程序

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款