Skip to content

Commit

Permalink
Add model translator for ICU (#115)
Browse files Browse the repository at this point in the history
tushuhei authored Feb 13, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent af59f9f commit 51a54b9
Showing 2 changed files with 113 additions and 0 deletions.
52 changes: 52 additions & 0 deletions scripts/tests/test_translate_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests the model translator script."""

import os
import sys
import unittest

# module hack
LIB_PATH = os.path.join(os.path.dirname(__file__), '..', '..')
sys.path.insert(0, os.path.abspath(LIB_PATH))

from scripts import translate_model # type: ignore # noqa (module hack)


class TestTranslate(unittest.TestCase):

def test_icu(self) -> None:
model = {'a': {'x': 12, 'y': 88}, 'b': {'x': 47, 'z': 13}}
expect = '''
jaml {
aKeys {
"x",
"y",
}
aValues:intvector {
12,
88,
}
bKeys {
"x",
"z",
}
bValues:intvector {
47,
13,
}
}
'''.strip()
result = translate_model.translate_icu(model)
self.assertEqual(result, expect)
61 changes: 61 additions & 0 deletions scripts/translate_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Translates a model JSON file to another format, such as ICU Resource Bundle."""

import argparse
import json
import typing

ArgList = typing.Optional[typing.List[str]]


def translate_icu(model: typing.Dict[str, typing.Dict[str, int]]) -> str:
"""Translates a model to the ICU Resource Bundle format.
The output is intended to update the data in:
https://github.com/unicode-org/icu/blob/main/icu4c/source/data/brkitr/adaboost/jaml.txt
Args:
model: A model.
Returns:
A model string formatted in the ICU Resource Bundle format.
"""
indent = ' '
output = 'jaml {\n'
for group_name, members in model.items():
output += f'{indent}{group_name}Keys {{\n'
for key in members.keys():
output += f'{indent}{indent}"{key}",\n'
output += f'{indent}}}\n'
output += f'{indent}{group_name}Values:intvector {{\n'
for val in members.values():
output += f'{indent}{indent}{val},\n'
output += f'{indent}}}\n'
output += '}'
return output


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'model', help='File path for the JSON format model file.', type=str)
args = parser.parse_args()
model_path: str = args.model
with open(model_path) as f:
model = json.load(f)
print(translate_icu(model))


if __name__ == '__main__':
main()

0 comments on commit 51a54b9

Please sign in to comment.