Advertisement
coderchesser

code2

Apr 12th, 2025
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | Source Code | 0 0
  1. import spacy
  2. nlp = spacy.load("en_core_web_sm")
  3.  
  4.  
  5. def extract_claim_triples(text):
  6.     doc = nlp(text)
  7.     claim_triples = []
  8.  
  9.     for sent in doc.sents:
  10.         subject = None
  11.         verb = None
  12.         obj = None
  13.  
  14.         for token in sent:
  15.             if token.dep_ == "ROOT" and token.pos_ == "VERB":
  16.                 verb = token.text
  17.                 for child in token.children:
  18.                     if child.dep_ in {"nsubj", "nsubjpass"}:
  19.                         subject = child.text
  20.                         break
  21.                 for child in token.children:
  22.                     if child.dep_ in {"dobj", "attr", "acomp"}:
  23.                         obj = child.text
  24.                         break
  25.  
  26.         if not subject:
  27.             for token in sent:
  28.                 if token.dep_ in {"nsubj", "nsubjpass"}:
  29.                     subject = token.text
  30.                     break
  31.         if not obj:
  32.             for token in sent:
  33.                 if token.dep_ in {"dobj", "attr", "acomp"}:
  34.                     obj = token.text
  35.                     break
  36.  
  37.         if subject and verb and obj:
  38.             claim_triples.append((subject, verb, obj))
  39.  
  40.     return claim_triples
  41.  
  42.  
  43. if __name__ == "__main__":
  44.     sample_text = (
  45.         "The patient exhibits fever. "
  46.         "A high temperature indicates an infection. "
  47.         "The patient also reports a severe cough, which may be a sign of pneumonia."
  48.     )
  49.     claim_triples = extract_claim_triples(sample_text)
  50.     print(claim_triples)
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement