#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2014  <copyright holder> <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#
import logging
import corpus2
import networkx
import plwnmappinginterface
import grafon.constants as constants

class PlWNMappingMerger(plwnmappinginterface.PlwnMappingInterface):
  def __init__(self, bg):
    """Only calls the partents constructor."""
    super(PlWNMappingMerger, self).__init__(bg, str_name='PlWNMappingMerger')

  def merge(self, g1, g2):
    """!
    Merge two graphs into one. The first graph into nodes should contains
    tokens from the text which are stored into the nodes. The second graph
    is the mimimal subraph of plWordNet which contains all synsets for
    analyzed tokens from the text. The main idea about this merger is to
    merge two graphs into one which will contains all nodes from plWordNet
    and with text. And based on the word sense disambiguation, the nodes
    with disambiguated words are connected with its sense node with new 
    edge called 'plwn_map_wsd'

    @param g1: it is graph built from basegraph type: Any networkx graph type
    @param g2: sense graph - it is plWordNet graph: Type networkx graph type
    @return g1 and g2 merged into a new graph, type networkx.MultiDiGraph
    """
    logging.info('~\t merger {}'.format(str(self)))

    n_g = networkx.MultiDiGraph()
    for node, node_attr in g2.nodes(data=True):
      n_g.add_node(node, attr_dict=node_attr)
    n_g.add_edges_from(g2.edges(data = True), data = True)

    for node, node_attr in g1.nodes(data=True):
      n_g.add_node(node, attr_dict=node_attr)
    n_g.add_edges_from(g1.edges(data = True), data = True)

    for n in g1:
      for token in g1.node[n][constants.NODE_TOKENS]:
        sense = self.get_sense_for_token(token)
        if sense and n_g.has_node(sense):
          
          n_g.add_edge(n, sense, label = 'plwn_map_wsd')
    return n_g