'''
Draws precision/recall/fmeasure graph for multiple values of k for two MSRs
Created on Dec 15, 2009

@author: Bartosz Broda
'''

import os, 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) != 6:
		print "Usage: python wnp_eval.py pairs_fileordir kbest_file1 kbest_file2 true|false minsim"
		sys.exit(1)
	pairs_filename = sys.argv[1]
	kbest_filename_1 = sys.argv[2]
	kbest_filename_2 = sys.argv[3]
	minsim = float(sys.argv[5])
	
	only_wordnet = False
	if sys.argv[4].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_1 
	kbest_lists_1 = kbest.parse_kbest_txt_file(kbest_filename_1, 100)
	print "Loaded kbest lists for %s head words" % len(kbest_lists_1)
	
	print "Loading kbest file", kbest_filename_2 
	kbest_lists_2 = kbest.parse_kbest_txt_file(kbest_filename_2, 100)
	print "Loaded kbest lists for %s head words" % len(kbest_lists_2)
	
	prec_1 = []
	rec_1 = []
	fs_1 = []
	
	prec_2 = []
	rec_2 = []
	fs_2 = []
	
	m = 100
	for k in range(1,m):
		print "%d/%d" % (k,m)
		ev = wnp_eval.ListEvaluator(kbest_lists_1, loaded_pairs, k = k, flex = 'subst', minsim = minsim)
		p,r = ev.evaluate(only_wordnet)
		f = (2*p*r)/(p+r)
		prec_1.append(p)
		rec_1.append(r)
		fs_1.append(f)
		
		ev = wnp_eval.ListEvaluator(kbest_lists_2, loaded_pairs, k = k, flex = 'subst', minsim = minsim)
		p,r = ev.evaluate(only_wordnet)
		f = (2*p*r)/(p+r)
		prec_2.append(p)
		rec_2.append(r)
		fs_2.append(f)
	
	plt.plot(range(1,m), prec_1, linestyle= '--', color = 'darkorange', label='Precision ' + os.path.basename(kbest_filename_1))
	plt.plot(range(1,m), rec_1, linestyle= '--', color='darkred', label='Recall ' + os.path.basename(kbest_filename_1))
	plt.plot(range(1,m), fs_1, linestyle= '--', color='darksalmon', label='F-measure ' + os.path.basename(kbest_filename_1))
	
	plt.plot(range(1,m), prec_2, color ='midnightblue', label='Precision ' + os.path.basename(kbest_filename_2))
	plt.plot(range(1,m), rec_2, color='olivedrab', label='Recall ' + os.path.basename(kbest_filename_2))
	plt.plot(range(1,m), fs_2, color='springgreen', label='F-measure ' + os.path.basename(kbest_filename_2))
	plt.ylabel("[%]")
	plt.xlabel("k")
	plt.legend()
	plt.show()


if __name__ == '__main__':
	main()
	
