diff options
Diffstat (limited to 'src/interactive.js')
-rw-r--r-- | src/interactive.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/interactive.js b/src/interactive.js new file mode 100644 index 0000000..2ca8687 --- /dev/null +++ b/src/interactive.js @@ -0,0 +1,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();
\ No newline at end of file |