-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |