26
26
import json
27
27
import yaml
28
28
29
+ from hxlm .core .schema .vocab import (
30
+ # HXLM_CORE_SCHEMA_CORE_VOCAB,
31
+ ItemHVocab
32
+ )
33
+
29
34
30
35
__all__ = ['HDP' ]
31
36
@@ -68,6 +73,8 @@ class HDP:
68
73
listed item already is compromised.
69
74
"""
70
75
76
+ _vocab : dict
77
+
71
78
HDP_JSON_EXTENSIONS : Tuple = (
72
79
'.hdp.json' ,
73
80
'.hdpd.json' ,
@@ -97,6 +104,10 @@ def __init__(self, hdp_entry_point: str = None,
97
104
self ._online_unrestricted_init = online_unrestricted_init
98
105
self ._debug = debug
99
106
107
+ self ._vocab = ItemHVocab ().to_dict ()
108
+
109
+ # print('self._vocab', self._vocab)
110
+
100
111
if safer_zone_hosts :
101
112
self ._safer_zone_hosts = safer_zone_hosts
102
113
if safer_zone_list :
@@ -164,7 +175,8 @@ def _update(self, hdp_rules: Union[List, dict],
164
175
165
176
return True
166
177
167
- def _get_filtered (self , hdp_filters : dict = None ) -> dict :
178
+ def _get_filtered (self , hdp_filters : dict = None ,
179
+ linguam : str = None ) -> dict :
168
180
"""Apply filters to HDP complete points to knowledge
169
181
170
182
Args:
@@ -177,7 +189,7 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict:
177
189
filtered = self ._hdp
178
190
179
191
if self ._debug :
180
- print ('HDP._get_filtered hdp_filters' , hdp_filters )
192
+ print ('HDP._get_filtered hdp_filters' , hdp_filters , linguam )
181
193
182
194
if 'verum_urn' in hdp_filters :
183
195
filtered = self ._get_filtered_urn (
@@ -193,6 +205,10 @@ def _get_filtered(self, hdp_filters: dict = None) -> dict:
193
205
filtered = self ._get_filtered_grupum (
194
206
filtered , hdp_filters ['non_grupum' ], False )
195
207
208
+ # if linguam and filtered and len(filtered):
209
+ if linguam :
210
+ filtered = self ._get_translated (filtered , linguam )
211
+
196
212
return filtered
197
213
198
214
def _get_filtered_grupum (self , hdp_current : dict ,
@@ -283,6 +299,63 @@ def _get_filtered_urn(self, hdp_current: dict,
283
299
284
300
return hdp_result
285
301
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
+
286
359
def _prepare (self , hdp_entry_point : str , is_startup : bool = False ) -> bool :
287
360
288
361
if self ._debug :
@@ -540,7 +613,7 @@ def export_json_processing_specs(self, options=None) -> str:
540
613
541
614
return json .dumps (result , indent = 4 , sort_keys = True )
542
615
543
- def export_yml (self , hdp_filters : dict = None ) -> str :
616
+ def export_yml (self , hdp_filters : dict = None , linguam : str = None ) -> str :
544
617
"""Export the current HDP internal metadata in an YAML format
545
618
546
619
Returns:
@@ -554,7 +627,7 @@ def export_yml(self, hdp_filters: dict = None) -> str:
554
627
# if hdp_filters:
555
628
# print('TODO hdp_filters', hdp_filters)
556
629
557
- result = self ._get_filtered (hdp_filters )
630
+ result = self ._get_filtered (hdp_filters , linguam )
558
631
559
632
# print('result', result, type(result), yaml.dump(None))
560
633
# print('result none', yaml.dump(None))
0 commit comments