# -*- coding: utf-8 -*-
'''
Draws precision/recall/fmeasure graph for multiple values of k
Created on Dec 11, 2009

@author: Bartosz Broda
'''

import sys
import wnp_eval
from wnp_eval import kbest
from wnp_eval import pairs
import matplotlib.pyplot as plt


def main():
	if len(sys.argv) != 5:
		print "Usage: python wnp_eval.py pairs_fileordir kbest_file true|false minsim"
		sys.exit(1)
	pairs_filename = sys.argv[1]
	kbest_filename = sys.argv[2]
	minsim = float(sys.argv[4])
	
	only_wordnet = False
	if sys.argv[3].lower().strip() == 'true':
		only_wordnet = True
	
	print "Opening", pairs_filename
	loaded_pairs = pairs.load(pairs_filename)
	print "Loaded %d pairs" % (len(loaded_pairs))
	print "Loading kbest file", kbest_filename 
	kbest_lists = kbest.parse_kbest_txt_file(kbest_filename, 100)
	print "Loaded kbest lists for %s head words" % len(kbest_lists)
	
	prec = []
	rec = []
	fs = []
	
	m = 50
	for k in range(1,m):
		print "%d/%d" % (k,m)
		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)
		prec.append(p)
		rec.append(r)
		fs.append(f)
	
	plt.plot(range(1,m), prec, color='r', label='Precision')
	plt.plot(range(1,m), rec, color='g', label='Recall')
	plt.plot(range(1,m), fs, color='b', label='F-measure')
	plt.ylabel("[%]")
	plt.xlabel("k")
	plt.legend()
	plt.show()


if __name__ == '__main__':
	main()
	