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

'''
Wczytuje rzutowanie PWN na SUMO i przedstawia wynik
'''

import argparse
import os
import sys
import codecs

import plumper.wn.pwn as pwn
import plumper.sumo.pwn2sumo as pwn2sumo

def make_parser():
	desc = 'Krok pierwszy rzutowania plWN -> SUMO'
	parser = argparse.ArgumentParser(description = desc) 
	parser.add_argument('-o', '--output-file', dest = 'output_file', required = True)
	return parser

def main(argv = None):
	parser = make_parser()
	args = parser.parse_args(argv)

	# sense p2s
	pwn_map_dir = '../../../resources/pwn_sumo_map'
	senseindexfile = '../../../resources/wn30-dict/index.sense'
	senseindexfiles = [
			os.path.join(pwn_map_dir, 'WordNetMappings30-adj.txt'),
			os.path.join(pwn_map_dir, 'WordNetMappings30-adv.txt'),
			os.path.join(pwn_map_dir, 'WordNetMappings30-noun.txt'),
			os.path.join(pwn_map_dir, 'WordNetMappings30-verb.txt'),
			os.path.join(pwn_map_dir, 'MWEmapping-noun.txt')
	]
	
	# reading sense index file
	print >> sys.stderr, 'Reading PWN sense index...', 
	spwnreader = pwn.PWNSenseIndexReader(senseindexfile)
	sensesidxs = spwnreader.read()
	print >> sys.stderr, ' read %d instances' % len(sensesidxs)
	
	# reading mapping PWN to SUMO
	print >> sys.stderr, 'Reading map of SUMO instances' 
	p2s_map_list = []
	for sumofilename in senseindexfiles:
		print >> sys.stderr, '\tReading %s' % (sumofilename),
		p2sreader = pwn2sumo.PWN2SUMOReader(sumofilename, sensesidxs)
		p2sm = p2sreader.read(clear_synset_lemma = True)
		print >> sys.stderr, ' read %d instances' % len(p2sm)
		p2s_map_list.extend(p2sm)
	print >> sys.stderr, 'Read %d maps' % len(p2s_map_list)

	# teraz lece z rzutowaniem pl na SUMO :)
	# wszystkie synsetyid jednoznacznione w tekscie/z bazy
	with codecs.open(args.output_file, 'wt', 'utf8') as fout:
		# save header 
		fout.write('synset_str_pwn;pos_str;rel_sumo_pwn;pojecie_sumo\n')
		for p2s in p2s_map_list:
			sense_idx = p2s.pwn_sense_index
			fout.write('%s.%d;%s;%s;%s\n' % (
					sense_idx.lemma, sense_idx.sensenum, 
					sense_idx.pos_disamb_str(),
					p2s.sumo2pwn_relation,
					p2s.sumoinstance.concept_name))

if __name__ == '__main__':
	main()

