#!/usr/bin/python
#encoding:utf8

import argparse

'''
Dla zadanego zbioru danych wykorzystanych do rzutowania PLWN-PWN-SUMO
Sprawdza licznosci pojec w kontekscie danej relacji
'''

import sys

def make_str_pwn_sumo_dict(in_file):
	str_dict = dict()
	with open(in_file, 'rt') as fin:
		for line in fin:
			line = line.strip()
			spl_line = line.split(';')
			sumo_lemma = spl_line[8].strip().lower()
			pwn_sumo_rel = spl_line[-1].strip().lower()
			str_key = '%s;%s' % (pwn_sumo_rel, sumo_lemma)
			
			if not str_dict.has_key(str_key):
				str_dict[str_key] = 0
			str_dict[str_key] += 1
	return str_dict

def make_rel_stat(str_dict):
	rel_eq_out = dict()
	rel_sb_out = dict()
	rel_io_out = dict()

	for str_key, conc_rel_count in str_dict.iteritems():
		pwnsumo_rel, sumo_concept_name = str_key.split(';')
		if pwnsumo_rel in 'equivalent':
			rel_eq_out[sumo_concept_name] = conc_rel_count
		elif pwnsumo_rel == 'subsumed':
			rel_sb_out[sumo_concept_name] = conc_rel_count
		elif pwnsumo_rel == 'instance_of':
			rel_io_out[sumo_concept_name] = conc_rel_count 
		else:
			print 'Nieznane:', str_key, conc_rel_count
	return [rel_eq_out, rel_sb_out, rel_io_out] 

def show_header():
	print >> sys.stderr, 'Relation;Sumo concept lemma;Sumo concept count'

def show_rel_stat(rel_name, rel_dict):
	for pwn_sumo_concept_name, conc_count in rel_dict.iteritems():
		print >> sys.stderr, '%s;%s;%d' % (rel_name, pwn_sumo_concept_name, conc_count)

def go(args = None):

	argp = argparse.ArgumentParser(
		description = \
		'Dla zadanego zbioru danych wykorzystanych do rzutowania PLWN-PWN-SUMO' \
		' sprawdza licznosci pojec w kontekscie danej relacji')
	argp.add_argument('mapping_result_file', 
		help='plik csv z wynikami rzutowania plWN -> SUMO')
	argv = argp.parse_args(args)
	
	str_dict = make_str_pwn_sumo_dict(argv.mapping_result_file)
	eq_dict, subs_dict, instof_dict = make_rel_stat(str_dict)
	
	show_header()
	show_rel_stat('equivalent', eq_dict)
	show_rel_stat('subsumed', subs_dict)
	show_rel_stat('instance_of', instof_dict)

if __name__ == '__main__':
	go()
