build_graph: réorganisation du code en fonction

This commit is contained in:
Rémi Cérès 2019-11-27 00:52:21 -05:00
parent 0d149bc9e7
commit ea0be79129
1 changed files with 64 additions and 56 deletions

View File

@ -7,9 +7,6 @@ NEO4J_URI = "bolt://localhost:7687"
NEO4J_USR = "neo4j" NEO4J_USR = "neo4j"
NEO4J_PSW = "test" NEO4J_PSW = "test"
# Conection with Neo4j
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USR, NEO4J_PSW))
def define_link_from_type(link_id): def define_link_from_type(link_id):
@ -28,14 +25,18 @@ def define_link_from_type(link_id):
# Create indexes def create_graph():
with driver.session() as session: """
Build and insert graph from wikidata to neo4j
"""
# Create indexes
with driver.session() as session:
session.run("CREATE INDEX ON :Disease(id);") session.run("CREATE INDEX ON :Disease(id);")
session.run("CREATE INDEX ON :Sign_symsymptoms(id);") session.run("CREATE INDEX ON :Sign_symsymptoms(id);")
session.run("CREATE INDEX ON :Risk_factor(id);") session.run("CREATE INDEX ON :Risk_factor(id);")
# Get all diseases, links, symptoms # Get all diseases, links, symptoms
request_disease_links = (wikidata.request(""" request_disease_links = (wikidata.request("""
SELECT ?maladie ?maladieLabel ?link ?linkLabel ?signe_symptome ?signe_symptomeLabel SELECT ?maladie ?maladieLabel ?link ?linkLabel ?signe_symptome ?signe_symptomeLabel
WHERE { WHERE {
?maladie ?link ?signe_symptome. ?maladie ?link ?signe_symptome.
@ -43,10 +44,10 @@ request_disease_links = (wikidata.request("""
VALUES ?link {wdt:P780 wdt:p1542 wdt:P5642} VALUES ?link {wdt:P780 wdt:p1542 wdt:P5642}
} }
ORDER BY ?maladie ORDER BY ?maladie
""" """
))['results']['bindings'] ))['results']['bindings']
for link in request_disease_links: for link in request_disease_links:
disease_id = link["maladie"]["value"].split("/")[-1] disease_id = link["maladie"]["value"].split("/")[-1]
disease_label = link["maladieLabel"]["value"].lower() disease_label = link["maladieLabel"]["value"].lower()
disease_type = "Disease" disease_type = "Disease"
@ -85,5 +86,12 @@ for link in request_disease_links:
signe_symptome_id=signe_symptome_id signe_symptome_id=signe_symptome_id
) )
# Conection with Neo4j
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USR, NEO4J_PSW))
create_graph()
# Close Neo4j connection # Close Neo4j connection
driver.close() driver.close()