Repository: google/budoux

Issue:
Add `lang` parameter to JavaScript CLI
Consider to add the `--lang` option to the JavaScript CLI tool similar to #55 


Discussion:

Resolving patch:
diff --git a/budoux/main.py b/budoux/main.py
--- a/budoux/main.py
+++ b/budoux/main.py
@@ -59,14 +59,11 @@ def get_model_langs() -> typing.Dict[str, str]:
       typing.Dict[str, str]: A dictionary of model languages and its paths.
   """
   models = glob.glob(
-      pkg_resources.resource_filename(__name__, "models") + "/*-*.json")
+      pkg_resources.resource_filename(__name__, "models") + "/*.json")
   langs = {}
   for model in models:
-    model_name = model.split(os.sep)[-1][:-5]
-    if model_name.startswith('zh-'):
-      langs[model_name] = model
-    else:
-      langs[model_name[:2]] = model
+    lang = model.split(os.sep)[-1][:-5]
+    langs[lang] = model
   return langs
 
 
@@ -129,7 +126,7 @@ def parse_args(test: ArgList = None) -> argparse.Namespace:
       "--model",
       metavar="JSON",
       type=check_file,
-      default=pkg_resources.resource_filename(__name__, "models/ja-knbc.json"),
+      default=pkg_resources.resource_filename(__name__, "models/ja.json"),
       help="custom model file path",
   )
   model_select_group.add_argument(
diff --git a/budoux/parser.py b/budoux/parser.py
--- a/budoux/parser.py
+++ b/budoux/parser.py
@@ -164,7 +164,7 @@ def load_default_japanese_parser() -> Parser:
   Returns:
     A parser (:obj:`budoux.Parser`).
   """
-  with open(os.path.join(MODEL_DIR, 'ja-knbc.json'), encoding='utf-8') as f:
+  with open(os.path.join(MODEL_DIR, 'ja.json'), encoding='utf-8') as f:
     model = json.load(f)
   return Parser(model)
 


Test patch:
diff --git a/javascript/src/tests/test_cli.ts b/javascript/src/tests/test_cli.ts
--- a/javascript/src/tests/test_cli.ts
+++ b/javascript/src/tests/test_cli.ts
@@ -18,6 +18,7 @@ import {cli} from '../cli.js';
 import {execFile, ExecFileException} from 'child_process';
 import * as path from 'path';
 import stream from 'stream';
+import {loadDefaultParsers} from '../parser.js';
 
 type execFileCallBack = {
   error: ExecFileException | null;
@@ -91,6 +92,53 @@ describe('cli', () => {
     });
   });
 
+  it('should use the corresponding language model when the -l parameter is given.', () => {
+    const inputTextHans = '我们的使命是整合全球信息，供大众使用，让人人受益。';
+    const expectedStdOuts = loadDefaultParsers()
+      .get('zh-hans')!
+      .parse(inputTextHans);
+    const argv = ['node', 'budoux', '-l', 'zh-hans', inputTextHans];
+    cli(argv);
+    expectedStdOuts.forEach(stdout => {
+      expect(console.log).toHaveBeenCalledWith(stdout);
+    });
+  });
+
+  it('should use the corresponding language model when the --lang parameter is given.', () => {
+    const inputTextHans = '我們的使命是匯整全球資訊，供大眾使用，使人人受惠。';
+    const expectedStdOuts = loadDefaultParsers()
+      .get('zh-hant')!
+      .parse(inputTextHans);
+    const argv = ['node', 'budoux', '--lang', 'zh-hant', inputTextHans];
+    cli(argv);
+    expectedStdOuts.forEach(stdout => {
+      expect(console.log).toHaveBeenCalledWith(stdout);
+    });
+  });
+
+  it('should prioritize -m and --model over -l and --lang', () => {
+    const inputTextHans = '我們的使a命';
+    const customModelPath = path.resolve(
+      __dirname,
+      'models',
+      'separate_right_before_a.json'
+    );
+    const argv = [
+      'node',
+      'budoux',
+      '--model',
+      customModelPath,
+      '--lang',
+      'zh-hant',
+      inputTextHans,
+    ];
+    cli(argv);
+    const expectedStdOuts = '我們的使\na命'.split('\n');
+    expectedStdOuts.forEach(stdout => {
+      expect(console.log).toHaveBeenCalledWith(stdout);
+    });
+  });
+
   it('should output the separated sentence with separater when execute budoux command with --delim option.', () => {
     const inputText = '今日は天気です。\n明日は雨かな？';
     const argv = ['node', 'budoux', '--delim', '---', inputText];
diff --git a/tests/test_main.py b/tests/test_main.py
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -72,7 +72,7 @@ def test_cmdargs_invalid_lang_1(self) -> None:
     self.assertEqual(cm.exception.code, 2)
 
   def test_cmdargs_invalid_lang_2(self) -> None:
-    cmdargs = ['-l', 'ja-knbc']
+    cmdargs = ['-l', 'ja-abc']
     with self.assertRaises(SystemExit) as cm:
       main.parse_args(cmdargs)