Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model translator for ICU #115

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()