nlp.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if not command:
  25. command = token
  26. command_index = i
  27. break
  28. else:
  29. return "Hey! Une commande à la fois svp, je ne suis qu'en beta."
  30. if not command:
  31. return "Je n'ai pas compris votre demande"
  32. logger.info("Commande: %s", command)
  33. # Détaille les arguments de la commande
  34. if command.lemma_ == "copy":
  35. args = doc[command_index + 1:]
  36. what = ""
  37. where = ""
  38. for token in args:
  39. if not what:
  40. if Path(token.text).expandvars().isfile():
  41. # chemin absolu vers un fichier existant
  42. what = Path(token.text).expandvars()
  43. continue
  44. if Path(token.text).expandvars().abspath().isfile():
  45. # chemin relatif vers un fichier existant
  46. what = Path(token.text).expandvars().abspath()
  47. continue
  48. if Path(token.text).expandvars().isdir():
  49. # chemin absolu vers un repertoire existant
  50. what = Path(token.text).expandvars()
  51. continue
  52. if Path(token.text).expandvars().abspath().isdir():
  53. # chemin relatif vers un repertoire existant
  54. what = Path(token.text).expandvars().abspath()
  55. continue
  56. if not where:
  57. if Path(token.text).expandvars().isdir():
  58. # la cible est un répertoire existant (chemin absolu)
  59. where = Path(token.text).expandvars()
  60. continue
  61. if Path(token.text).expandvars().abspath().isdir():
  62. # la cible est un répertoire existant (chemin relatif)
  63. where = Path(token.text).expandvars().abspath()
  64. continue
  65. if Path(token.text).expandvars().parent.isdir():
  66. # la cible est un répertoire inexistant (chemin absolu), mais son parent existe
  67. where = Path(token.text).expandvars()
  68. continue
  69. if Path(token.text).expandvars().abspath().parent.isdir():
  70. # la cible est un répertoire inexistant (chemin absolu), mais son parent existe
  71. where = Path(token.text).expandvars().abspath()
  72. continue
  73. if not what or not where:
  74. return "Je n'ai pas compris votre demande"
  75. return "Vous voulez copier '{}' vers '{}'".format(what, where)
  76. if command.lemma_ == "delete":
  77. args = doc[command_index + 1:]
  78. what = ""
  79. for token in args:
  80. if Path(token.text).abspath().isfile() or Path(token.text).abspath().isdir():
  81. what = Path(token.text)
  82. break
  83. if not what:
  84. return "Je n'ai pas compris votre demande"
  85. return "Vous voulez supprimer '{}'".format(what)
  86. if command.lemma_ == "rename":
  87. return "Vous voulez renommer"
  88. if command.lemma_ == "move":
  89. return "Vous voulez déplacer"
  90. if command.lemma_ == "open":
  91. return "Vous voulez ouvrir"
  92. if command.lemma_ == "new":
  93. return "Vous voulez créer"
  94. return "Je n'ai pas compris votre demande"
  95. def print_analyse(doc):
  96. for token in doc:
  97. logger.debug("\tJeton: %s", token.text)
  98. logger.debug("\t\t* Lemma: %s", token.lemma_)
  99. logger.debug("\t\t* Position: %s", token.pos_)
  100. logger.debug("\t\t* Tag: %s", token.tag_)
  101. logger.debug("\t\t* Dependance: %s", token.dep_)
  102. logger.debug("\t\t* Forme: %s", token.shape_)
  103. logger.debug("\t\t* Alphanum.: %s", token.is_alpha)
  104. logger.debug("\t\t* Stop: %s", token.is_stop)
  105. for ent in doc.ents:
  106. logger.debug("\tEntité: %s", ent.text)
  107. logger.debug("\t\t* Start: %s", ent.start_char)
  108. logger.debug("\t\t* End: %s", ent.end_char)
  109. logger.debug("\t\t* Label: %s", ent.label_)
  110. if __name__ == "__main__":
  111. HERE = Path(__file__).parent
  112. TEST_DIR = HERE.parent / "test"
  113. TEST_I_DIR = TEST_DIR / "init"
  114. TEST_W_DIR = TEST_DIR / "work"
  115. logger.info(submit(r"Copy from {} in {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR)))
  116. logger.info(submit(r"Copy {} to {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR)))
  117. logger.info(submit(r"Copy {} to {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR.relpath(HERE))))
  118. logger.info(submit(r"Copy {} to {}".format((TEST_I_DIR / "a.txt").relpath(HERE), TEST_W_DIR)))
  119. logger.info(submit(r"Delete {}".format(TEST_I_DIR / "a.txt")))
  120. # submit(r"Rename .\test\work\a.txt in .\test\work\b.txt")