# -*- coding: utf-8 -*-
'''
Wrapper for wnp_eval that tests one file with multiple k values. 

Created on Dec 11, 2009

@author: Bartosz Broda
'''
import os
import sys
import wnp_eval
from wnp_eval import kbest
from wnp_eval import pairs

def main():
	if len(sys.argv) != 6:
		print "Usage: python wnp_eval.py pairs_fileordir kbest_dir outfile true|false minsim"
		print "\ttrue - will only evaluate using words in wordnet (on kbest list)"
		sys.exit(1)
		
	only_wordnet = False
	if sys.argv[4].lower().strip() == 'true':
		only_wordnet = True
		
	pairs_filename = sys.argv[1]
	kbest_dir = sys.argv[2]
	outfile = sys.argv[3]
	minsim = float(sys.argv[5])
	
	print "Opening", pairs_filename
	loaded_pairs = pairs.load(pairs_filename)
	print "Loaded %d pairs" % (len(loaded_pairs))
	
	lines = []
	i = 0
	for fn in os.listdir(kbest_dir):
		filename = os.path.join(kbest_dir, fn)
		if not os.path.isfile(filename):
			continue
		
		lines.append(fn)
		print "Loading kbest file", filename 
		kbest_lists = kbest.parse_kbest_txt_file(filename, 100)
		print "Loaded kbest lists for %s head words" % len(kbest_lists)
		
		print "Evaluation is underway"
		k_vals = [1,5,10,20,50,75,100]
		for k in k_vals:
			ev = wnp_eval.ListEvaluator(kbest_lists, loaded_pairs, k = k, flex = 'subst', minsim = minsim)
			p,r = ev.evaluate(only_wordnet)
			f = (2*p*r)/(p+r)
			lines[i] += ";%.2f%%;%.2f%%;%.2f%%" % (p,r,f)
		i += 1

	with open(outfile, 'w') as f:
		header = ""
		for k in k_vals:
			header += ";P(%d);R(%d);F(%d)" % (k,k,k)
		f.write(header + '\n')
		print header
		for line in lines:
			print line
			f.write(line + '\n')
			


if __name__ == '__main__':
	main()
	