# -*- coding: utf-8 -*-
'''
Utility for converting of intermediate, multifile format of lemma pairs into single file data.

Intermediate files are for exapmple generated by:
- pwnpairs.py for PWN
- plwnpairs.py for plWordNet

@author: Bartosz Broda, bartosz.broda@pwr.wroc.pl
'''
import codecs, os, sys
from optparse import OptionParser

import plwnpairs

class PairsConv:
	"""
	Loads up all files generated by PairsGen and creates simple file of lemmas in relations containing
	lemma1;lemma2 in every line
	"""
	def __init__(self, workdir):
		self.workdir = workdir
		
	def load_rel_pairs(self, filename):
		"""
		Loads files containing relation;lemma1;lemma2 tuples in every line.
		"""
		pairs = set([])
		header = True
		for line in codecs.open(filename, 'r', 'utf8'):
			line = line.strip()
			if header:
				header = False
				continue
			if len(line):
				rel, l1, l2 = line.split(';')
				l1 = l1.strip()
				l2 = l2.strip()
				pairs.add( (l1,l2) )
		return pairs
	
	def load_synonyms(self, filename):
		"""
		Loads files containing lemma1;lemma2 tuples in every line without skipping headers
		"""
		pairs = set([])
		for line in codecs.open(filename, 'r', 'utf8'):
			line = line.strip()
			if len(line):
				l1, l2 = line.split(';')
				l1 = l1.strip()
				l2 = l2.strip()
				pairs.add( (l1,l2) )
		return pairs	
	
	def load_cousins(self, filename, max_mn_sum):
		pairs = set([])
		header = True
		for line in codecs.open(filename, 'r', 'utf8'):
			line = line.strip()
			if header:
				header = False
				continue
			if len(line):
				l1, l2, lcs, m,n = line.split(';')
				l1 = l1.strip()
				l2 = l2.strip()
				mn = int(m) + int(n)
				if mn <= max_mn_sum:
					pairs.add( (l1,l2) )
					
		return pairs
	
	def load_all(self, max_mn_sum):
		filename = os.path.join(self.workdir, plwnpairs.LEX_REL_PAIRS)
		pairs_lex_rel = self.load_rel_pairs( filename )
		print "Loaded %d lemma pairs in lexical relations from %s" % (len(pairs_lex_rel), filename)
		
		filename = os.path.join(self.workdir, plwnpairs.SYN_REL_PAIRS) 
		pairs_syn_rel = self.load_rel_pairs(filename)
		print "Loaded %d lemma pairs in lexical relations from %s" % (len(pairs_syn_rel), filename)
		
		filename = os.path.join(self.workdir, plwnpairs.SYNONYMS)
		pairs_synonyms= self.load_synonyms(filename)
		print "Loaded %d synonymous lemma pairs from %s" % (len(pairs_synonyms), filename)
		
		filename = os.path.join(self.workdir, plwnpairs.COUSINS)
		pairs_cousins = self.load_cousins(filename, max_mn_sum)
		print "Loaded %d cousin lemma pairs having maximal path len = %d from %s" % (len(pairs_cousins), 
																					max_mn_sum, 
																					filename)
		pairs = pairs_cousins.union(pairs_synonyms.union(pairs_lex_rel.union(pairs_syn_rel)))
		
		print "Loaded %d lemma pairs" % len(pairs)
		return pairs
	
def save_pairs(filename, pairs):
	out = codecs.open(filename, 'w', 'utf8')
	for p in sorted(pairs):
		out.write("%s;%s\n" % p)
	out.close()
	
def main():
	parser = OptionParser()
	parser.add_option("-o", "--outfile", dest="outfile", default = 'pairs.csv', help="Output file")
	parser.add_option("-l", "--max-len", dest="maxlen", type='int', help="Maximum value for m+n for (m,n)-cousins", default=3)
	parser.add_option("-w", "--workdir", dest="workdir", default = 'workdir', help="Workdir file (dir with: cousins.csv, " +
					"lex_rel_pairs.csv, syn_rel_pairs.csv and synonyms.csv)")
	
	options = parser.parse_args(sys.argv)[0]
	
	if not os.path.exists(options.workdir):
		os.makedirs(options.workdir)
	
	conv = PairsConv(options.workdir)
	pairs = conv.load_all(options.maxlen)
	
	save_pairs(options.outfile, pairs)
	

if __name__ == '__main__':
	main()
	
