#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2016  <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 edgebuiderinterface
import grafon.constants as constants

class MSREdgeBuilder(edgebuiderinterface.EdgeBuiderInterface):
  def __init__(self, str_name = 'msr'):
    super(MSREdgeBuilder, self).__init__(str_name)
  
  def add_edges(self, base_graph):
    """
    Based on the chunkrel relation adds edges between two nodes representing
    tokens from text. The nodes are connected with tokens from text between
    which occurs chunkrel relation. New edge is labbeled in accordance with 
    chunkrel relation name.
    
    @param base_graph: Object of grafon.graphs.BaseGraph
    """
    msrhndl = base_graph.resources[constants.RESOURCES_MSR_HNDL]
    if not msrhndl:
      return
    
    for paragraph in base_graph.document.paragraphs():
      for sentence in paragraph.sentences():
        g = base_graph.graph_dict.get(sentence.id(), None)
        if not g:
          logging.warning('There is no graphs for sentence {}'.format(
            sentence.id()))
          continue
        for t1 in sentence.tokens():
          b1 = t1.get_preferred_lexeme(base_graph.tagset).lemma_utf8()
          for t2 in sentence.tokens():
            b2 = t2.get_preferred_lexeme(base_graph.tagset).lemma_utf8()
            if not msrhndl.can_add_edge(b1, b2):
              continue

            from_nodes = base_graph.node_converter.key(t1)
            to_nodes = base_graph.node_converter.key(t2)
            for fn in from_nodes:
              if not g.has_node(fn):
                continue
              for tn in to_nodes:
                if not g.has_node(tn):
                  continue
                if fn and tn:
                  # first way
                  if self.exists_edge(g, fn, tn, self._str_name):
                    g[fn][tn][self._str_name][constants.EDGE_COUNT] += 1
                  else:
                    g.add_edge(
                      fn, tn, key=self._str_name, attr_dict={
                        constants.EDGE_LABEL: self._str_name,
                        constants.EDGE_COUNT: 1})
                  # second way
                  if self.exists_edge(g, tn, fn, self._str_name):
                    g[tn][fn][self._str_name][constants.EDGE_COUNT] += 1
                  else:
                    g.add_edge(
                      tn, fn, key=self._str_name, attr_dict={
                        constants.EDGE_LABEL: self._str_name,
                        constants.EDGE_COUNT: 1})