-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknowledge_graph.py
28 lines (23 loc) · 1.05 KB
/
knowledge_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import spacy
import nlp_tasks as mynlp
import pandas as pd
nlp = spacy.load("en_core_web_sm")
def construct_knowledge_graph(texts):
sentences = []
for text in texts:
coref_text = mynlp.coref_resolution(text)
doc = nlp(coref_text)
sentences.extend([sent.text for sent in doc.sents])
docs = [nlp(sent) for sent in sentences]
knowledge_graph_dict_list = []
for doc in docs:
subjects, objects, main_subj, main_obj = mynlp.get_subjects_and_objects(doc)
relation = mynlp.get_relation(doc)
entities = mynlp.get_entities(doc)
subjects_and_objects = subjects + objects
#if all([not (item is None or item.strip() == '') for item in [main_subj, relation, main_obj]]):
knowledge_graph_dict_list.append(
{'Entity': main_subj, 'Relation': relation, 'Value': main_obj,
'Entity List': entities, 'Subjects and Objects': subjects_and_objects, 'Text Used': doc.text})
knowledge_graph_df = pd.DataFrame(knowledge_graph_dict_list)
return knowledge_graph_df