-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhybrid.py
442 lines (371 loc) · 12.6 KB
/
hybrid.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import implementation
import collections
import time
from random import randint
import copy
import random
import bisect
import sys
#PRs:
def general_PRs(d,p):
"""Document->Person->int
Generalized version of implementation.PRs
"""
k=0
if (p.questionaire['works_in_industry'] and d.keywords['industry']):
k+=5
if (p.questionaire['environmentalist'] and d.keywords['ocean']):
k+=4
if (p.questionaire['economist'] and d.keywords['economics']):
k+=5
if (p.questionaire['30orOlder'] and d.keywords['foradults']):
k+=4
if (p.questionaire['likesmath'] and d.keywords['math']):
k+=5
if (p.questionaire['hatesmath'] and d.keywords['math']):
k-=7
if (p.questionaire['SellsOil'] and d.keywords['excessoil']):
k+=5
if (p.questionaire['lovesanimals'] and d.keywords['animals']):
k+=4
if (p.questionaire['isDoctor'] and d.keywords['medical']):
k+=5
if (not p.questionaire['works_in_industry'] and d.keywords['industry']):
k-=2
if (not p.questionaire['environmentalist'] and d.keywords['ocean']):
k-=2
if (not p.questionaire['economist'] and d.keywords['economics']):
k-=2
if (not p.questionaire['30orOlder'] and d.keywords['foradults']):
k-=1
if (not p.questionaire['likesmath'] and d.keywords['math']):
k-=2
if (not p.questionaire['SellsOil'] and d.keywords['excessoil']):
k-=1
if (not p.questionaire['lovesanimals'] and d.keywords['animals']):
k-=1
if (not p.questionaire['isDoctor'] and d.keywords['medical']):
k-=2
return k
#Brute force
def general_brute_force(K,docs,p):
D={}
for d in docs:
D[d.title]=general_PRs(d,p)
return [find_doc_in_list(docs,a)
for a in sorted(D, key=D.get, reverse=True)[:K]]
def general_brute_force_iter(I,K,docs,p):
return general_brute_force(K,docs,p)
#stochastic local search
def neighbors(d):
neighbors=[]
for x in d.keywords:
k=copy.copy(d.keywords)
k[x]=not k[x]
d1=implementation.Document('n')
d1.assign(k)
neighbors.append(copy.copy(d1))
return neighbors
def general_neighbors2(d,dke):
neighbors=[]
for x in d.keywords:
k=copy.copy(d.keywords)
k[x]=not k[x]
d1=implementation.Document('n')
d1.assign(k)
if d1.keywords in dke:
neighbors.append(copy.copy(d1))
return neighbors
def gen_doc_key_combos(docs):
"""[Documents] -> [dict(str,bool)]
Generate a list of document keyword combinations from a
list of documents."""
dk=[]
for d in docs:
dk.append(d.keywords)
return dk
def general_FLS(I,K,docs,p):
random.seed()
d=random.choice(docs)
top=[]
k=general_PRs(d,p)
for i in range(I):
neighbors1=neighbors(d)
r={}
for x in neighbors1:
r[x]=general_PRs(x,p)
m=max(r, key=r.get)
if r[m]>k:
top.append(m)
d=m
k=r[m]
else:
d=random.choice(docs)
# top=top.sort()
top=top[len(top)-1:len(top)-1-K:-1]
return top
def general_SLS(I,K,docs,p):
random.seed()
d=random.choice(docs)
top=[]
k=general_PRs(d,p)
for i in range(I):
r = { r:general_PRs(r,p) for r in neighbors(d) }
r = { k1:v for k1, v in r.items() if v > k }
if len(r)>0:
d = random.choice(list(r.keys()))
if d not in top:
bisect.insort(top, d)
k=r[d]
else:
d=random.choice(docs)
if d not in top:
bisect.insort(top, d)
top=top[len(top)-1:len(top)-(K+1):-1]
return top
def general_SLS2(I,K,docs,p):
random.seed()
dke = gen_doc_key_combos(docs)
d=random.choice(docs)
top=[]
k=general_PRs(d,p)
for i in range(I):
r = { r:general_PRs(r,p) for r in general_neighbors2(d,dke) }
r = { k1:v for k1, v in r.items() if v > k }
if len(r)>0:
d = random.choice(list(r.keys()))
dx = find_doc_in_list_by_keywords(docs,d.keywords)
if dx is not None:
if dx not in top:
bisect.insort(top, dx)
k=r[d]
else:
d=random.choice(docs)
dx = find_doc_in_list_by_keywords(docs,d.keywords)
if dx is not None:
if dx not in top:
bisect.insort(top, dx)
if len(top) >= K:
pass
else:
top=top[len(top)-1:len(top)-(K+1):-1]
return top
def uniqueL(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (x in seen or seen_add(x))]
'''
asks person, "Which categories do you belong too?"
outputs a dictionary, with keys of Strings (identifier of person) and values of Person object
(imaginary person that represents only one category)
'''
def make_category_list():
"""None -> dict(str,Person)
Make a list of persons, each representing a category.
"""
pdict={}
# class implementation, class Person
# creating random Person object
pbase=implementation.Person('%imaginary%')
for k in pbase.questionaire:
pdict[str(k)] = implementation.Person('%imaginary%'+str(k))
pdict[str(k)].Qchange(str(k))
pdict[str(k)].set_weight(str(k),1.0)
return pdict
# input: dictionary created by make_category_list()
# output: list of categories (instances of class Person)
def cat_dict_to_cat_list(cat_dict):
cat_list = []
for i,j in cat_dict.items():
cat_list.append(j)
return cat_list
'''
returns a dictionary where the key is a string (name of category that
the list represents) and the value is a list of documents
clist -- list of people (that represent categories)
docs -- list of documents
method -- determines method to call e.g. FLS, SLS, etc.
I -- # of iterations for FLS, SLS, etc.
K -- # of docs
'''
def use_category_list(clist,docs,method,I,K):
"""dict(str,Person) -> [Document] -> (int->int->[Documents]->Person->[Document]) ->
int -> int -> dict(str,[Document])"""
outdct = {}
for k in clist:
cat = clist[k]
outdct[k] = method(I,K,docs,cat)
return outdct
def use_category_list_brute(clist,docs,I,K):
return use_category_list(clist,docs,general_brute_force_iter,I,K)
def use_category_list_FLS(clist,docs,I,K):
return use_category_list(clist,docs,general_FLS,I,K)
def use_category_list_SLS(clist,docs,I,K):
return use_category_list(clist,docs,general_SLS,I,K)
def use_category_list_SLS2(clist,docs,I,K):
return use_category_list(clist,docs,general_SLS2,I,K)
def fancy_results(dres, file=sys.stdout):
"""dict(str,[Document]) -> None"""
for x in dres:
print(str(x)+": ",file=file)
for y in dres[x]:
print("\t"+str(y),file=file)
return
'''
zips up the "top k" documents for each category, which may repeat documents in a list of
at most (# of categories)*(k) documents
input: dictionary -- key: string that is the category, value: list of documents in order that
best relate to that category
output: a list of documents
'''
# input: list l of ordered documents (repeats)
# output: final of list of documents (no repeats) of size k
def final_list(l, k):
count = {}
final = []
# adds docs in order from input list ignoring repeats until length k
for doc in l:
if len(final) == k:
break
d = doc.title
if d in count.keys():
count[d] += 1
else:
count[d] = 1
final.append(doc)
return final
###############################################
# sample users Laura and Chris
lo = implementation.Person("Laura")
lo.Qchange('environmentalist')
lo.Qchange('economist')
lo.Qchange('lovesanimals')
lo.set_weight('environmentalist', 0.2)
lo.set_weight('economist', 0.3)
lo.set_weight('lovesanimals', 0.5)
#print (lo.name, lo.questionaire, lo.weights)
chris = implementation.Person("Chris")
chris.Qchange('30orOlder')
chris.Qchange('SellsOil')
chris.Qchange('works_in_industry')
chris.Qchange('lovesanimals')
chris.set_weight('30orOlder', 0.25)
chris.set_weight('SellsOil', 0.3)
chris.set_weight('works_in_industry', 0.35)
chris.set_weight('lovesanimals', 0.1)
#print (chris.name, chris.questionaire, chris.weights)
################################################
# create test documents
# very very applicable to lo
d1 = implementation.Document('d1')
d1.add_keyword('economics')
d1.add_keyword('animals')
d1.add_keyword('ocean')
# very very applicable to chris
d2 = implementation.Document('d2')
d2.add_keyword('foradults')
d2.add_keyword('excessoil')
d2.add_keyword('industry')
# semi-applicable to both chris and lo
d3 = implementation.Document('d3')
d3.add_keyword('ocean')
d3.add_keyword('animals')
# more applicable to chris than lo
d4 = implementation.Document('d4')
d4.add_keyword('medical')
d4.add_keyword('math')
d4.add_keyword('foradults')
d4.add_keyword('excessoil')
d4.add_keyword('industry')
d4.add_keyword('animals')
docs = [d1,d2,d3,d4]
# helper function
# takes string and creates an instance of class Person (category)
# returns the instance of class Person (category)
def str_to_cat(s):
cat = implementation.Person("%s" % s)
cat.Qchange(s)
return cat
# generates clist for a user (helper method for list_per_user)
# clist is a list with each element as a category (str) the user is
def clist_per_user(person):
clist = []
for key,val in person.questionaire.items():
if (val == True):
clist.append(key)
else:
continue
return clist
# helper function
def find_doc_in_list(docs, title):
for d in docs:
if d.title == title:
return d
else:
continue
return None #"Error. Document not found with that title."
def find_doc_in_list_by_title(docs, title):
return find_doc_in_list(docs,title)
def find_doc_in_list_by_keywords(docs, keywords):
for d in docs:
if d.keywords == keywords:
return d
else:
continue
return None #"Error. Document not found with that title."
# Input:
# person is a user (instance of class Person)
# docs is a list of all documents in the system
# I is the number of iterations (moot point for brute force)
# K is the number of documents in final list
# Output:
# list of top k weighted documents for the specific user
def list_per_user(person, docs, I, K, typ="sls2"):
# clist is a list of the categories the user belongs to
clist = clist_per_user(person)
cat_list = {}
# convert list of strings to list of categories for person
# cat_list is now a list of categories as instances of the class Person for the specific user
for i in clist:
cat_list[i] = str_to_cat(i)
# final_dict is a dictionary with keys as categories and values as a list of docs for that category
if typ=="brute":
final_dict = use_category_list_brute(cat_list, docs, I, K)
else:
final_dict = use_category_list_SLS2(cat_list, docs, I, K)
# for each document in value lists, store in dictionary as key and accumulate the value as weight
weighted_docs = {}
for cat_string,value_list in final_dict.items():
# store weight and doc in dictionary weighted_docs
cat_instance = str_to_cat(cat_string)
for d in value_list:
curr_doc = d #find_doc_in_list(docs, d)
if curr_doc is not None:
curr_weight = general_PRs(curr_doc, cat_instance)
if d not in weighted_docs.keys():
weighted_docs[d] = curr_weight
else:
weighted_docs[d] += curr_weight
return select_top_k_docs(weighted_docs,K)
# helper function
# sorts dictionary of documents by weight (selects highest k docs)
# returns list of k docs (those most applicable to hybrid user)
def select_top_k_docs(dict_of_docs, k):
final_list = sorted(dict_of_docs,key=dict_of_docs.get,reverse=True)[:k]
return final_list
################################################
# FINAL TEST: do the methods return an accurate list of most applicable documents
# for hybrid users???
def post_evaluation(docs, person):
n = 0
for i in docs:
n += general_PRs(i, person)
return n
test_l = list_per_user(lo , docs, 400, 3)
print(lo.name, "..", test_l, post_evaluation(test_l, lo))
test_l = list_per_user(lo , docs, 400, 3, "brute")
print(lo.name, "[]", test_l, post_evaluation(test_l, lo))
test_l = list_per_user(chris, docs, 400, 3)
print(chris.name, "..", test_l, post_evaluation(test_l, chris))
test_l = list_per_user(chris, docs, 400, 3, "brute")
print(chris.name, "[]", test_l, post_evaluation(test_l, chris))