def get_uri_children(self, uri): """ gets the children for a given uri """ output = [] if isinstance(uri, str): lr = LinkRecursion() lr.get_entity_children(uri, False) children = lr.child_entities if isinstance(children, dict): child_uris = [] for ch_uri, ch_list in children.items(): if uri == ch_uri: child_uris = ch_list break # now we have child entity uris, put them in the proper order lequiv = LinkEquivalence() child_uris = lequiv.get_identifier_list_variants(child_uris) child_ents = LinkEntity.objects\ .filter(uri__in=child_uris)\ .exclude(uri=self.uri)\ .order_by('sort', 'label', 'uri') for act_ent in child_ents: ent_dict = LastUpdatedOrderedDict() ent_dict['id'] = act_ent.uri ent_dict['label'] = act_ent.label ent_dict['slug'] = act_ent.slug ent_dict['href'] = self.make_local_url(act_ent.uri) output.append(ent_dict) return output
def make_reconcilation_json(self, search_term, geojson_ld): """ takes the geojson_ld and makes a reconcilation json output """ recon_json = False if isinstance(geojson_ld, dict): self.geojson_ld = geojson_ld recon_json = [] if 'oc-api:has-facets' in geojson_ld: facets = geojson_ld['oc-api:has-facets'] for facet in facets: if 'oc-api:has-id-options' in facet: max_count = 0 for facet_value in facet['oc-api:has-id-options']: if facet_value['count'] > max_count: max_count = facet_value['count'] id_ranks = {} for facet_value in facet['oc-api:has-id-options']: id_uri = facet_value['rdfs:isDefinedBy'] lr = LinkRecursion() lr.get_entity_children(id_uri) if len(lr.child_entities) > 0: levels = len(lr.child_entities) + 1 else: levels = 1 # calculate a ranking for items, with more specific (fewer children) # categories ranked higher rank = (facet_value['count'] / levels) / max_count match_count = self.count_label_matches( search_term, id_uri) rank = rank + (match_count / levels) id_ranks[id_uri] = rank sorted_ids = sorted(id_ranks.items(), key=operator.itemgetter(1), reverse=True) for id_key, rank in sorted_ids: if len(recon_json) < 5: for facet_value in facet[ 'oc-api:has-id-options']: if facet_value[ 'rdfs:isDefinedBy'] == id_key: rank_item = LastUpdatedOrderedDict() rank_item['id'] = id_key rank_item['label'] = facet_value[ 'label'] rank_item['rank'] = rank rank_item[ 'related-labels'] = self.get_related_labels( id_key, True) recon_json.append(rank_item) break else: break return recon_json
def get_children(self, identifier): """ Gets SKOS or OWL children for an entity """ ent = Entity() found = ent.dereference(identifier) if found: self.children = [] lr = LinkRecursion() lr.get_entity_children(identifier) self.children = lr.child_entities return self.children
def make_reconcilation_json(self, search_term, geojson_ld): """ takes the geojson_ld and makes a reconcilation json output """ recon_json = False if isinstance(geojson_ld, dict): self.geojson_ld = geojson_ld recon_json = [] if 'oc-api:has-facets' in geojson_ld: facets = geojson_ld['oc-api:has-facets'] for facet in facets: if 'oc-api:has-id-options' in facet: max_count = 0 for facet_value in facet['oc-api:has-id-options']: if facet_value['count'] > max_count: max_count = facet_value['count'] id_ranks = {} for facet_value in facet['oc-api:has-id-options']: id_uri = facet_value['rdfs:isDefinedBy'] lr = LinkRecursion() lr.get_entity_children(id_uri) if len(lr.child_entities) > 0: levels = len(lr.child_entities) + 1 else: levels = 1 # calculate a ranking for items, with more specific (fewer children) # categories ranked higher rank = (facet_value['count'] / levels) / max_count match_count = self.count_label_matches(search_term, id_uri) rank = rank + (match_count / levels) id_ranks[id_uri] = rank sorted_ids = sorted(id_ranks.items(), key=operator.itemgetter(1), reverse=True) for id_key, rank in sorted_ids: if len(recon_json) < 5: for facet_value in facet['oc-api:has-id-options']: if facet_value['rdfs:isDefinedBy'] == id_key: rank_item =LastUpdatedOrderedDict() rank_item['id'] = id_key rank_item['label'] = facet_value['label'] rank_item['rank'] = rank rank_item['related-labels'] = self.get_related_labels(id_key, True) recon_json.append(rank_item) break else: break return recon_json
def get_numeric_range_from_children(self, predicate_uuids): """ gets numeric range from a list of predicates OR their children """ if not isinstance(predicate_uuids, list): predicate_uuids = [str(predicate_uuids)] all_preds = [] for predicate_uuid in predicate_uuids: all_preds.append(predicate_uuid) lr = LinkRecursion() lr.get_entity_children(predicate_uuid) for child_uuid_key, val in lr.child_entities.items(): if child_uuid_key not in all_preds: all_preds.append(child_uuid_key) # now get the summary, but don't allow recursive looking at children output = self.get_numeric_range(all_preds, False) return output
def get_entity_children_db(self, entity_uri): """ returns the children of an entity """ children = [] if entity_uri in self.entity_children: children = self.entity_children[entity_uri] else: lr = LinkRecursion() lr.mem_cache_entities = self.entities lr.child_entities = self.entity_children children = lr.get_entity_children(entity_uri) self.entities = lr.mem_cache_entities self.entity_children = lr.child_entities return children
def get_description_tree(self, entity_obj, depth=1, first_time=True, item_type=False, class_uri=False): """ gets a hierarchy for descriptive predicates and types """ lr = LinkRecursion() if entity_obj.item_type == 'projects': tree = self.make_containment_item(entity_obj) if item_type is not False and class_uri is False: # returns the classes associated with an item_type for a project tree['label'] = tree['label'] + ', ' + item_type tree['children'] = self.get_proj_type_classes_items(entity_obj.uuid, item_type) elif item_type is not False and class_uri is not False: # returns the predicates associated with an item_type and class_uri tree['children'] = self.get_proj_type_class_preds(entity_obj.uuid, item_type, class_uri, True) else: # project root, returns the item_types for the project tree['children'] = self.get_proj_types(entity_obj.uuid) if first_time: output = [] output.append(tree) else: output = tree elif entity_obj.item_type == 'predicates': tree = self.make_containment_item(entity_obj) tree['children'] = [] child_list = lr.get_entity_children(entity_obj.uuid, False) if len(child_list) > 0: for child_uuid in child_list: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_containment_children(child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) tree['children'].append(child) elif entity_obj.data_type == 'id': top_types = lr.get_pred_top_rank_types(entity_obj.uuid) for top_type in top_types: uri = top_type['id'] uuid = URImanagement.get_uuid_from_oc_uri(uri) item = False if depth > 1: child_ent = Entity() found = child_ent.dereference(uuid) if found: item = self.get_description_tree(child_ent, depth - 1, False) else: item = LastUpdatedOrderedDict() item['id'] = uuid item['label'] = top_type['label'] item['class_uri'] = 'type' item['class_label'] = 'type' tree['children'].append(item) tree['children'] = self.sort_children_by_label(tree['children']) else: pass if first_time: output = [] output.append(tree) else: output = tree elif entity_obj.item_type == 'types': tree = self.make_containment_item(entity_obj) tree['children'] = [] act_children = lr.get_entity_children(entity_obj.uuid, False) for child_uuid in act_children: if child_uuid != entity_obj.uuid: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_description_tree(child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) child['class_uri'] = 'type' child['class_label'] = 'type' tree['children'].append(child) if len(tree['children']) == 0: tree.pop('children', None) else: tree['children'] = self.sort_children_by_label(tree['children']) if first_time: output = [] output.append(tree) else: output = tree else: output = [] return output
def get_description_tree(self, entity_obj, depth=1, first_time=True, item_type=False, class_uri=False): """ gets a hierarchy for descriptive predicates and types """ lr = LinkRecursion() if entity_obj.item_type == 'projects': tree = self.make_containment_item(entity_obj) if item_type is not False and class_uri is False: # returns the classes associated with an item_type for a project tree['label'] = tree['label'] + ', ' + item_type tree['children'] = self.get_proj_type_classes_items( entity_obj.uuid, item_type) elif item_type is not False and class_uri is not False: # returns the predicates associated with an item_type and class_uri tree['children'] = self.get_proj_type_class_preds( entity_obj.uuid, item_type, class_uri, True) else: # project root, returns the item_types for the project tree['children'] = self.get_proj_types(entity_obj.uuid) if first_time: output = [] output.append(tree) else: output = tree elif entity_obj.item_type == 'predicates': tree = self.make_containment_item(entity_obj) tree['children'] = [] child_list = lr.get_entity_children(entity_obj.uuid, False) if len(child_list) > 0: for child_uuid in child_list: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_containment_children( child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) tree['children'].append(child) elif entity_obj.data_type == 'id': top_types = lr.get_pred_top_rank_types(entity_obj.uuid) for top_type in top_types: uri = top_type['id'] uuid = URImanagement.get_uuid_from_oc_uri(uri) item = False if depth > 1: child_ent = Entity() found = child_ent.dereference(uuid) if found: item = self.get_description_tree( child_ent, depth - 1, False) else: item = LastUpdatedOrderedDict() item['id'] = uuid item['label'] = top_type['label'] item['class_uri'] = 'type' item['class_label'] = 'type' tree['children'].append(item) tree['children'] = self.sort_children_by_label( tree['children']) else: pass if first_time: output = [] output.append(tree) else: output = tree elif entity_obj.item_type == 'types': tree = self.make_containment_item(entity_obj) tree['children'] = [] act_children = lr.get_entity_children(entity_obj.uuid, False) for child_uuid in act_children: if child_uuid != entity_obj.uuid: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_description_tree( child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) child['class_uri'] = 'type' child['class_label'] = 'type' tree['children'].append(child) if len(tree['children']) == 0: tree.pop('children', None) else: tree['children'] = self.sort_children_by_label( tree['children']) if first_time: output = [] output.append(tree) else: output = tree else: output = [] return output