def find_all_paths(start, end, level, paths=[], entities=[], relation=None): if level < 0: return [] # if use append, entities will change internally # if use +, a new entities will be created entities = entities + [start] if relation is not None: paths = paths + [relation] if start == end: return [paths] status, relations_from_start = get_direct_relation(start.encode('UTF-8')) if (not status) or (relations_from_start is None): return [] level -= 1 all_paths = [] for r in relations_from_start['result']: if (r['object'] not in entities) and \ (not r['predicate'].startswith('attribute')): temp_paths = find_all_paths(r['object'], end, level, paths, entities, r) for temp_path in temp_paths: if temp_path not in all_paths: all_paths.append(temp_path) return all_paths
def hehe(): xian_triples = [('重庆警方', '击毙', '周克华'), ('重庆警方', '刑事拘留', '张贵英'), ('重庆市沙坪坝区人民法院', '开庭审理', '张贵英'), ('四川舟楫律师事务所的律师', '辩护', '张贵英')] entities = set() relations = [] for triple in xian_triples: entities.add(triple[0]) entities.add(triple[2]) relations.append({ 'subject': triple[0], 'predicate': triple[1], 'object': triple[2], 'flag': 0 }) for e in entities: status, ying_triples = get_direct_relation(e) if status: for triple in ying_triples['result']: if triple['predicate'].startswith('attribute'): continue relations.append({ 'subject': triple['subject'], 'predicate': triple['predicate'], 'object': triple['object'], 'flag': 1 }) return {'result': relations}
def eb_info(entity_name): entities = [] relations = [] text_attrs = [] pic_attrs = [] video_attrs = [] status, result = get_direct_relation(entity_name.encode('UTF-8')) if status and result is not None: for quad in result['result']: temp_subject = quad['subject'] temp_preidicate = quad['predicate'] temp_object = quad['object'] if temp_object not in entities and not \ temp_preidicate.startswith('attribute:'): entities.append(temp_object) if temp_preidicate not in relations and not \ temp_preidicate.startswith('attribute:'): relations.append(temp_preidicate) if temp_preidicate.startswith('attribute:'): if temp_preidicate.startswith('attribute:pic'): pic_attrs.append(temp_object) elif temp_preidicate.startswith('attribute:video'): video_attrs.append(temp_object) else: text_attrs.append(temp_object) return render_template('entity_base/eb_info.html', entity_name=entity_name, entities=entities, relations=relations, text_attrs=text_attrs, pic_attrs=pic_attrs, video_attrs=video_attrs, data=result)
def eb_info(entity_name): entities = [] relations = [] text_attrs = [] pic_attrs = [] video_attrs = [] status, result = get_direct_relation(entity_name.encode("UTF-8")) if status and result is not None: for quad in result["result"]: temp_subject = quad["subject"] temp_preidicate = quad["predicate"] temp_object = quad["object"] if temp_object not in entities and not temp_preidicate.startswith("attribute:"): entities.append(temp_object) if temp_preidicate not in relations and not temp_preidicate.startswith("attribute:"): relations.append(temp_preidicate) if temp_preidicate.startswith("attribute:"): if temp_preidicate.startswith("attribute:pic"): pic_attrs.append(temp_object) elif temp_preidicate.startswith("attribute:video"): video_attrs.append(temp_object) else: text_attrs.append(temp_object) return render_template( "entity_base/eb_info.html", entity_name=entity_name, entities=entities, relations=relations, text_attrs=text_attrs, pic_attrs=pic_attrs, video_attrs=video_attrs, data=result, )