sbot.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. # speed-up spacy: https://medium.com/@vishnups/speeding-up-spacy-f766e3dd033c
  3. """
  4. import logging.config
  5. import fr_core_news_sm
  6. import yaml
  7. from core.spacy_helper import analyse
  8. from core.speedup import speedup
  9. # ## Logging
  10. with open('logging.yaml', 'rt') as f:
  11. conf = yaml.load(f)
  12. logging.config.dictConfig(conf)
  13. logger = logging.getLogger("sbot")
  14. # ##
  15. nlp = fr_core_news_sm.load()
  16. speedup(nlp)
  17. def answer_to(sentence):
  18. """Main program loop: select a response for the input sentence and return it"""
  19. doc = nlp(sentence)
  20. root = next((t for t in doc if t.dep_ == "ROOT"))
  21. print("la racine est: ", root.text)
  22. if root.pos_ == "VERB":
  23. print("> root est un verbe")
  24. print("> Infinitif: ", root.lemma_)
  25. for t in root.children:
  26. if t.dep_ == "nsubj":
  27. print("son sujet est:", t.text)
  28. if t.dep_ == "dobj":
  29. print("son objet est:", t.text)
  30. elif root.pos_ == "NOUN":
  31. print("> root est un nom commun")
  32. elif root.pos_ == "PROPN":
  33. print("> root est un nom propre")
  34. for t in root.children:
  35. if t.dep_ == "amod":
  36. print("son modificateur (adj):", t.text)
  37. if t.dep_ == "nmod":
  38. print("son objet est:", t.text)
  39. else:
  40. print("> root est un: ", root.pos_)
  41. return doc.print_tree(light=True)
  42. if __name__ == '__main__':
  43. msg = "Apple cherche à acheter une startup anglaise pour 1 milliard de dollars."
  44. analyse(nlp, msg)