Skip to content

Instantly share code, notes, and snippets.

@pafonta
Created April 27, 2020 15:49
Show Gist options
  • Save pafonta/8232f7c8405ca8800aefbfff2b160f5f to your computer and use it in GitHub Desktop.
Save pafonta/8232f7c8405ca8800aefbfff2b160f5f to your computer and use it in GitHub Desktop.
nlp2 = load_model(MODEL)
nlp2.add_pipe(nlp2.create_pipe("merge_entities"))
nlp2.add_pipe(nlp2.create_pipe("merge_noun_chunks"))
def extract_relations_umls(text, umls):
ents = {x[0].lower(): x[2] for x in umls}
rels = []
for x in nlp2(text):
if x.dep_ != "ROOT":
if x.ent_type_ == "ENTITY":
if x.head.ent_type_ == "ENTITY":
rels.append((x.head.lower_, x.dep_, x.lower_))
else:
if x.head.ent_type_ == "ENTITY":
children = list(x.children)
if children and children[0].ent_type_ == "ENTITY":
rels.append((x.head.lower_, x.dep_, x.lower_))
rels.append((x.lower_, children[0].dep_, children[0].lower_))
return [(ents.get(x, x), y, ents.get(z, z)) for x, y, z in rels]
rels = extract_relations_umls(text, umls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment