olinox 7 anni fa
parent
commit
84c61b4aee
1 ha cambiato i file con 81 aggiunte e 48 eliminazioni
  1. 81 48
      core/nlp.py

+ 81 - 48
core/nlp.py

@@ -1,5 +1,7 @@
 import en_core_web_lg
 import fr_core_news_md  # @UnusedImport
+from path import Path
+
 from core import logging_
 
 
@@ -10,75 +12,106 @@ logger.info("Nestor se réveille...")
 nlp = en_core_web_lg.load()
 logger.info("> Ok")
 
+# process:
+# 1: question, affirmation, injonction?
+
 def submit(command):
     logger.info("""Message reçu: "%s" """, command)
     doc = nlp(command)
 
-    for token in doc:
-        logger.debug("\tJeton: %s", token.text)
-        logger.debug("\t\t* Lemma: %s", token.lemma_)
-        logger.debug("\t\t* Position: %s", token.pos_)
-        logger.debug("\t\t* Tag: %s", token.tag_)
-        logger.debug("\t\t* Dependance: %s", token.dep_)
-        logger.debug("\t\t* Forme: %s", token.shape_)
-        logger.debug("\t\t* Alphanum.: %s", token.is_alpha)
-        logger.debug("\t\t* Stop: %s", token.is_stop)
+#     print_analyse(doc)
 
-    for ent in doc.ents:
-        logger.debug("\tEntité: %s", ent.text)
-        logger.debug("\t\t* Start: %s", ent.start_char)
-        logger.debug("\t\t* End: %s", ent.end_char)
-        logger.debug("\t\t* Label: %s", ent.label_)
+    # 1. Cherche l'action (copier)
+    # 2. Cherche quoi copier (fichier A)
+    # 3. Cherche vers où le copier (fichier B)
 
-    unused = []
-    for i in range(len(doc)):
-        token = doc[i]
-        if token.lemma_ == "copy":
-            for j in range(i, len(doc)):
-                token = doc[j]
-                if token.lemma_ in ["from"]:
-                    from_ = doc[j + 1].text
-                if token.lemma_ in ["to", "in"]:
-                    to_ = doc[j + 1].text
-
-            logger.info("Vous voulez copier %s vers %s", from_, to_)
+    # Quelle commande?
+    command = ""
+    command_index = -1
+    for i, token in enumerate(doc):
+        if token.lemma_ in ("copy", "delete", "rename", "move", "open", "new", "create"):
+            command = token
+            command_index = i
             break
 
-        if token.lemma_ == "delete":
-            what = doc[i + 1]
-            logger.info("Vous voulez supprimer %s", what)
-            break
+    if not command:
+        return "Je n'ai pas compris votre demande"
+    logger.info("Commande: %s", command)
+
+    if command.lemma_ == "copy":
+        args = doc[command_index + 1:]
+        what = ""
+        where = ""
 
-        if token.lemma_ == "rename":
-            logger.info("Vous voulez renommer")
+        for token in args:
+            if not what:
+                if Path(token.text).abspath().isfile():
+                    what = Path(token.text)
+                    continue
+            if not where:
+                if Path(token.text).abspath().isdir():
+                    where = Path(token.text)
+                    continue
 
-        if token.lemma_ == "move":
-            logger.info("Vous voulez déplacer")
+        if not what or not where:
+            return "Je n'ai pas compris votre demande"
+        return "Vous voulez copier '{}' vers '{}'".format(what, where)
 
-        if token.lemma_ == "open":
-            logger.info("Vous voulez ouvrir")
+    if command.lemma_ == "delete":
+        args = doc[command_index + 1:]
+        what = ""
+        for token in args:
+            if Path(token.text).abspath().isfile() or Path(token.text).abspath().isdir():
+                what = Path(token.text)
+                break
 
-        if token.lemma_ == "new":
-            logger.info("Vous voulez créer")
+        if not what:
+            return "Je n'ai pas compris votre demande"
 
-        logger.debug("Connais pas: %s", token.text)
-        unused.append(token)
+        return "Vous voulez supprimer '{}'".format(what)
 
+    if command.lemma_ == "rename":
+        return "Vous voulez renommer"
 
+    if command.lemma_ == "move":
+        return "Vous voulez déplacer"
 
-def analyse(message):
-    doc = nlp(message)
+    if command.lemma_ == "open":
+        return "Vous voulez ouvrir"
 
+    if command.lemma_ == "new":
+        return "Vous voulez créer"
+
+    return "Je n'ai pas compris votre demande"
+
+
+def print_analyse(doc):
     for token in doc:
-        print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
-              token.shape_, token.is_alpha, token.is_stop)
+        logger.debug("\tJeton: %s", token.text)
+        logger.debug("\t\t* Lemma: %s", token.lemma_)
+        logger.debug("\t\t* Position: %s", token.pos_)
+        logger.debug("\t\t* Tag: %s", token.tag_)
+        logger.debug("\t\t* Dependance: %s", token.dep_)
+        logger.debug("\t\t* Forme: %s", token.shape_)
+        logger.debug("\t\t* Alphanum.: %s", token.is_alpha)
+        logger.debug("\t\t* Stop: %s", token.is_stop)
 
     for ent in doc.ents:
-        print(ent.text, ent.start_char, ent.end_char, ent.label_)
-
+        logger.debug("\tEntité: %s", ent.text)
+        logger.debug("\t\t* Start: %s", ent.start_char)
+        logger.debug("\t\t* End: %s", ent.end_char)
+        logger.debug("\t\t* Label: %s", ent.label_)
 
 if __name__ == "__main__":
 
-    submit(r"Copy from .\test\work\a.txt to .\test\work\b.txt")
-    submit(r"Delete .\test\work\a.txt")
+    file_init_a = Path(__file__).parent.parent / "test" / "init" / "a.txt"
+    dir_a = Path(__file__).parent.parent / "test" / "work"
+    file_a = Path(__file__).parent.parent / "test" / "work" / "a.txt"
+    file_b = Path(__file__).parent.parent / "test" / "work" / "b.txt"
+
+    file_init_a.copy(file_a)
+
+    logger.info(submit(r"Copy from {} in {}".format(file_init_a, dir_a)))
+    logger.info(submit(r"Copy {} to {}".format(file_init_a, dir_a)))
+    logger.info(submit(r"Delete {}".format(file_init_a)))
     # submit(r"Rename .\test\work\a.txt in .\test\work\b.txt")