# -*- coding: utf-8 -*-
'''
Wrapper for wnp_eval that tests multiple k best files for different MSRs. 

Created on 17 Sep, 2010

@author: Jacek Bochenek
'''
import os
import sys
import wnp_eval
import codecs
from wnp_eval import *

def load_msr_descs(ifile):
	msrs = {}
	for line in codecs.open(ifile,"r","utf-8"):
		msr,kbest = line.split(";",1)
		msrs [msr] = kbest
	return msrs

def main():
    msrs = load_msr_descs(sys.argv[1])
    k = int(sys.argv[2])
    pairs_filename = sys.argv[3]
    only_wordnet = False
    if sys.argv[4].lower().strip() == 'true':
        only_wordnet = True
	ofile = codecs.open(sys.argv[5],"w","utf-8")
	ofile.write("MSR;kbestfile;k;wordnet_only;precision;recall\n")
	for msr in sorted(msrs.keys()):
		kbest_filename = msrs[msr]
	
		print "Loading kbest file", kbest_filename 
		kbest_lists = kbest.parse_kbest_txt_file(kbest_filename, k)
		print "Opening", pairs_filename
		loaded_pairs = pairs.load(pairs_filename)
		print "Loaded %d pairs" % (len(loaded_pairs))
		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) 
		print "Precision: %.2f%%\nRecall: %.2f%%" % (prec,rec)
		print "Skipped %d words..." % len(ev.not_evaluated)
		ofile.write(msr)
		ofile.write(";")
		ofile.write(msrs[msr])
		ofile.write(";")
		ofile.write(str[k])
		ofile.write(";")
		ofile.write(str(only_wordnet))
		ofile.write(";")
		ofile.write(str(prec))
		ofile.write(";")
		ofile.write(str(rec))
		ofile.write("\n")
		

if __name__ == '__main__':
	main()
	
