1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | }));
|
28 | var __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 | });
|
33 | var __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 | };
|
40 | Object.defineProperty(exports, "__esModule", { value: true });
|
41 | exports.cli = void 0;
|
42 | const fs_1 = require("fs");
|
43 | const path = __importStar(require("path"));
|
44 | const readline = __importStar(require("readline"));
|
45 | const commander_1 = require("commander");
|
46 | const index_js_1 = require("./index.js");
|
47 | const CLI_VERSION = '0.6.2';
|
48 | const defaultParsers = (0, index_js_1.loadDefaultParsers)();
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const 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 | };
|
98 | exports.cli = cli;
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | const 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 |
|
128 |
|
129 |
|
130 | const 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 |
|
\ | No newline at end of file |