From 430ffcb1cbced47150ff204d7b94b5175b76f56e Mon Sep 17 00:00:00 2001 From: Emerson Rocha Date: Tue, 16 Mar 2021 02:41:54 -0300 Subject: [PATCH] hxlm (#11): initial draft of hdpcli --objectivum-linguam NNN --- hxlm/core/bin/hdpcli.py | 5 ++- hxlm/core/model/hdp.py | 81 +++++++++++++++++++++++++++++++++++++++-- tests/manual-tests.sh | 3 +- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/hxlm/core/bin/hdpcli.py b/hxlm/core/bin/hdpcli.py index 9af9efd..1445a07 100755 --- a/hxlm/core/bin/hdpcli.py +++ b/hxlm/core/bin/hdpcli.py @@ -709,7 +709,10 @@ def execute_cli(self, args, # print('hdp_filters', hdp_filters) # print('hdp', hdp) - hdp_rules = hdp.export_yml(hdp_filters) + hdp_rules = hdp.export_yml(hdp_filters, args.objectivum_linguam) + + # if 'objectivum_linguam' in args: + print(beautify(hdp_rules, 'yaml')) # print(hdp.export_yml()) # print('hdp _hdp', hdp._hdp) diff --git a/hxlm/core/model/hdp.py b/hxlm/core/model/hdp.py index d0f651c..5c63b03 100644 --- a/hxlm/core/model/hdp.py +++ b/hxlm/core/model/hdp.py @@ -26,6 +26,11 @@ import json import yaml +from hxlm.core.schema.vocab import ( + # HXLM_CORE_SCHEMA_CORE_VOCAB, + ItemHVocab +) + __all__ = ['HDP'] @@ -68,6 +73,8 @@ class HDP: listed item already is compromised. """ + _vocab: dict + HDP_JSON_EXTENSIONS: Tuple = ( '.hdp.json', '.hdpd.json', @@ -97,6 +104,10 @@ def __init__(self, hdp_entry_point: str = None, self._online_unrestricted_init = online_unrestricted_init self._debug = debug + self._vocab = ItemHVocab().to_dict() + + # print('self._vocab', self._vocab) + if safer_zone_hosts: self._safer_zone_hosts = safer_zone_hosts if safer_zone_list: @@ -164,7 +175,8 @@ def _update(self, hdp_rules: Union[List, dict], return True - def _get_filtered(self, hdp_filters: dict = None) -> dict: + def _get_filtered(self, hdp_filters: dict = None, + linguam: str = None) -> dict: """Apply filters to HDP complete points to knowledge Args: @@ -177,7 +189,7 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict: filtered = self._hdp if self._debug: - print('HDP._get_filtered hdp_filters', hdp_filters) + print('HDP._get_filtered hdp_filters', hdp_filters, linguam) if 'verum_urn' in hdp_filters: filtered = self._get_filtered_urn( @@ -193,6 +205,10 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict: filtered = self._get_filtered_grupum( filtered, hdp_filters['non_grupum'], False) + # if linguam and filtered and len(filtered): + if linguam: + filtered = self._get_translated(filtered, linguam) + return filtered def _get_filtered_grupum(self, hdp_current: dict, @@ -283,6 +299,63 @@ def _get_filtered_urn(self, hdp_current: dict, return hdp_result + def _get_translated(self, hdp_current: dict, linguam: str) -> dict: + + if self._debug: + print('HDP._get_translated', linguam, hdp_current) + # print('HDP._get_translated', self._vocab) + + if len(hdp_current) == 0: + return hdp_current + + hdp_result = deepcopy(hdp_current) + + if len(linguam) != 3: + raise SyntaxError('linguam must be an ISO 639-3 (3 letter) ' + + 'code, like "ara" or "rus" [' + linguam + ']') + + for hdpns in hdp_current: + + # First level + for key_l1 in hdp_current[hdpns]: + if ((key_l1 in self._vocab['root']) and + (linguam in self._vocab['root'][key_l1])): # noqa + newterm = self._vocab['root'][key_l1][linguam]['id'] + # print('key_l1 in self._vocab.root', key_l1) + # print('key_l1 in self._vocab.root', newterm) # noqa + hdp_result[hdpns][newterm] = hdp_result[hdpns].pop(key_l1) + hdp_result[hdpns][newterm] = \ + self._get_translated_attr( + hdp_result[hdpns][newterm], linguam) + else: + if not str(key_l1).startswith('_'): + hdp_result[hdpns][key_l1] = \ + self._get_translated_attr( + hdp_current[hdpns][key_l1], linguam) + # continue + + return hdp_result + + def _get_translated_attr(self, hdp_current: dict, linguam: str) -> dict: + hdp_result = deepcopy(hdp_current) + + # print('oioioioioi2', linguam, type(linguam)) + + for key_ln in hdp_current: + # print('oioioioioi3', type(key_ln), key_ln) + + if not isinstance(key_ln, str): + if self._debug: + print('HDP._get_translated_attr: TODO: fix this', key_ln) + continue + + if ((key_ln in self._vocab['attr']) and + (linguam in self._vocab['attr'][key_ln])): # noqa + newterm = self._vocab['attr'][key_ln][linguam]['id'] + hdp_result[newterm] = hdp_result.pop(key_ln) + + return hdp_result + def _prepare(self, hdp_entry_point: str, is_startup: bool = False) -> bool: if self._debug: @@ -540,7 +613,7 @@ def export_json_processing_specs(self, options=None) -> str: return json.dumps(result, indent=4, sort_keys=True) - def export_yml(self, hdp_filters: dict = None) -> str: + def export_yml(self, hdp_filters: dict = None, linguam: str = None) -> str: """Export the current HDP internal metadata in an YAML format Returns: @@ -554,7 +627,7 @@ def export_yml(self, hdp_filters: dict = None) -> str: # if hdp_filters: # print('TODO hdp_filters', hdp_filters) - result = self._get_filtered(hdp_filters) + result = self._get_filtered(hdp_filters, linguam) # print('result', result, type(result), yaml.dump(None)) # print('result none', yaml.dump(None)) diff --git a/tests/manual-tests.sh b/tests/manual-tests.sh index 85d25c2..a9ae6a1 100644 --- a/tests/manual-tests.sh +++ b/tests/manual-tests.sh @@ -23,7 +23,8 @@ hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --non-grupum hello-world hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --verum-grupum hello-world hdpcli tests/hrecipe/yemen-01.hrecipe.hdp.yml --verum-urn yemen hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --verum-urn hello-world --verum-grupum hello-world - +# TODO: implement use this: +hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --objectivum-linguam rus # To inspect the result (pretty print) hdpcli --export-to-hxl-json-processing-specs tests/hxl-processing-specs/hxl-processing-specs-test-01.hdp.yml