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

class LemmaNode(nodeinterface.NodeInterface):
  """!
  Represents node with lemma from the token used as a key.
  """

  def __init__(self, tagset):
    """!
    A child of NodeInterface, constructor only calls the NodeInterface
    constructor and doing nothing else.

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

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

    It gets as argument a tokent and convert it to string of lemma. It uses
    for converting method lemma_utf8() from corpus2 and the utf8 is decoded
    to utf8 also. As a lemma is chosen preffered lexeme from the token. 
    Often it is a lexeme with disamb tag stored into ccl file.

    @param token: corpus2.Token which sould be converted to key
    @return lemma of given token decoded to utf8 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 == "%":
      return "\%"
    return [unicode(lemma_utf8)]


class LemmaNodeLower(LemmaNode):
  """!
  Represents node with lower lemma from the token used as a key.
  It is a child of class LemmaNode.
  """

  def __init__(self, tagset):
    """!
    A child of LemmaNode, constructor only calls the LemmaNodeLower
    constructor and doing nothing else.

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

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

    It gets as argument a tokent and convert it to string of lemma. It uses
    for converting method lemma_utf8() from corpus2 and the utf8 is decoded
    to utf8 also. As a lemma is chosen preffered lexeme from the token. 
    Often it is a lexeme with disamb tag stored into ccl file. The lemma is
    returned as lower() string

    @param token: corpus2.Token which sould be converted to key
    @return lower-lemma of given token decoded to utf8 
            or None if token is not given
    """
    if not token:
      return None
    return [super(LemmaNodeLower, self).key(token)[0].lower()]


class LemmaNodeUpper(LemmaNode):
  """!
  Represents node with upper lemma from the token used as a key.
  It is a child of class LemmaNode.
  """

  def __init__(self, tagset):
    """!
    A child of LemmaNode, constructor only calls the LemmaNodeLower
    constructor and doing nothing else.

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

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

    It gets as argument a tokent and convert it to string of lemma. It uses
    for converting method lemma_utf8() from corpus2 and the utf8 is decoded
    to utf8 also. As a lemma is chosen preffered lexeme from the token. 
    Often it is a lexeme with disamb tag stored into ccl file. The lemma is
    returned as upper() string

    @param token: corpus2.Token which sould be converted to key
    @return upper-lemma of given token decoded to utf8 
            or None if token is not given
    """
    if not token:
      return None
    return [super(LemmaNodeUpper, self).key(token)[0].upper()]