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

"""
This module fixes sumo mapping according to cross-lingual relations.
"""
__author__ = 'mieszko'
from plumper.sumo.plwnpwnsumo_file import MappingFileOperator
import StringIO
import collections
import sys
import argparse

class Windower(MappingFileOperator):
    def __init__(self):
        MappingFileOperator.__init__(self)
        self._line_ids = None
        self._plwn_sumo_rels = None
        self._rels_buff = []

    def __linefunc(self, cross_ling_rel, plWN_str, sumo_str, sumo_rel):
        line_id = plWN_str, sumo_str
        
        if ((line_id, sumo_rel) not in self._line_ids and sumo_rel != "R"):
            if ((cross_ling_rel == "hipo_pa" and (line_id,"hiper_pa") in self._rels_buff) or 
                (cross_ling_rel == "hiper_pa" and (line_id,"hipo_pa") in self._rels_buff)):
                    self._plwn_sumo_rels[line_id].add("instance_of")
            elif ((cross_ling_rel == "syn_pa" and (line_id, "hiper_pa") in self._rels_buff) or 
                (cross_ling_rel == "hiper_pa" and (line_id, "syn_pa") in self._rels_buff)):
                    self._plwn_sumo_rels[line_id].add("equivalent")
            elif ((cross_ling_rel == "mero:el_pa" and (line_id, "hipo_pa") in self._rels_buff) or 
                (cross_ling_rel == "hipo_pa" and (line_id, "mero:el_pa") in self._rels_buff) or
                
                (cross_ling_rel == "mero:cz_pa" and (line_id, "hipo_pa") in self._rels_buff) or 
                (cross_ling_rel == "hipo_pa" and (line_id, "mero:cz_pa") in self._rels_buff) or

                (cross_ling_rel == "mero:cz_pa" and (line_id, "hiper_pa") in self._rels_buff) or 
                (cross_ling_rel == "hiper_pa" and (line_id, "mero:cz_pa") in self._rels_buff) or

                (cross_ling_rel == "synmr_pa" and (line_id, "syncz_pa") in self._rels_buff) or 
                (cross_ling_rel == "syncz_pa" and (line_id, "synmr_pa") in self._rels_buff) or

                (cross_ling_rel == "synmr_pa" and (line_id, " hipo_pa") in self._rels_buff) or 
                (cross_ling_rel == " hipo_pa" and (line_id, "synmr_pa") in self._rels_buff)):
                    self._plwn_sumo_rels[line_id].add("subsumed")
        
        self._line_ids.append((line_id, sumo_rel))
        self._rels_buff.append((line_id, cross_ling_rel))

    def clean_window(self, mapping_result_file, out_fileobj=sys.stdout):
        self._line_ids = []
        self._plwn_sumo_rels = collections.defaultdict(set)

        self.read_mapping_file(mapping_result_file,
                               (u'rel_miedzyjezykowa', u'synset_str_plwn',
                                u'pojecie_sumo',
                                -1),
                               self.__linefunc)

        # Expand header to add the output column
        self._header.append('plwn_sumo_rel_step3')

        for i in xrange(len(self._line_ids)):
            line_id, sumo_rel = self._line_ids[i]
            self._linebuffer[i].append(
                sumo_rel
                if line_id not in self._plwn_sumo_rels or
                   len(self._plwn_sumo_rels[line_id]) != 1
                else list(self._plwn_sumo_rels[line_id])[0])

        self.print_contents(out_fileobj)


def main():
    argparser = argparse.ArgumentParser(description = __doc__)
    argparser.add_argument('mapping_result_file', help="csv file with " +
                                                       "plWN -> SUMO " +
                                                       "output")
    args = argparser.parse_args()
    b = Windower()
    b.clean_window(args.mapping_result_file)

if __name__ == '__main__':
    main()
