nlp.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import en_core_web_lg
  2. import fr_core_news_md # @UnusedImport
  3. from core import logging_
  4. logger = logging_.getLogger("nlp")
  5. logger.info("Nestor se réveille...")
  6. # nlp = fr_core_news_md.load()
  7. nlp = en_core_web_lg.load()
  8. logger.info("> Ok")
  9. def submit(command):
  10. logger.info("""Message reçu: "%s" """, command)
  11. doc = nlp(command)
  12. for token in doc:
  13. logger.debug("\tJeton: %s", token.text)
  14. logger.debug("\t\t* Lemma: %s", token.lemma_)
  15. logger.debug("\t\t* Position: %s", token.pos_)
  16. logger.debug("\t\t* Tag: %s", token.tag_)
  17. logger.debug("\t\t* Dependance: %s", token.dep_)
  18. logger.debug("\t\t* Forme: %s", token.shape_)
  19. logger.debug("\t\t* Alphanum.: %s", token.is_alpha)
  20. logger.debug("\t\t* Stop: %s", token.is_stop)
  21. for ent in doc.ents:
  22. logger.debug("\tEntité: %s", ent.text)
  23. logger.debug("\t\t* Start: %s", ent.start_char)
  24. logger.debug("\t\t* End: %s", ent.end_char)
  25. logger.debug("\t\t* Label: %s", ent.label_)
  26. unused = []
  27. for i in range(len(doc)):
  28. token = doc[i]
  29. if token.lemma_ == "copy":
  30. for j in range(i, len(doc)):
  31. token = doc[j]
  32. if token.lemma_ in ["from"]:
  33. from_ = doc[j + 1].text
  34. if token.lemma_ in ["to", "in"]:
  35. to_ = doc[j + 1].text
  36. logger.info("Vous voulez copier %s vers %s", from_, to_)
  37. break
  38. if token.lemma_ == "delete":
  39. what = doc[i + 1]
  40. logger.info("Vous voulez supprimer %s", what)
  41. break
  42. if token.lemma_ == "rename":
  43. logger.info("Vous voulez renommer")
  44. if token.lemma_ == "move":
  45. logger.info("Vous voulez déplacer")
  46. if token.lemma_ == "open":
  47. logger.info("Vous voulez ouvrir")
  48. if token.lemma_ == "new":
  49. logger.info("Vous voulez créer")
  50. logger.debug("Connais pas: %s", token.text)
  51. unused.append(token)
  52. def analyse(message):
  53. doc = nlp(message)
  54. for token in doc:
  55. print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
  56. token.shape_, token.is_alpha, token.is_stop)
  57. for ent in doc.ents:
  58. print(ent.text, ent.start_char, ent.end_char, ent.label_)
  59. if __name__ == "__main__":
  60. submit(r"Copy from .\test\work\a.txt to .\test\work\b.txt")
  61. submit(r"Delete .\test\work\a.txt")
  62. # submit(r"Rename .\test\work\a.txt in .\test\work\b.txt")