# -*- coding: utf-8 -*-

import pymatrices, pycomparator, argparse, os, codecs, corpus2

def main():
    
    parser = argparse.ArgumentParser(description='Question/answer checker')
    parser.add_argument('-m', '--matrix-directory', required=True, dest='m_dir',help='Matrix directory')
    parser.add_argument('-n', '--matrix-name', required=True, dest='m_name',help='Name of matrix')
    parser.add_argument('-d', '--directory', required=True, dest='dir',help='Directory with questions in ccl format')
    parser.add_argument('-s', '--msr', required=True, dest='msr',help='measure of similarity relatedness in SuperMatrix format')
    parser.add_argument('-k', default = 1, dest ='k',help = 'check if good answer is in k best results')
    parser.add_argument('-u', '--dump', dest='dump', default=50, help='')
    args = parser.parse_args()
    qa_dict = {}
    counter = 0
    for path in os.listdir(args.dir):
        counter = counter +1
        if (counter%args.dump)==0:
            print "Loaded:" +str(counter)+"/"+str(len(os.listdir(args.dir)))
        tagset = corpus2.get_named_tagset('nkjp')
        reader = corpus2.TokenReader.create_path_reader('ccl', tagset, str(args.dir+path))
        chunk = reader.get_next_chunk()
        all_pos_mask = corpus2.get_attribute_mask(tagset, '')
        
        question = 'Q_A:'
        answer = 'A_A:'
        while(not(not chunk)):
            chunk_id = chunk.get_attribute('id')
            sentences = chunk.sentences()
            for sent in sentences:
                tokens = sent.tokens()
                for token in tokens:
                    orth = token.orth_utf8()
                    if (orth!=';') and (chunk_id.startswith('Q')):
                        question += orth + " "
                    if (orth!=';') and (chunk_id.startswith('A')):
                        answer += orth + " "
            chunk = reader.get_next_chunk()
        if not qa_dict.has_key(question.strip()):
                qa_dict[question.strip()] = []
        qa_dict[question.strip()].append(answer.strip())
    print "Loaded:" +str(len(os.listdir(args.dir)))+"/"+str(len(os.listdir(args.dir)))
    hit = 0.0
    miss = 0.0
    all = 0.0
    wyjscie = open('odpowiedzi','w')
    for key in qa_dict.iterkeys():
        print all
        if (all%args.dump)==0:
            print "Processed:" +str(int(all))+"/"+str((len(qa_dict)))
        #result = get_best(args.m_dir, args.m_name,args.msr,key.encode('utf8') )
	result = get_best(args.m_dir, args.m_name,args.msr,key,int(args.k))
	
	wyjscie.write('Q:'+key+'\n')
	for odp in result:
		wyjscie.write('A:'+odp[0]+'\n')
	
        all += 1
	ok =0
        if len(result) != 0:
		for res in result:
			if res[0] in qa_dict[key]:
				hit +=1
				ok=1
            	if ok ==0:
			miss +=1
		#if result[0][0] in qa_dict[key]:
                #hit += 1
            #else:
                #miss += 1
        else:
            miss += 1
    wyjscie.close()
    print "Processed:" +str((len(qa_dict)))+"/"+str((len(qa_dict)))
    print "HIT: "+ str(hit)
    print "MISS: "+ str(miss)
    print "ALL:" + str(all)
    print "================="
    print "Precision %: " + str(hit/all*100) 
    
def get_best(matrix_dir, matrix_name, msr, word,k):
    matrix = pymatrices.LoadSuperMatrix(matrix_dir, matrix_name, False, True)
    cmp = pycomparator.FunctionFactory.CreateFunction(msr, matrix)
    return pycomparator.generate_kbest(cmp,word,k)
if __name__ == '__main__':
    main()
