
import sys
from photos import photos

def pca_distance(face1,face2):
	score = 'None'
	f = file('pca_distances/%s.sfi' % face1)
	for line in f.readlines():
		other,score = [x.strip() for x in line.strip().split()]
		if other == (face2+'.sfi'):
			break
	score = eval(score)
	return score

def do_knn(category,k,dist):
	output = []
	train = []
	for photo in photos:
		if len(photo.faces) == 0:
			output = output + [0]
		else:
			ans = 0
			for face in photo.face_ids:
				neighbors = [(None,None)]*k
				for p in train:
					for otherface in p.face_ids:
						d = dist(face,otherface)
						if (None,None) in neighbors:
							for i in range(len(neighbors)):
								if neighbors[i][1] == None:
									if category in p.keywords:
										neighbors[i] = (1,d)
									else:
										neighbors[i] = (0,d)
									break
						else:
							maxi = 0
							for i in range(len(neighbors)):
								if neighbors[i][1] > neighbors[maxi][1]:
									maxi = i
							if d < neighbors[maxi][1]:
								if category in p.keywords:
									neighbors[maxi] = (1,d)
								else:
									neighbors[maxi] = (0,d)
				neighbors = [n[0] for n in neighbors if n != (None,None)]
				if len(neighbors):
					if float(sum(neighbors))/len(neighbors) >= 0.5:
						ans = 1
			output = output + [ans]
		train = train + [photo]
	return output
	
category = sys.argv[1]
k = eval(sys.argv[2])
output = do_knn(category,k,pca_distance)

def fix(b):
	if b:
		return 1
	else:
		return 0

actual = [fix(category in photo.keywords) for photo in photos]

def evaluate(output):
	a,b,c,d = 0,0,0,0
	for line in output:
		prediction, actual = line
		if actual:
			if prediction:
				a = a + 1
			else:
				b = b + 1
		else:
			if prediction:
				c = c + 1
			else:
				d = d + 1
	recall = float(a)/float(a+b)
	precision = float(a)/float(a+c)
	return (recall,precision)

print evaluate(zip(output,actual))

