def extract_relations_from_bioc_object(bioc_object, task, id_prefix, denotations): relations = [] known_types = dict( (t['code'], t) for t in get_task_types(task, relation=True)) for b_relation in bioc_object.relations: nodes = list(b_relation.nodes) subj_id = denotations.get(nodes[0].refid, None) obj_id = denotations.get(nodes[1].refid, None) if subj_id is not None and obj_id is not None: relation_type = None b_relation_infons = b_relation.infons.values() for value in b_relation_infons: relation_type = known_types.get(value, None) if relation_type is not None: break relation = { 'id': id_prefix + b_relation.id, 'subj': subj_id, 'obj': obj_id, 'pred': relation_type } if relation_type is None: label_guesses = filter( lambda x: x[0] in ['label', 'type'] or (x[1] != 'None' and x[1] is not None and x[1] != 'undefined'), b_relation.infons.iteritems()) if len(label_guesses) > 0: relation['pred'] = {'label': label_guesses[0][1]} relations.append(relation) return relations
def extract_relations_from_bioc_object(bioc_object, task, id_prefix, denotations): relations = [] known_types = dict((t['code'], t) for t in get_task_types(task, relation=True)) for b_relation in bioc_object.relations: nodes = list(b_relation.nodes) subj_id = denotations.get(nodes[0].refid, None) obj_id = denotations.get(nodes[1].refid, None) if subj_id is not None and obj_id is not None: relation_type = None b_relation_infons = b_relation.infons.values() for value in b_relation_infons: relation_type = known_types.get(value, None) if relation_type is not None: break relation = {'id': id_prefix + b_relation.id, 'subj': subj_id, 'obj': obj_id, 'pred': relation_type } if relation_type is None: label_guesses = filter( lambda x: x[0] in ['label', 'type'] or (x[1] != 'None' and x[1] is not None and x[1] != 'undefined'), b_relation.infons.iteritems()) if len(label_guesses) > 0: relation['pred'] = {'label': label_guesses[0][1]} relations.append(relation) return relations
def extract_denotations_from_bioc_object(bioc_object, task, id_prefix): denotations = [] known_types = dict( (t['code'], t) for t in get_task_types(task, relation=False)) for annotation in bioc_object.annotations: denotation = {'id': id_prefix + annotation.id, 'span': {}} denotation['span']['begin'] = annotation.locations[0].offset denotation['span']['end'] = annotation.locations[ 0].offset + annotation.locations[0].length annotation_info = annotation.infons.values() for value in annotation_info: umls_type = known_types.get(value, None) if umls_type is not None: denotation['obj'] = umls_type break if denotation.get('obj') is None: label_guesses = filter( lambda x: x[0] in ['label', 'type'] or (x[1] != 'None' and x[1] is not None and x[1] != 'undefined'), annotation.infons.iteritems()) if len(label_guesses) > 0: denotation['obj'] = {'label': label_guesses[0][1]} denotations.append(denotation) return denotations
def extract_denotations_from_bioc_object(bioc_object, task, id_prefix): denotations = [] known_types = dict((t['code'], t) for t in get_task_types(task, relation=False)) for annotation in bioc_object.annotations: denotation = {'id': id_prefix + annotation.id, 'span': {}} denotation['span']['begin'] = annotation.locations[0].offset denotation['span']['end'] = annotation.locations[0].offset + annotation.locations[0].length annotation_info = annotation.infons.values() for value in annotation_info: umls_type = known_types.get(value, None) if umls_type is not None: denotation['obj'] = umls_type break if denotation.get('obj') is None: label_guesses = filter( lambda x: x[0] in ['label', 'type'] or (x[1] != 'None' and x[1] is not None and x[1] != 'undefined'), annotation.infons.iteritems()) if len(label_guesses) > 0: denotation['obj'] = {'label': label_guesses[0][1]} denotations.append(denotation) return denotations