-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
executable file
·327 lines (296 loc) · 7.87 KB
/
code.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/python
import psyco
psyco.full()
cocount = {} # co-occurrence repo pair count
sortedco = {} # sorted co-occurences
user = {} # users info
repo = {} # repo info
popular = [] # popularity
own = {} # repos owned by named user
lang = {} # repos in specific language
class User(object):
def __init__(self):
self.watch = [] # repos s/he watches
class Repo(object):
def __init__(self):
self.wcount = 0
self.owner = None
self.parent = None
self.lang = []
self.watcher = [] # who watches this repo
def loaddata():
'''load data.txt'''
print 'loading data.txt...'
f = open('data.txt')
for x in f:
u, r = x.strip().split(':')
if r not in repo:
repo[r] = Repo()
repo[r].wcount += 1
repo[r].watcher.append(u)
if u not in user:
user[u] = User()
user[u].watch.append(r)
def loadrepo():
'''load repos.txt'''
print 'loading repos.txt...'
f = open('repos.txt')
for x in f:
r, info = x.strip().split(':')
if r not in repo:
repo[r] = Repo()
owner, s = info.split('/')
repo[r].owner = owner
if owner not in own:
own[owner] = []
own[owner].append(r)
a = s.split(',')
if len(a) == 3:
repo[r].parent = a[2]
def loadlang():
'''load lang.txt'''
print 'loading lang.txt...'
f = open('lang.txt')
for x in f:
r, info = x.strip().split(':')
if r not in repo:
repo[r] = Repo()
v = repo[r].lang
for s in info.split(','):
a, b = s.split(';')
v.append((a, int(b)))
if a not in lang:
lang[a] = []
lang[a].append(r)
def getco():
'''get co-occurence counting'''
print 'build co-occurence count...'
def add(a, b):
'''helper function, add (a,b) pair to co-coccurence counting'''
cocount.setdefault(a, {})
cocount[a].setdefault(b, 0)
cocount[a][b] += 1
for u in user:
r = user[u].watch
print ' for user ' + u + ' with ' + str(len(r)) + ' repos'
for i in xrange(len(r)):
a = r[i]
for j in xrange(i + 1, len(r)):
b = r[j]
add(a, b)
add(b, a)
def sortco():
'''sort co-occurences'''
print 'sort out co-occurence...'
for a in cocount:
s = cocount[a]
b = [x for x in s]
b.sort(key = lambda x: -s[x])
sortedco[a] = b
def dumpco():
'''dump sorted co-occurence to disk file'''
out = open('sortedco.txt', 'w')
for r in sortedco:
out.write(r + ':')
s = sortedco[r]
for x in xrange(len(s) - 1):
out.write(s[x] + ',')
if len(s) > 0:
out.write(s[-1] + '\n')
else:
out.write('\n')
def loadco():
'''load sorted co-occurences from dumped file'''
print 'loading sorted co-occurences info...'
f = open('sortedco.txt')
for x in f:
u, rs = x.strip().split(':')
sortedco[u] = rs.split(',')
def getpop():
'''get popularity'''
print 'get repos popularity...'
global popular
popular = [x for x in repo]
popular.sort(key = lambda a: -repo[a].wcount)
# sort each lang by popularity
for x in lang:
lang[x].sort(key = lambda a: -repo[a].wcount)
def bytree(u, result):
'''get unwatched repos from most popular family tree'''
got = len(result)
if got >= 10:
return
rs = user[u].watch
w = {} # weight of watched family
for r in rs:
if r not in repo:
continue
# walk up the tree
x = r
while x != None:
if x not in w:
w[x] = 1
else:
w[x] += 1
x = repo[x].parent
candi = [x for x in w]
candi.sort(key = lambda a: -w[a])
for x in candi:
if x in rs or x in result: # already watched
continue
result.append(x)
got += 1
if got >= 10:
break
def bylang(u, result):
'''get unwatched repos by his/her language preference'''
got = len(result)
if got >= 10:
return
rs = user[u].watch
prefer = {}
for r in rs:
for a, b in repo[r].lang:
if a not in prefer:
prefer[a] = 0
prefer[a] += b # weighted by code length
candi = [x for x in prefer]
candi.sort(key = lambda a: -prefer[a])
for x in candi:
for a in lang[x]:
if a in rs or a in result:
continue
result.append(a)
got += 1
if got >= 10:
return
def byown(u, result):
'''get unwatched repos from your watched repos' owners'''
got = len(result)
if got >= 10:
return
rs = user[u].watch
who = set([]) # watched repos' owners (by user name)
for r in rs:
if r not in repo:
continue
owner = repo[r].owner
if owner == None:
continue
if owner not in who:
who.add(owner)
candi = [y for x in who for y in own[x]]
candi.sort(key = lambda a: -repo[a].wcount)
for x in candi:
if x in rs or x in result: # already watched
continue
result.append(x)
got += 1
if got >= 10:
break
def bywatch(u, result):
'''get unwatched repos from your watched repos' watchers'''
got = len(result)
if got >= 10:
return
print u, got, 'hit bywatch'
rs = user[u].watch
who = set([]) # watched repos' watchers (by uid)
for r in rs:
if r not in repo:
continue
for w in repo[r].watcher:
if w not in who:
who.add(w)
candi = [y for x in who for y in user[x].watch]
candi.sort(key = lambda a: -repo[a].wcount)
for x in candi:
if x in rs or x in result: # already watched
continue
result.append(x)
got += 1
if got >= 10:
break
def byco(u, result):
'''choose top most co-occurences'''
got = len(result)
if got >= 10:
return
rs = user[u].watch
cur = [0] * len(rs)
def biggest():
'''return next biggest or -1 if there's no one.'''
# kinda like mergesort
best = -1
idx = -1
for x in xrange(len(rs)):
r = rs[x]
if r not in sortedco:
continue
s = sortedco[r]
a = -1 # init
while cur[x] < len(s):
a = s[cur[x]]
if a not in rs and a not in result: # got one
break
cur[x] += 1
if a > best:
best = a
idx = x
if idx != -1:
cur[idx] += 1
return best
while got < 10:
x = biggest()
if x == -1:
break
result.append(x)
got += 1
def bypop(u, result):
got = len(result)
if got >= 10:
return
rs = user[u].watch
for x in popular:
if x in rs or x in result: # watched already
continue
result.append(x)
got += 1
if got >= 10:
break
def doit():
'''lets do it'''
print 'try guessing...'
f = open('test.txt')
out = open('results.txt', 'w')
for x in f:
u = x.strip()
#print ' for user ' + u + '...'
if u not in user:
user[u] = User()
result = []
# rules in order
bytree(u, result)
byco(u, result)
byown(u, result)
bywatch(u, result)
bylang(u, result)
bypop(u, result)
# write
out.write(u + ':')
for x in xrange(10):
c = ','
if x == 9:
c = '\n'
out.write(result[x] + c)
loaddata()
loadrepo()
loadlang()
getpop()
'''
getco()
sortco()
dumpco()
'''
loadco()
doit()