def Parse_Sentence(sentence): pattern1 = re.compile(r'<e1>(.*)</e1>') pattern2 = re.compile(r'<e2>(.*)</e2>') clean_sent = preprocess_sent(sentence) match1 = pattern1.search(clean_sent) match2 = pattern2.search(clean_sent) if match1: entity_e1 = match1.group(1).split() if match2: entity_e2 = match2.group(1).split() sent = clean_sent.replace('<e1>', '').replace('</e1>', '').replace('<e2>', '').replace('</e2>', '') words = sent.split() e1 = words.index(entity_e1[0]) e2 = words.index(entity_e2[0]) # dependency path path = sentence.replace('<e1>', '').replace('</e1>', '').replace( '<e2>', '').replace('</e2>', '').strip('\n').strip('.').strip('\"') try: path = spacy_parser.parse_sent(sent, entity_e1, e1, entity_e2, e2) except Exception as e: print(sentence) # pass return path, e1, e2
__author__ = 'PC-LiNing' import spacy_parser sentence = "Fall planting cannot be completed before the ground freezes , so I stored the happy seeds in a stratification unit until spring" entity_e1 = ['happy', 'seeds'] e1 = 14 entity_e2 = ['stratification', 'unit'] e2 = 18 result = spacy_parser.parse_sent(sentence, entity_e1, e1, entity_e2, e2) print(result)