nlp.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import en_core_web_lg
  2. import fr_core_news_md # @UnusedImport
  3. from path import Path
  4. from core import logging_
  5. logger = logging_.getLogger("nlp")
  6. logger.info("Nestor se réveille...")
  7. # nlp = fr_core_news_md.load()
  8. nlp = en_core_web_lg.load()
  9. logger.info("> Ok")
  10. # process:
  11. # 1: question, affirmation, injonction?
  12. def submit(command):
  13. logger.info("""Message reçu: "%s" """, command)
  14. doc = nlp(command)
  15. # print_analyse(doc)
  16. # 1. Cherche l'action (copier)
  17. # 2. Cherche quoi copier (fichier A)
  18. # 3. Cherche vers où le copier (fichier B)
  19. # Quelle commande?
  20. command = ""
  21. command_index = -1
  22. for i, token in enumerate(doc):
  23. if token.lemma_ in ("copy", "delete", "rename", "move", "open", "new", "create"):
  24. command = token
  25. command_index = i
  26. break
  27. if not command:
  28. return "Je n'ai pas compris votre demande"
  29. logger.info("Commande: %s", command)
  30. if command.lemma_ == "copy":
  31. args = doc[command_index + 1:]
  32. what = ""
  33. where = ""
  34. for token in args:
  35. if not what:
  36. if Path(token.text).abspath().isfile():
  37. what = Path(token.text)
  38. continue
  39. if not where:
  40. if Path(token.text).abspath().isdir():
  41. where = Path(token.text)
  42. continue
  43. if not what or not where:
  44. return "Je n'ai pas compris votre demande"
  45. return "Vous voulez copier '{}' vers '{}'".format(what, where)
  46. if command.lemma_ == "delete":
  47. args = doc[command_index + 1:]
  48. what = ""
  49. for token in args:
  50. if Path(token.text).abspath().isfile() or Path(token.text).abspath().isdir():
  51. what = Path(token.text)
  52. break
  53. if not what:
  54. return "Je n'ai pas compris votre demande"
  55. return "Vous voulez supprimer '{}'".format(what)
  56. if command.lemma_ == "rename":
  57. return "Vous voulez renommer"
  58. if command.lemma_ == "move":
  59. return "Vous voulez déplacer"
  60. if command.lemma_ == "open":
  61. return "Vous voulez ouvrir"
  62. if command.lemma_ == "new":
  63. return "Vous voulez créer"
  64. return "Je n'ai pas compris votre demande"
  65. def print_analyse(doc):
  66. for token in doc:
  67. logger.debug("\tJeton: %s", token.text)
  68. logger.debug("\t\t* Lemma: %s", token.lemma_)
  69. logger.debug("\t\t* Position: %s", token.pos_)
  70. logger.debug("\t\t* Tag: %s", token.tag_)
  71. logger.debug("\t\t* Dependance: %s", token.dep_)
  72. logger.debug("\t\t* Forme: %s", token.shape_)
  73. logger.debug("\t\t* Alphanum.: %s", token.is_alpha)
  74. logger.debug("\t\t* Stop: %s", token.is_stop)
  75. for ent in doc.ents:
  76. logger.debug("\tEntité: %s", ent.text)
  77. logger.debug("\t\t* Start: %s", ent.start_char)
  78. logger.debug("\t\t* End: %s", ent.end_char)
  79. logger.debug("\t\t* Label: %s", ent.label_)
  80. if __name__ == "__main__":
  81. file_init_a = Path(__file__).parent.parent / "test" / "init" / "a.txt"
  82. dir_a = Path(__file__).parent.parent / "test" / "work"
  83. file_a = Path(__file__).parent.parent / "test" / "work" / "a.txt"
  84. file_b = Path(__file__).parent.parent / "test" / "work" / "b.txt"
  85. file_init_a.copy(file_a)
  86. logger.info(submit(r"Copy from {} in {}".format(file_init_a, dir_a)))
  87. logger.info(submit(r"Copy {} to {}".format(file_init_a, dir_a)))
  88. logger.info(submit(r"Delete {}".format(file_init_a)))
  89. # submit(r"Rename .\test\work\a.txt in .\test\work\b.txt")