import en_core_web_lg import fr_core_news_md # @UnusedImport from path import Path from core import logging_ logger = logging_.getLogger("nlp") logger.info("Nestor se réveille...") # nlp = fr_core_news_md.load() 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) # print_analyse(doc) # 1. Cherche l'action (copier) # 2. Cherche quoi copier (fichier A) # 3. Cherche vers où le copier (fichier B) # 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 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 = "" 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 not what or not where: return "Je n'ai pas compris votre demande" return "Vous voulez copier '{}' vers '{}'".format(what, where) 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 not what: return "Je n'ai pas compris votre demande" return "Vous voulez supprimer '{}'".format(what) if command.lemma_ == "rename": return "Vous voulez renommer" if command.lemma_ == "move": return "Vous voulez déplacer" 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: 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: 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__": 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")