| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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"):
- if not command:
- command = token
- command_index = i
- break
- else:
- return "Hey! Une commande à la fois svp, je ne suis qu'en beta."
- if not command:
- return "Je n'ai pas compris votre demande"
- logger.info("Commande: %s", command)
- # Détaille les arguments de la commande
- if command.lemma_ == "copy":
- args = doc[command_index + 1:]
- what = ""
- where = ""
- for token in args:
- if not what:
- if Path(token.text).expandvars().isfile():
- # chemin absolu vers un fichier existant
- what = Path(token.text).expandvars()
- continue
- if Path(token.text).expandvars().abspath().isfile():
- # chemin relatif vers un fichier existant
- what = Path(token.text).expandvars().abspath()
- continue
- if Path(token.text).expandvars().isdir():
- # chemin absolu vers un repertoire existant
- what = Path(token.text).expandvars()
- continue
- if Path(token.text).expandvars().abspath().isdir():
- # chemin relatif vers un repertoire existant
- what = Path(token.text).expandvars().abspath()
- continue
- if not where:
- if Path(token.text).expandvars().isdir():
- # la cible est un répertoire existant (chemin absolu)
- where = Path(token.text).expandvars()
- continue
- if Path(token.text).expandvars().abspath().isdir():
- # la cible est un répertoire existant (chemin relatif)
- where = Path(token.text).expandvars().abspath()
- continue
- if Path(token.text).expandvars().parent.isdir():
- # la cible est un répertoire inexistant (chemin absolu), mais son parent existe
- where = Path(token.text).expandvars()
- continue
- if Path(token.text).expandvars().abspath().parent.isdir():
- # la cible est un répertoire inexistant (chemin absolu), mais son parent existe
- where = Path(token.text).expandvars().abspath()
- 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__":
- HERE = Path(__file__).parent
- TEST_DIR = HERE.parent / "test"
- TEST_I_DIR = TEST_DIR / "init"
- TEST_W_DIR = TEST_DIR / "work"
- logger.info(submit(r"Copy from {} in {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR)))
- logger.info(submit(r"Copy {} to {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR)))
- logger.info(submit(r"Copy {} to {}".format(TEST_I_DIR / "a.txt", TEST_W_DIR.relpath(HERE))))
- logger.info(submit(r"Copy {} to {}".format((TEST_I_DIR / "a.txt").relpath(HERE), TEST_W_DIR)))
- logger.info(submit(r"Delete {}".format(TEST_I_DIR / "a.txt")))
- # submit(r"Rename .\test\work\a.txt in .\test\work\b.txt")
|