Spaces:
Sleeping
Sleeping
File size: 1,449 Bytes
9c7387c |
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 29 30 31 32 33 34 35 36 |
from py2neo import Graph, Node, Relationship
import spacy
def extract_knowledge_graph(text, nlp):
"""Extracts entities and relationships and stores them to Neo4j."""
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password")) # Adjust credentials
doc = nlp(text)
for ent in doc.ents:
node = Node("Entity", name=ent.text, label=ent.label_)
graph.create(node)
#This requires more work for the relationship
"""
This needs more work to make the information work.
Example only. More data cleaning needed before real implementation
for token in doc:
# Example: look for verbs connecting entities
if token.dep_ == "ROOT" and token.pos_ == "VERB":
for child in token.children:
if child.dep_ == "nsubj" and child.ent_type_: # Subject is an entity
for obj in token.children:
if obj.dep_ == "dobj" and obj.ent_type_: # Object is an entity
subject_node = Node("Entity", name=child.text, label=child.ent_type_)
object_node = Node("Entity", name=obj.text, label=obj.ent_type_)
relation = Relationship(subject_node, token.text, object_node)
graph.create(relation)
"""
print("Successfully loaded data to the knowledge base.")
# Example Node
print("Create a node called entity.") |