-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotherways.py
38 lines (32 loc) · 1.19 KB
/
otherways.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
from csc import divisi2
from divisi2.blending import blend
from csc.util.persist import PickleDict
pd = PickleDict('./pickledir/')
def NotOverRide(game, object):
if pd.has_key(game):
overlist = pd[game]['over']
else:
file = open(game + '.over', 'r')
overlist = file.readlines()
file.close()
overlist = [x.lower().replace('\n', '') for x in overlist]
if object in overlist: return False
return True
def clear_top_items(sim, game, num):
all = sim.top_items(num*3)
return [x for x in all if NotOverRide(game, x)][:num]
def top_game_sims(game, object, n=6):
if pd.has_key(game):
similarity = pd[game]['blend']
else:
similarity = make_blend(game + '.pickle')
frame = divisi2.SparseVector.from_counts([object])
return clear_top_items(similarity.right_category(frame), game, n)
def make_blend(thefile):
conceptnet = divisi2.network.conceptnet_matrix('en')
thegame = divisi2.load(thefile).normalize_all()
blended_matrix = blend(conceptnet, thegame)
u,s,v = blended_matrix().svd()
similarity = divisi2.reconstruct_similarity(u,s)
pd[thefile.split('.')[0]] = similarity
return similarity