gianma commited on
Commit
ec1486c
·
1 Parent(s): 4348196

Update libs/eurovoc/EurovocTh.py

Browse files
Files changed (1) hide show
  1. libs/eurovoc/EurovocTh.py +74 -2
libs/eurovoc/EurovocTh.py CHANGED
@@ -192,7 +192,7 @@ class EurovocTh:
192
  return self.get_dom_label(id)
193
  elif hierarchy_level == "eurovoc_th":
194
  return self.get_th_label(id)
195
- elif hierarchy_level == "eurovoc_classifiers":
196
  return self.get_label(id)
197
  else:
198
  UnknownParameterException(f'level {hierarchy_level} not implemented')
@@ -364,7 +364,7 @@ class EurovocTh:
364
  def get_narrower_ids(self, id:str):
365
  """
366
  :param id: a string of the searched id
367
- :return the set of immediately broader terms for the given id possibly empty
368
  """
369
  try:
370
  return self._relation_nt[id]
@@ -419,6 +419,25 @@ class EurovocTh:
419
 
420
  except KeyError:
421
  return {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
 
423
 
424
  def convert_ids_from_to(self, ids, input_level, target_level):
@@ -458,3 +477,56 @@ class UnknownParameterException(Exception):
458
  #it may also be possible to get terms that are related to another
459
  #but I'm not sure whether it may be usefull or not
460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  return self.get_dom_label(id)
193
  elif hierarchy_level == "eurovoc_th":
194
  return self.get_th_label(id)
195
+ elif hierarchy_level == "eurovoc_classifiers" or hierarchy_level == "eurovoc_classifiers_top_level":
196
  return self.get_label(id)
197
  else:
198
  UnknownParameterException(f'level {hierarchy_level} not implemented')
 
364
  def get_narrower_ids(self, id:str):
365
  """
366
  :param id: a string of the searched id
367
+ :return the set of immediately narrower terms for the given id possibly empty
368
  """
369
  try:
370
  return self._relation_nt[id]
 
419
 
420
  except KeyError:
421
  return {}
422
+
423
+ def get_all_narrower_ids(self, id:str):
424
+ """
425
+ :param id: a string of the searched id
426
+ :retrun the set of all the ids that are narrower (i.e. lower in the hierarchy) for the given id
427
+ """
428
+ try:
429
+ broader_ids = self.get_narrower_ids(id)
430
+
431
+ return_ids = set().union(broader_ids)
432
+
433
+ for b_id in broader_ids:
434
+
435
+ return_ids = return_ids.union(self.get_all_narrower_ids(b_id))
436
+
437
+ return return_ids
438
+
439
+ except KeyError:
440
+ return {}
441
 
442
 
443
  def convert_ids_from_to(self, ids, input_level, target_level):
 
477
  #it may also be possible to get terms that are related to another
478
  #but I'm not sure whether it may be usefull or not
479
 
480
+
481
+
482
+ class EurovocThExt(EurovocTh):
483
+ """ EurovocTh extended class to account also for an additional sink class with 'other' meaning
484
+ this sink class will be the same for all classification levels (same id and label), plus it will be
485
+ mapped in itself when moving between differente levels, additionally it will be considered a leaf and a top
486
+ term in the classification
487
+ """
488
+ def __init__(self, sink_class_id, sink_class_label, language='it', sink_class_desc=None):
489
+
490
+ super().__init__(language)
491
+
492
+ self._sink_class_id = sink_class_id
493
+ self._sink_class_label = sink_class_label
494
+ self._sink_class_desk = sink_class_desc
495
+
496
+ # add the new id and label pairs
497
+ self.__update_language_data()
498
+
499
+ # add relations for other class with itself (we use the same sink class for all the levels)
500
+ self._id_th_relation[sink_class_id].add(sink_class_id)
501
+ self._dom_th_relation[sink_class_id].add(sink_class_id)
502
+ self._th_id_relation[sink_class_id].add(sink_class_id)
503
+
504
+
505
+ def __update_language_data(self):
506
+
507
+ # add the new id and label pairs
508
+ self._labels_dic[self._sink_class_id] = self._sink_class_label
509
+ self._th_labels_dic[self._sink_class_id] = self._sink_class_label
510
+ self._dom_labels_dic[self._sink_class_id] = self._sink_class_label
511
+
512
+ self._label_def[self._sink_class_id] = 'attitional class with meaning other' if not self._sink_class_desk else self._sink_class_desk
513
+
514
+
515
+ def __load_language_data(self, language:str):
516
+ super().__load_language_data(language)
517
+ self.__update_language_data()
518
+
519
+
520
+ def get_leaves(self):
521
+ super().__init__(language)
522
+ self._leaves.add(self._sink_class_id)
523
+ return self._leaves
524
+
525
+
526
+ def get_th_dom(self, id:str):
527
+ if id == self._sink_class_id:
528
+ return self._sink_class_id
529
+ else:
530
+ return super().get_th_dom(id)
531
+
532
+