#
# <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 corpus2
import nodeinterface
import grafon.constants as constants

class LemmaPosNode(nodeinterface.NodeInterface):
  """!
  This class inherit from NodeInterface.
  Represents the token as a pair of part of speeach and lemma.
  """
  
  def __init__(self, tagset):
    """!
    A child of NodeInterface, constructor calls the NodeInterface
    constructor and set __full_pos_mask to the mask based on the given tagset

    @param tagset: Tagset used in process of document reading with type
                   corpus2.Tagset
    """
    super(LemmaPosNode, self).__init__(tagset)
    self.__full_pos_mask = corpus2.get_attribute_mask(self.tagset, '')

  def key(self, token):
    """!
    Implementation of abstract key method.

    It gets as argument a tokent and convert it to pair of part of speech 
    and string of lemma from this token. As a lemma is chosen preffered 
    lexeme from the token - it often is a lexeme with disamb tag stored 
    into ccl file. It uses for converting method lemma_utf8() from corpus2 
    and the utf8 is decoded to utf8 also. The POS tag is also converted to
    string representation.
    
    @param token: corpus2.Token which sould be converted to key
    @return pair of POS, lemma of given token decoded to utf8 and converted
            to string like a "Lemma:pos" or None if token is not given.
    """
    if not token:
      return None
    lexem = token.get_preferred_lexeme(self.tagset)
    lemma_utf8 = lexem.lemma_utf8().decode(constants.ENCODING)
    # fix for node representation as string using %. It is very possible
    # that % may be intepreted as the string requirement
    if lemma_utf8 == "%":
      lemma_utf8 = "\%"
    tag_pos_mask = lexem.tag().get_masked(self.__full_pos_mask)
    return ["%s:%s" % (
      lemma_utf8 , self.tagset.tag_to_symbol_string(tag_pos_mask))]


class LemmaPosNodeLower(LemmaPosNode):
  def __init__(self, tagset):
    """!
    A child of LemmaPosNode, constructor calls the LemmaPosNode constructor.

    @param tagset: Tagset used in process of document reading with type
                   corpus2.Tagset
    """
    super(LemmaPosNodeLower, self).__init__(tagset)

  def key(self, token):
    """!
    Calls parent's method key and makes the key-string as lower string.

    @param token: corpus2.Token which sould be converted to key
    @return pair of POS, lemma of given token decoded to utf8 and converted
            to string like a "lemma:pos", the whole string is converted to 
            lower or returns None if token is not given.
    """
    if not token:
      return None
    return [super(LemmaPosNodeLower, self).key(token)[0].lower()]


class LemmaPosNodeUpper(LemmaPosNode):
  def __init__(self, tagset):
    """!
    A child of LemmaPosNode, constructor calls the LemmaPosNode constructor.

    @param tagset: Tagset used in process of document reading with type
                   corpus2.Tagset
    """
    super(LemmaPosNodeUpper, self).__init__(tagset)

  def key(self, token):
    """!
    Calls parent's method key and makes the key-string as upper string.

    @param token: corpus2.Token which sould be converted to key
    @return pair of POS, lemma of given token decoded to utf8 and converted
            to string like a "LEMMA:POS", the whole string is converted to 
            upper or returns None if token is not given.
    """
    if not token:
      return None
    return [super(LemmaPosNodeUpper, self).key(token)[0].upper()]