#!/usr/bin/env python

#assumes final score dictionary 'a' 'b' 5 and 'b' 'a' 4 (both directions specified)

import pickle
import operator
import sys,os,random
from collections import defaultdict
import sagh

'''
d = {}
d[('a','a')] = 1
d[('a','b')] = 0.1
d[('a','c')] = 0.4
d[('a','d')] = 0.6
d[('a','e')] = 0.2
d[('b','a')] = 0.8
d[('b','b')] = 1
d[('b','c')] = 0.4
d[('b','d')] = 0.3
d[('b','e')] = 0.5
d[('c','a')] = 0.5
d[('c','b')] = 0.1
d[('c','c')] = 1
d[('c','d')] = 0.3
d[('c','e')] = 0.4
d[('d','a')] = 0.7
d[('d','b')] = 0.1
d[('d','c')] = 0.4
d[('d','d')] = 1
d[('d','e')] = 0.2
d[('e','a')] = 0.2 
d[('e','b')] = 0.1
d[('e','c')] = 0.4
d[('e','d')] = 0.3
d[('e','e')] = 1
file = open('log.txt','w')
pickle.dump(d,file)
sys.exit(0)
'''
###INPUTS

if len(sys.argv) < 3:
    print >>sys.stderr, "Usage: compute-global-top-similar-users SCORE_FILE TOP_K  USER..."
    sys.exit(1)


filename = sys.argv[1]
k = int(sys.argv[2]) # 5 #return top-k for each developer
seedUsersFileName = sys.argv[3]
Inames = [line.strip() for line in open(seedUsersFileName)] # ['FordArnold','TomasCayuelas','astronaughts','MarkHarris'] #list of input developers

topPairsFileName = "top-similar.%s.%s.%s" % (seedUsersFileName, k, filename) #sagh.suffixWithArgs("top-similar")

    
O = {}
print >>sys.stderr, "loading %s" % filename
score = pickle.load(open(filename))

# use dictionary if available
dictFilename = filename + ".dict"
if os.access(dictFilename, os.R_OK):
    print >>sys.stderr, "using dictionary %s" % filename
    namemap = pickle.load(open(dictFilename))
    def nameFor(v): return namemap["inverse"][v]
    def idFor(n): return namemap["forward"][n]
else:
    def nameFor(v): return v
    def idFor(n): return n

I = map(idFor, Inames)
#print >>sys.stderr, I

data = defaultdict(list)
#data['a'] = [('b',.5),('c',.2),('a',1)]
#data['b'] = [('a',.4),('c',.3),('b',1)]
#data['c'] = [('a',.5),('b',.2),('c',1)]

allIds = set()
for (u,v),score in score.iteritems():
    allIds.add(u)
    allIds.add(v)
    if u in I:
        data[u].append((v,score))

allIds = [u for u in allIds]
#print data


for d1 in I:
    l = data[d1]
    l = sorted(l, key=operator.itemgetter(1), reverse = True)
    O[d1] = []
    for count in range(min(len(l)-1,k)):
        O[d1].append(l[count+1])
    while len(O[d1]) < k:
        d2 = random.choice(allIds)
        if d2 in O[d1] or d1 == d2: continue
        O[d1].append((d2,0))



#print '-------------------------------------------'
#print 'Scores for', filename 
#print '-------------------------------------------'
for o,vs in O.iteritems():
    print "%s:\t%s" % (nameFor(o), "\t".join(map(lambda (v,s): "%s=%f" % (nameFor(v),s), vs)))
#print '-------------------------------------------'

print >>sys.stderr, "storing %s" % topPairsFileName
pickle.dump(O, open(topPairsFileName, 'w'))

fbdff4632c77cb8faf44db805441df022f99bb42
#!/usr/bin/env python

#assumes final score dictionary 'a' 'b' 5 and 'b' 'a' 4 (both directions specified)

import pickle
import operator
import sys,os,random
from collections import defaultdict
import sagh

'''
d = {}
d[('a','a')] = 1
d[('a','b')] = 0.1
d[('a','c')] = 0.4
d[('a','d')] = 0.6
d[('a','e')] = 0.2
d[('b','a')] = 0.8
d[('b','b')] = 1
d[('b','c')] = 0.4
d[('b','d')] = 0.3
d[('b','e')] = 0.5
d[('c','a')] = 0.5
d[('c','b')] = 0.1
d[('c','c')] = 1
d[('c','d')] = 0.3
d[('c','e')] = 0.4
d[('d','a')] = 0.7
d[('d','b')] = 0.1
d[('d','c')] = 0.4
d[('d','d')] = 1
d[('d','e')] = 0.2
d[('e','a')] = 0.2 
d[('e','b')] = 0.1
d[('e','c')] = 0.4
d[('e','d')] = 0.3
d[('e','e')] = 1
file = open('log.txt','w')
pickle.dump(d,file)
sys.exit(0)
'''
###INPUTS

if len(sys.argv) < 3:
    print >>sys.stderr, "Usage: compute-global-top-similar-users SCORE_FILE TOP_K  USER..."
    sys.exit(1)


filename = sys.argv[1]
k = int(sys.argv[2]) # 5 #return top-k for each developer
seedUsersFileName = sys.argv[3]
Inames = [line.strip() for line in open(seedUsersFileName)] # ['FordArnold','TomasCayuelas','astronaughts','MarkHarris'] #list of input developers

topPairsFileName = "top-similar.%s.%s.%s" % (seedUsersFileName, k, filename) #sagh.suffixWithArgs("top-similar")

    
O = {}
print >>sys.stderr, "loading %s" % filename
score = pickle.load(open(filename))

# use dictionary if available
dictFilename = filename + ".dict"
if os.access(dictFilename, os.R_OK):
    print >>sys.stderr, "using dictionary %s" % filename
    namemap = pickle.load(open(dictFilename))
    def nameFor(v): return namemap["inverse"][v]
    def idFor(n): return namemap["forward"][n]
else:
    def nameFor(v): return v
    def idFor(n): return n

I = map(idFor, Inames)
#print >>sys.stderr, I

data = defaultdict(list)
#data['a'] = [('b',.5),('c',.2),('a',1)]
#data['b'] = [('a',.4),('c',.3),('b',1)]
#data['c'] = [('a',.5),('b',.2),('c',1)]

allIds = set()
for (u,v),score in score.iteritems():
    allIds.add(u)
    allIds.add(v)
    if u in I:
        data[u].append((v,score))

allIds = [u for u in allIds]
#print data


for d1 in I:
    l = data[d1]
    l = sorted(l, key=operator.itemgetter(1), reverse = True)
    O[d1] = []
    for count in range(min(len(l)-1,k)):
        O[d1].append(l[count+1])
    while len(O[d1]) < k:
        d2 = random.choice(allIds)
        if d2 in O[d1] or d1 == d2: continue
        O[d1].append((d2,0))



#print '-------------------------------------------'
#print 'Scores for', filename 
#print '-------------------------------------------'
for o,vs in O.iteritems():
    print "%s:\t%s" % (nameFor(o), "\t".join(map(lambda (v,s): "%s=%f" % (nameFor(v),s), vs)))
#print '-------------------------------------------'

print >>sys.stderr, "storing %s" % topPairsFileName
pickle.dump(O, open(topPairsFileName, 'w'))
