from flow_elem import FlowElem
from wosedon.run_wosedon import disambiguate_document


class Wsd(FlowElem):
  """
  A class for running WSD module (WoSeDon) in order to disambiguate meanings
  of words in a text.
  """
  def __init__(self, config):
    """
    Initialize the class by getting appropriate configuration file's path.
    
    @param config a configuration object created by the main running script.
    @type config: saper.config.ConfigLoader
    """
    self._wosedon_objects = None
    self._cfg_object = None
    try:
      self._config_path = config.get('wsd','wosedon_config_file')
    except:
      self._config_path = config.path

  def feed_flow(self, document):
    """
    Run the WSD module and enhance the input document with WSD information.
    
    @param document a document object to be passed to the WSD module.
    @type document: corpus2.Document
    
    @return the same document object, but with additional information.
    @rtype: corpus2.Document
    """
    if not self._wosedon_objects:
      # The first returned object is a document, but it's the same as the input
      # document. Thus no need to keep it now.
      _, self._cfg_object, self._wosedon_objects = \
          disambiguate_document(document, self._config_path)
    else:
      disambiguate_document(document, self._cfg_object, self._wosedon_objects)
    return document
