Skip to content

Commit 430ffcb

Browse files
committed
hxlm (#11): initial draft of hdpcli --objectivum-linguam NNN
1 parent 40f9d79 commit 430ffcb

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

hxlm/core/bin/hdpcli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,10 @@ def execute_cli(self, args,
709709

710710
# print('hdp_filters', hdp_filters)
711711
# print('hdp', hdp)
712-
hdp_rules = hdp.export_yml(hdp_filters)
712+
hdp_rules = hdp.export_yml(hdp_filters, args.objectivum_linguam)
713+
714+
# if 'objectivum_linguam' in args:
715+
713716
print(beautify(hdp_rules, 'yaml'))
714717
# print(hdp.export_yml())
715718
# print('hdp _hdp', hdp._hdp)

hxlm/core/model/hdp.py

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import json
2727
import yaml
2828

29+
from hxlm.core.schema.vocab import (
30+
# HXLM_CORE_SCHEMA_CORE_VOCAB,
31+
ItemHVocab
32+
)
33+
2934

3035
__all__ = ['HDP']
3136

@@ -68,6 +73,8 @@ class HDP:
6873
listed item already is compromised.
6974
"""
7075

76+
_vocab: dict
77+
7178
HDP_JSON_EXTENSIONS: Tuple = (
7279
'.hdp.json',
7380
'.hdpd.json',
@@ -97,6 +104,10 @@ def __init__(self, hdp_entry_point: str = None,
97104
self._online_unrestricted_init = online_unrestricted_init
98105
self._debug = debug
99106

107+
self._vocab = ItemHVocab().to_dict()
108+
109+
# print('self._vocab', self._vocab)
110+
100111
if safer_zone_hosts:
101112
self._safer_zone_hosts = safer_zone_hosts
102113
if safer_zone_list:
@@ -164,7 +175,8 @@ def _update(self, hdp_rules: Union[List, dict],
164175

165176
return True
166177

167-
def _get_filtered(self, hdp_filters: dict = None) -> dict:
178+
def _get_filtered(self, hdp_filters: dict = None,
179+
linguam: str = None) -> dict:
168180
"""Apply filters to HDP complete points to knowledge
169181
170182
Args:
@@ -177,7 +189,7 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict:
177189
filtered = self._hdp
178190

179191
if self._debug:
180-
print('HDP._get_filtered hdp_filters', hdp_filters)
192+
print('HDP._get_filtered hdp_filters', hdp_filters, linguam)
181193

182194
if 'verum_urn' in hdp_filters:
183195
filtered = self._get_filtered_urn(
@@ -193,6 +205,10 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict:
193205
filtered = self._get_filtered_grupum(
194206
filtered, hdp_filters['non_grupum'], False)
195207

208+
# if linguam and filtered and len(filtered):
209+
if linguam:
210+
filtered = self._get_translated(filtered, linguam)
211+
196212
return filtered
197213

198214
def _get_filtered_grupum(self, hdp_current: dict,
@@ -283,6 +299,63 @@ def _get_filtered_urn(self, hdp_current: dict,
283299

284300
return hdp_result
285301

302+
def _get_translated(self, hdp_current: dict, linguam: str) -> dict:
303+
304+
if self._debug:
305+
print('HDP._get_translated', linguam, hdp_current)
306+
# print('HDP._get_translated', self._vocab)
307+
308+
if len(hdp_current) == 0:
309+
return hdp_current
310+
311+
hdp_result = deepcopy(hdp_current)
312+
313+
if len(linguam) != 3:
314+
raise SyntaxError('linguam must be an ISO 639-3 (3 letter) ' +
315+
'code, like "ara" or "rus" [' + linguam + ']')
316+
317+
for hdpns in hdp_current:
318+
319+
# First level
320+
for key_l1 in hdp_current[hdpns]:
321+
if ((key_l1 in self._vocab['root']) and
322+
(linguam in self._vocab['root'][key_l1])): # noqa
323+
newterm = self._vocab['root'][key_l1][linguam]['id']
324+
# print('key_l1 in self._vocab.root', key_l1)
325+
# print('key_l1 in self._vocab.root', newterm) # noqa
326+
hdp_result[hdpns][newterm] = hdp_result[hdpns].pop(key_l1)
327+
hdp_result[hdpns][newterm] = \
328+
self._get_translated_attr(
329+
hdp_result[hdpns][newterm], linguam)
330+
else:
331+
if not str(key_l1).startswith('_'):
332+
hdp_result[hdpns][key_l1] = \
333+
self._get_translated_attr(
334+
hdp_current[hdpns][key_l1], linguam)
335+
# continue
336+
337+
return hdp_result
338+
339+
def _get_translated_attr(self, hdp_current: dict, linguam: str) -> dict:
340+
hdp_result = deepcopy(hdp_current)
341+
342+
# print('oioioioioi2', linguam, type(linguam))
343+
344+
for key_ln in hdp_current:
345+
# print('oioioioioi3', type(key_ln), key_ln)
346+
347+
if not isinstance(key_ln, str):
348+
if self._debug:
349+
print('HDP._get_translated_attr: TODO: fix this', key_ln)
350+
continue
351+
352+
if ((key_ln in self._vocab['attr']) and
353+
(linguam in self._vocab['attr'][key_ln])): # noqa
354+
newterm = self._vocab['attr'][key_ln][linguam]['id']
355+
hdp_result[newterm] = hdp_result.pop(key_ln)
356+
357+
return hdp_result
358+
286359
def _prepare(self, hdp_entry_point: str, is_startup: bool = False) -> bool:
287360

288361
if self._debug:
@@ -540,7 +613,7 @@ def export_json_processing_specs(self, options=None) -> str:
540613

541614
return json.dumps(result, indent=4, sort_keys=True)
542615

543-
def export_yml(self, hdp_filters: dict = None) -> str:
616+
def export_yml(self, hdp_filters: dict = None, linguam: str = None) -> str:
544617
"""Export the current HDP internal metadata in an YAML format
545618
546619
Returns:
@@ -554,7 +627,7 @@ def export_yml(self, hdp_filters: dict = None) -> str:
554627
# if hdp_filters:
555628
# print('TODO hdp_filters', hdp_filters)
556629

557-
result = self._get_filtered(hdp_filters)
630+
result = self._get_filtered(hdp_filters, linguam)
558631

559632
# print('result', result, type(result), yaml.dump(None))
560633
# print('result none', yaml.dump(None))

tests/manual-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --non-grupum hello-world
2323
hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --verum-grupum hello-world
2424
hdpcli tests/hrecipe/yemen-01.hrecipe.hdp.yml --verum-urn yemen
2525
hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --verum-urn hello-world --verum-grupum hello-world
26-
26+
# TODO: implement use this:
27+
hdpcli tests/hrecipe/hello-world.hrecipe.hdp.yml --objectivum-linguam rus
2728

2829
# To inspect the result (pretty print)
2930
hdpcli --export-to-hxl-json-processing-specs tests/hxl-processing-specs/hxl-processing-specs-test-01.hdp.yml

0 commit comments

Comments
 (0)