import sys
import os
import subprocess
import time
from wnp_eval import *

def main():
	sim_labeler = "/mnt/synat1a/jbochenek/gitrepos/supermatrix/bin/tools/similarity_results_labeler/./labeler --matrix-type supermatrix --k-best-results 200 "
	if len(sys.argv) != 4:
		print "Usage: " + sys.argv[0] + " <matrices_dir> <wnpairs> <outfile>"
		sys.exit(-1)

	indir = sys.argv[1]
	if not os.path.isdir(indir):
		print indir + " is not a valid directory."
		sys.exit(1)

	pairs_filename = sys.argv[2]
	ofile = open(sys.argv[3],"w")
	wnp_thresholds = [1,5,10,20,30,40,50,60,70,80,90,100]
        only_wordnet = True
        ofile.write("matrix;")
	for threshold in wnp_thresholds:
		ofile.write("P(" + str(threshold) + ");R(" + str(threshold) + ");F(" + str(threshold) + ");")
	ofile.write("\n")

	print "Opening", pairs_filename
	loaded_pairs = pairs.load(pairs_filename)
	print "Loaded %d pairs" % (len(loaded_pairs))

	msrs = {}
	for matrix_name in os.listdir(indir):
		print "PROCESSING MATRIX: " + matrix_name

		matrix_folder = os.path.join(indir, matrix_name)
		if not os.path.isdir(matrix_folder):
			continue
		matrix_file = os.path.join(matrix_folder, "matrix_crs.mat")
		label_file = os.path.join(matrix_folder, matrix_name + ".rlabel")
		results_file = os.path.join(matrix_folder,"200best.txt")
		cmd = sim_labeler + "--matrix-filename " + matrix_file + " --label-file " + label_file + " --results-file " + results_file
		labeler_process = subprocess.Popen(cmd, shell = True)
		while labeler_process.poll() == None:
			time.sleep(1)

		msrs[matrix_name] = {}
		
		kbest_filename = results_file
		ofile.write(matrix_name + ";")

		for k in wnp_thresholds:
			print "Loading kbest file", kbest_filename
			kbest_lists = kbest.parse_kbest_txt_file(kbest_filename, k)
			print "Loaded kbest lists for %s head words" % len(kbest_lists)
		        ev = ListEvaluator(kbest_lists, loaded_pairs, k = k, flex = 'subst')
		        print "Evaluation underway..."
		        (prec,rec) = ev.evaluate(only_wordnet) 
			fscore = 2*(prec*rec)/(prec+rec)
		        print "Precision: %.2f%%\nRecall: %.2f%%" % (prec,rec)
		        print "Skipped %d words..." % len(ev.not_evaluated)
			ofile.write(str(prec) + ";" + str(rec) + ";" + str(fscore) + ";")
			ofile.flush()
		ofile.write("\n") 
	ofile.close()


if __name__ == "__main__":
	main()
		
		
