from flow_elem import FlowElem
from serel.base import RecognizerFactory, SerelRecognizer


class Serel(FlowElem):
    """
    The Serel class runs serel project whose aim is to recognize relations
    between named entities (NE). NE are recognized by Liner (thus you need
    to run Liner before running this class).
    """
    def __init__(self, config):
        self.models_ini = config.get('serel', 'models')
        self.only_new = config.get('serel', 'only_new')
        self.tagset = config.get('saper', 'tagset')
        self.serel_recognizer = \
            SerelRecognizer(RecognizerFactory.load(self.models_ini))

    def feed_flow(self, document):
        """Run Serel and add semantic relations between named entities."""
        relations = self.serel_recognizer.process_document(
            document,
            self.only_new,
            self.tagset
            )
        for relation in relations:
            document.add_relation(relation.make_corpus2_rel())
        return document
