diff --git a/README.md b/README.md index 694bde7f..4163cb40 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,11 @@ For more details of the JavaScript model, please refer to [JavaScript module REA You can also format inputs on your terminal with `budoux` command. ```shellsession -$ budoux 本日は晴天です。 # default: japanese +$ budoux 本日は晴天です。 # default: japanese +本日は +晴天です。 + +$ budoux -l ja 本日は晴天です。 本日は 晴天です。 @@ -137,7 +141,7 @@ options: supported languages of `-l`, `--lang`: - zh-hans -- ja-knbc +- ja ``` ## Caveat diff --git a/budoux/main.py b/budoux/main.py index 96bf617b..5c70ef72 100644 --- a/budoux/main.py +++ b/budoux/main.py @@ -60,14 +60,19 @@ def get_model_langs() -> typing.Dict[str, str]: """ models = glob.glob( pkg_resources.resource_filename(__name__, "models") + "/*-*.json") - return {model.split(os.sep)[-1][:-5]: model for model in models} + langs = {} + for model in models: + model_name = model.split(os.sep)[-1][:-5] + langs[model_name if model_name.startswith('zh-') else model_name[:2]] = model + else: + return langs def check_lang(lang: str) -> str: """Check if given language exists or not. Args: - lang (str): language code (e.g.: 'ja-knbc') + lang (str): language code (e.g.: 'ja') Raises: argparse.ArgumentTypeError: Raise if no model for given language exists. diff --git a/tests/test_main.py b/tests/test_main.py index cc242a4e..9574c61c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -64,19 +64,32 @@ def test_cmdargs_invalid_json(self) -> None: self.assertEqual(cm.exception.code, 2) - def test_cmdargs_invalid_lang(self) -> None: + def test_cmdargs_invalid_lang_1(self) -> None: cmdargs = ['-l', 'aa'] with self.assertRaises(SystemExit) as cm: main.parse_args(cmdargs) self.assertEqual(cm.exception.code, 2) + def test_cmdargs_invalid_lang_2(self) -> None: + cmdargs = ['-l', 'ja-knbc'] + with self.assertRaises(SystemExit) as cm: + main.parse_args(cmdargs) + + self.assertEqual(cm.exception.code, 2) + def test_cmdargs_lang_ja(self) -> None: - cmdargs = ['-l', 'ja-knbc', '今日はいい天気ですね。'] + cmdargs = ['-l', 'ja', '今日はいい天気ですね。'] output = main._main(cmdargs) self.assertEqual(output, '今日は\nいい\n天気ですね。') + def test_cmdargs_lang_ja(self) -> None: + cmdargs = ['-l', 'zh-hans', '今天天气晴朗。'] + output = main._main(cmdargs) + + self.assertEqual(output, '今天天气\n晴朗。') + class TestTextArguments(unittest.TestCase):