"""

# speed-up spacy: https://medium.com/@vishnups/speeding-up-spacy-f766e3dd033c
"""

import logging.config

import fr_core_news_sm
import yaml

from core.spacy_helper import analyse
from core.speedup import speedup


# ## Logging
with open('logging.yaml', 'rt') as f:
    conf = yaml.load(f)
logging.config.dictConfig(conf)
logger = logging.getLogger("sbot")
# ##

nlp = fr_core_news_sm.load()
speedup(nlp)

def answer_to(sentence):
    """Main program loop: select a response for the input sentence and return it"""
    doc = nlp(sentence)

    root = next((t for t in doc if t.dep_ == "ROOT"))

    print("la racine est: ", root.text)

    if root.pos_ == "VERB":
        print("> root est un verbe")
        print("> Infinitif: ", root.lemma_)

        for t in root.children:
            if t.dep_ == "nsubj":
                print("son sujet est:", t.text)
            if t.dep_ == "dobj":
                print("son objet est:", t.text)

    elif root.pos_ == "NOUN":
        print("> root est un nom commun")

    elif root.pos_ == "PROPN":
        print("> root est un nom propre")
        for t in root.children:
            if t.dep_ == "amod":
                print("son modificateur (adj):", t.text)

            if t.dep_ == "nmod":
                print("son objet est:", t.text)
    else:
        print("> root est un: ", root.pos_)

    return doc.print_tree(light=True)

if __name__ == '__main__':
    msg = "Apple cherche à acheter une startup anglaise pour 1 milliard de dollars."

    analyse(nlp, msg)
