from flow_elem import FlowElem
from npsemrel.ccl_processor import NpSemrelCclProcessor
import os

__author__ = 'blaz'


class Npsemrel(FlowElem):
    """
    The Npsemrel class wraps the npsemrel ccl processor enabling processing
    of corpus2.Document via npsemrel logic.
    """
    def __init__(self, config):

        #This is some wccl hack.
        resources_dir = config.get('npsemrel', 'resources_dir')
        root, _ = os.path.split(resources_dir)
        pwd = os.getcwd()
        os.chdir(root)

        self.npsemrel_processor = \
            NpSemrelCclProcessor(config.get('npsemrel', 'first_type_predicate'),
                                 config.get('npsemrel', 'gerundium_dictionary'),
                                 config.get('npsemrel', 'second_type_predicate'),
                                 config.get('npsemrel', 'wccl_config'),
                                 config.get('npsemrel', 'npsemrel_config'),
                                 tagset=config.get('saper','tagset'),
                                 only_continous=config.get('npsemrel',
                                                    'only_continous')=='True')

        #The hack ends here.
        os.chdir(pwd)

    def feed_flow(self, document):
        """
        Feeds npsemrel processor and returns annotated with relations
        corpus2.Document.
        """
        annotated_document = self.npsemrel_processor.process_document(
            document,
            update = True)
        return annotated_document
