-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheml_mr.py
187 lines (176 loc) · 6.16 KB
/
eml_mr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
Author: Jaakko Lappalainen, 2013. email: [email protected]
'''
"""
Some primitives that use MR native framework from MongoDB to get some basic stats from the data. It generates data to plot histograms.
"""
# Get relative occurrence of tags inside 'tag' in 'collection'
def countTags(collection, tag):
mapper = Code("function () {"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" emit(i, 1);"
" }"
" }"
" }")
nDocs = collection.count()
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
r = {}
for doc in result.find():
r[doc['_id']] = doc['value']
return r
def countRelativeOccurrenceTags(collection, tag):
mapper = Code("function () {"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" emit(i, 1);"
" }"
" }"
" }")
nDocs = collection.count()
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
r = {}
nDocs = collection.count()
for doc in result.find():
n = float(doc['value'])/float(nDocs)*100
r[doc['_id']] = n
return r
def countTagValues(collection, tag):
mapper = Code("function () {"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" emit(this"+tag+", 1);"
" }"
" }"
" }")
reducer = Code('function (key, values) {'
' var total = 0;'
' if(key != "_id"){'
' for (var i = 0; i < values.length; i++) {'
' total += values[i];'
' }'
' }'
' return total;'
' }')
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
nDocs = collection.count()
r = {}
for doc in result.find():
n = float(doc['value'])/float(nDocs)*100
r[doc['_id']] = "{0:.1f}".format(n)+" %"
return r
# Search for 'tag' in 'module'
def searchTag(collection, module, tag):
mapper = Code("function () {"
" if(this"+module+".hasOwnProperty("+tag+")){"
" emit(this"+module+"["+tag+"], 1);"
" }"
" }")
nDocs = collection.count()
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
for doc in result.find():
n = float(doc['value'])/float(nDocs)*100
print (doc['_id']+" "+"{0:.1f}".format(n)+" %")
# Make a list with all the relevant tags inside a tag, using map-reduce
def listTags(col, tag):
mapper = Code("function () {"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" emit(i, 1);"
" }"
" }"
" }")
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = col.map_reduce(mapper, reducer, tag)
k = []
for doc in result.find():
k.append(doc['_id'].encode("utf8"))
return k
# Fields with longest text
def printLongestText(collection, tag):
mapper = Code("function () {"
" length = 0;"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" if(i.length > length){"
" length = i.length;"
" }"
" }"
" }"
" emit(i, length);"
" }")
nDocs = collection.count()
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
for doc in result.find():
print (doc['_id']+" "+"{0:.0f}".format(doc['value']))
def getTitleFromId(id):
if id != "":
return db.eml_docs.find({"_id": id})['dataset']['title']
def getAbsoluteOccurrences(collection, tag):
mapper = Code("function () {"
" for(var i in this"+tag+") {"
" if(this"+tag+".hasOwnProperty(i)){"
" emit(i, 1);"
" }"
" }"
" }")
reducer = Code("function (key, values) {"
" var total = 0;"
" for (var i = 0; i < values.length; i++) {"
" total += values[i];"
" }"
" return total;"
" }")
if tag == '':
tag = 'output'
result = collection.map_reduce(mapper, reducer, tag)
data = {}
for doc in result.find():
data[doc['_id'].encode("utf8")] = doc['value']
return data