我正在使用一个名为肉馅.py. 它包含一个map函数和reduce函数。首先,我要告诉你我要做什么。我正在上一个关于bigdata的coursera课程,其中有一个编程任务。问题是,有成百上千个文件包含paperid:::author1::author2::author3:::papertitle格式的数据
我们必须浏览所有的档案,为某个特定的作者提供他所使用的最大限度的词语。所以我写了下面的代码。在
import re
import glob
import mincemeat
from collections import Counter
text_files = glob.glob('test/*')
def file_contents(file_name):
f = open(file_name)
try:
return f.read()
finally:
f.close()
datasource = dict((file_name, file_contents(file_name)) for file_name in text_files)
def mapfn(key, value):
for line in value.splitlines():
wordsinsentence = line.split(":::")
authors = wordsinsentence[1].split("::")
# print authors
words = str(wordsinsentence[2])
words = re.sub(r'([^\s\w-])+', '', words)
# re.sub(r'[^a-zA-Z0-9: ]', '', words)
words = words.split(" ")
for author in authors:
for word in words:
word = word.replace("-"," ")
word = word.lower()
yield author, word
def reducefn(key, value):
return Counter(value)
s = mincemeat.Server()
s.datasource = datasource
s.mapfn = mapfn
s.reducefn = reducefn
results = s.run_server(password="changeme")
# print results
i = open('outfile','w')
i.write(str(results))
i.close()
我现在的问题是,reduce函数必须接收authorname和他在标题中使用的所有单词,对于所有作者。所以我期望输出
^{pr2}$但我得到的是
authorname: (authorname, Counter({'word1': countofword1,'word2':countofword2}))
有人能告诉我为什么会这样吗?我不需要帮助来解决这个问题,我需要帮助来知道为什么会这样!在
我看到你的代码运行正常。输出类似于{authorname:Counter({'word1':countofword1,'word2':countofword2,'word3':countofword3,…})。在
那就是说。删除此处的代码,因为它违反了Coursera荣誉准则。在
在计数器之前检查reducefn中的值数据结构。在
相关问题 更多 >
编程相关推荐