Created
March 10, 2013 22:50
-
-
Save masayang/5130862 to your computer and use it in GitHub Desktop.
共通友達を数えるMrJob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
W, A, B, C | |
X, A, B, C, D, Z | |
Y, A, B, E | |
Z, A, B, D, E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mrjob.job import MRJob | |
class Recommendation(MRJob): | |
def mapper(self, key, line): | |
input = line.split(',') | |
user, friends = input[0], input[1:] | |
for i in range(len(friends)): | |
f1 = friends[i].strip() | |
for j in range(i+1, len(friends)): | |
f2 = friends[j].strip() | |
if f1 < f2: | |
yield(f1, f2), 1 | |
else: | |
yield(f2, f1), 1 | |
def reducer(self, key, values): | |
f1, f2 = key | |
mutual_friends_count = 0 | |
for value in values: | |
mutual_friends_count += value | |
yield (f1, f2), mutual_friends_count | |
if __name__ == '__main__': | |
Recommendation.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment