Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import spacy
- nlp = spacy.load("en_core_web_sm")
- def extract_claim_triples(text):
- doc = nlp(text)
- claim_triples = []
- for sent in doc.sents:
- subject = None
- verb = None
- obj = None
- for token in sent:
- if token.dep_ == "ROOT" and token.pos_ == "VERB":
- verb = token.text
- for child in token.children:
- if child.dep_ in {"nsubj", "nsubjpass"}:
- subject = child.text
- break
- for child in token.children:
- if child.dep_ in {"dobj", "attr", "acomp"}:
- obj = child.text
- break
- if not subject:
- for token in sent:
- if token.dep_ in {"nsubj", "nsubjpass"}:
- subject = token.text
- break
- if not obj:
- for token in sent:
- if token.dep_ in {"dobj", "attr", "acomp"}:
- obj = token.text
- break
- if subject and verb and obj:
- claim_triples.append((subject, verb, obj))
- return claim_triples
- if __name__ == "__main__":
- sample_text = (
- "The patient exhibits fever. "
- "A high temperature indicates an infection. "
- "The patient also reports a severe cough, which may be a sign of pneumonia."
- )
- claim_triples = extract_claim_triples(sample_text)
- print(claim_triples)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement