Skip to content

Commit

Permalink
fix: lang
Browse files Browse the repository at this point in the history
zh-hans.json -> zh-hans, ja-knbc.json -> ja
  • Loading branch information
eggplants committed Apr 4, 2022
1 parent 1c03f6e commit a52c8ab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 本日は晴天です。
本日は
晴天です。

Expand Down Expand Up @@ -137,7 +141,7 @@ options:

supported languages of `-l`, `--lang`:
- zh-hans
- ja-knbc
- ja
```

## Caveat
Expand Down
9 changes: 7 additions & 2 deletions budoux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 15 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit a52c8ab

Please sign in to comment.