Skip to content

Commit

Permalink
hxlm (#11): initial draft of hdpcli --objectivum-linguam NNN
Browse files Browse the repository at this point in the history
  • Loading branch information
fititnt committed Mar 16, 2021
1 parent 40f9d79 commit 430ffcb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
5 changes: 4 additions & 1 deletion hxlm/core/bin/hdpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
81 changes: 77 additions & 4 deletions hxlm/core/model/hdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import json
import yaml

from hxlm.core.schema.vocab import (
# HXLM_CORE_SCHEMA_CORE_VOCAB,
ItemHVocab
)


__all__ = ['HDP']

Expand Down Expand Up @@ -68,6 +73,8 @@ class HDP:
listed item already is compromised.
"""

_vocab: dict

HDP_JSON_EXTENSIONS: Tuple = (
'.hdp.json',
'.hdpd.json',
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion tests/manual-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 430ffcb

Please sign in to comment.