UNPKG

5.1 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2021 Google LLC
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18 if (k2 === undefined) k2 = k;
19 var desc = Object.getOwnPropertyDescriptor(m, k);
20 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21 desc = { enumerable: true, get: function() { return m[k]; } };
22 }
23 Object.defineProperty(o, k2, desc);
24}) : (function(o, m, k, k2) {
25 if (k2 === undefined) k2 = k;
26 o[k2] = m[k];
27}));
28var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29 Object.defineProperty(o, "default", { enumerable: true, value: v });
30}) : function(o, v) {
31 o["default"] = v;
32});
33var __importStar = (this && this.__importStar) || function (mod) {
34 if (mod && mod.__esModule) return mod;
35 var result = {};
36 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37 __setModuleDefault(result, mod);
38 return result;
39};
40Object.defineProperty(exports, "__esModule", { value: true });
41exports.cli = void 0;
42const fs_1 = require("fs");
43const path = __importStar(require("path"));
44const readline = __importStar(require("readline"));
45const commander_1 = require("commander");
46const index_js_1 = require("./index.js");
47const CLI_VERSION = '0.6.2';
48const defaultParsers = (0, index_js_1.loadDefaultParsers)();
49/**
50 * Run the command line interface program.
51 * @param argv process.argv.
52 */
53const cli = (argv) => {
54 const program = new commander_1.Command('budoux');
55 program.usage('[-h] [-H] [-d STR] [-t THRES] [-m JSON] [-l LANG] [-V] [TXT]');
56 program.description('BudouX is the successor to Budou, the machine learning powered line break organizer tool.');
57 program
58 .option('-H, --html', 'HTML mode', false)
59 .option('-d, --delim <str>', 'output delimiter in TEXT mode', '---')
60 .option('-m, --model <json>', 'model file path')
61 .option('-l, --lang <str>', `language model to use. -m and --model will be prioritized if any.\navailable languages: ${[
62 ...defaultParsers.keys(),
63 ].join(', ')}`)
64 .argument('[txt]', 'text');
65 program.version(CLI_VERSION);
66 program.parse(argv);
67 const options = program.opts();
68 const { lang, model, delim, html } = options;
69 const { args } = program;
70 const parser = model
71 ? loadCustomParser(model)
72 : lang && defaultParsers.has(lang)
73 ? defaultParsers.get(lang)
74 : (0, index_js_1.loadDefaultJapaneseParser)();
75 switch (args.length) {
76 case 0: {
77 const rl = readline.createInterface({
78 input: process.stdin,
79 });
80 let stdin = '';
81 rl.on('line', line => {
82 stdin += line + '\n';
83 });
84 process.stdin.on('end', () => {
85 outputParsedTexts(parser, html, delim, [stdin]);
86 });
87 break;
88 }
89 case 1: {
90 outputParsedTexts(parser, html, delim, args);
91 break;
92 }
93 default: {
94 throw new Error('Too many arguments. Please, pass the only one argument.');
95 }
96 }
97};
98exports.cli = cli;
99/**
100 * Run the command line interface program.
101 * @param parser A parser.
102 * @param html A flag of html output mode.
103 * @param delim A delimiter to separate output sentence.
104 * @param args string array to parse. Array should have only one element.
105 */
106const outputParsedTexts = (parser, html, delim, args) => {
107 if (html) {
108 const text = args[0];
109 const output = parser.translateHTMLString(text);
110 console.log(output);
111 }
112 else {
113 const splitedTextsByNewLine = args[0]
114 .split(/\r?\n/)
115 .filter(text => text !== '');
116 splitedTextsByNewLine.forEach((text, index) => {
117 const parsedTexts = parser.parse(text);
118 parsedTexts.forEach(parsedText => {
119 console.log(parsedText);
120 });
121 if (index + 1 !== splitedTextsByNewLine.length)
122 console.log(delim);
123 });
124 }
125};
126/**
127 * Loads a parser equipped with custom model.
128 * @return A parser with the loaded model.
129 */
130const loadCustomParser = (modelPath) => {
131 const file = (0, fs_1.readFileSync)(path.resolve(modelPath)).toString();
132 const model = JSON.parse(file);
133 return new index_js_1.HTMLProcessingParser(model);
134};
135//# sourceMappingURL=cli.js.map
\No newline at end of file