summaryrefslogtreecommitdiff
path: root/src/interactive.js
blob: 2ca8687117f892e351bc53d452fb2a1e0de2d043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const chalk = require('chalk');
const readline = require("readline");

commands = {
    exit: () => {
        console.log("Bye!");
        process.exit();
    },
    help: () => {
        console.log("LangDetect Shell Help\n\nYou can use this shell to have an interactive access to all the LangDetect features. It can also be used in environments that disallow using a regular command interpreter.\nHere are all the commands you can use on the LangDetect Shell. Do not enter the '" + chalk.blue(">") + "' in front, it's just to indicate it is on the LangDetect Shell. Arguments are in " + chalk.cyan("{brackets}") + ", use " + chalk.yellow("\"quotes\"") + " to add spaces in arguments\n\n" + chalk.blue(">") + " " + chalk.magenta("status") + "\n    Get info about supported languages and training status\n\n" + chalk.blue(">") + " " + chalk.magenta("train") + " " + chalk.cyan("{text}") + " " + chalk.cyan("{language}") + "\n    Train the system to recognize this " + chalk.cyan("{text}") + " in a specific " + chalk.cyan("{language}") + ".\n        " + chalk.cyan("{text}") + " : The text to make it recognize\n        " + chalk.cyan("{language}") + " : The language code of one of the supported languages\n\n" + chalk.blue(">") + " " + chalk.magenta("detect") + " " + chalk.cyan("{text}") + "\n    Make the system analyze this " + chalk.cyan("{text}") + " and get the language.\n        " + chalk.cyan("{text}") + " : The text to make it analyze\n\n" + chalk.blue(">") + " " + chalk.magenta("learn") + "\n    Make the system learn automatically from text files placed in the /train directory.\n")
    },
    status: (args) => {
        oldv = process.argv;
        process.argv = [oldv[0], oldv[1], ...args];
        try {
            require("./status");
            console.log(chalk.gray("Command succeeded"));
        } catch (e) {
            console.log(chalk.red("Command failed"));
        }
        delete require.cache[require.resolve("./status")];
        process.argv = oldv;
    },
    learn: (args) => {
        oldv = process.argv;
        process.argv = [oldv[0], oldv[1], ...args];
        try {
            require("./auto");
            console.log(chalk.gray("Command succeeded"));
        } catch (e) {
            console.log(chalk.red("Command failed"));
        }
        delete require.cache[require.resolve("./auto")];
        process.argv = oldv;
    },
    detect: (args) => {
        oldv = process.argv;
        process.argv = [oldv[0], oldv[1], ...args];
        try {
            require("./detect");
            console.log(chalk.gray("Command succeeded"));
        } catch (e) {
            console.log(chalk.red("Command failed"));
        }
        delete require.cache[require.resolve("./detect")];
        process.argv = oldv;
    },
    train: (args) => {
        oldv = process.argv;
        process.argv = [oldv[0], oldv[1], ...args];
        try {
            require("./train");
            console.log(chalk.gray("Command succeeded"));
        } catch (e) {
            console.log(chalk.red("Command failed"));
        }
        delete require.cache[require.resolve("./train")];
        process.argv = oldv;
    },
}

function command(data) {
    items = data.split(/ +(?=(?:(?:[^"]*"){2})*[^"]*$)/g);;
    cmd = items[0];
    items.shift();
    args = items;

    if (typeof commands[cmd] != "function") {
        console.log(cmd + ": unknown command");
        prompt()
    } else {
        commands[cmd](args);
        prompt()
    }
}

function prompt() {
    rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.question(chalk.blue("langdetect> "), function(d) {
        rl.close();
        command(d);
    });
}

console.log("Welcome to LangDetect shell! Enter command 'help' for more information.")
prompt();