From 869737011ac12ca08c80f1c667a7e6285528ff79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20C=C3=A9r=C3=A8s?= Date: Tue, 26 Nov 2019 14:09:17 -0500 Subject: [PATCH 1/3] Ajout fonction d'export en json --- data/alignement.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/data/alignement.py b/data/alignement.py index 77e9e98..65a7474 100644 --- a/data/alignement.py +++ b/data/alignement.py @@ -1,6 +1,7 @@ from fetch import wikidata, wikimedica import requests import rdflib +import json SIGNES_ET_SYMPTOMES_WIKIMEDICA_PAGE = ( 'http://wikimedi.ca/wiki/Sp%C3%A9cial:' @@ -22,10 +23,10 @@ nb_multi_align_possibility = 0 # get name and URI from WikiMedica request_name_uri = """ - SELECT ?name ?uri + SELECT ?name ?wikimedica_uri WHERE { ?el rdfs:label ?name . - ?el rdfs:isDefinedBy ?uri + ?el rdfs:isDefinedBy ?wikimedica_uri } """ @@ -41,10 +42,11 @@ for entity in entitys_dic: # Pre-traitment on name entity['name'] = (entity['name'].split("(")[0]).strip() name = entity['name'] + wikimedica_uri = entity['wikimedica_uri'] print() print("="*10 + " " + name + " " + "="*10) - print(entity['uri']) + print(wikimedica_uri) # use Wikidata_id property request_prop_Wikidata_id = """ @@ -56,7 +58,7 @@ for entity in entitys_dic: """.format(PREFIX_PROPERTY) prop_Wikidata_id = wikimedica.request( - entity['uri'], + wikimedica_uri, request_prop_Wikidata_id ) @@ -85,10 +87,14 @@ for entity in entitys_dic: if len(list_wikidata_pages_uri) == 1: nb_align_by_name = nb_align_by_name + 1 - wikidata_uri = list_wikidata_pages_uri[0] - entity['wikidata_id'] = rdflib.term.URIRef(wikidata_uri) + + entity['wikidata_id'] = ( + list_wikidata_pages_uri[0]['entity']['value'] + ).split("/")[-1] + print("align with 'name', 1 response") - print(wikidata_uri['entity']['value']) + print(entity['wikidata_id']) + elif len(list_wikidata_pages_uri) > 1: nb_multi_align_possibility = nb_multi_align_possibility + 1 print("align with 'name', multi response") @@ -101,3 +107,8 @@ print("nb align by name : ", nb_align_by_name) print("nb multi align possibility : ", nb_multi_align_possibility) print("nb no align : ", nb_wikimedica_entity - nb_align_by_property - nb_align_by_name) + +# export result in file +with open("./data/alignment_result.json", "w") as result_file: + json_content = json.dumps(entitys_dic, sort_keys=True, indent=4) + result_file.write(json_content) From 1c9a590ad123a2ab1451db347645f66824a4c2a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20C=C3=A9r=C3=A8s?= Date: Tue, 26 Nov 2019 14:09:47 -0500 Subject: [PATCH 2/3] =?UTF-8?q?Ajout=20r=C3=A9sultat=20alignement=20automa?= =?UTF-8?q?tiue=20+=20manuel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/alignment_result.json | 495 +++++++++++++++++++++++++++++++++++++ 1 file changed, 495 insertions(+) create mode 100644 data/alignment_result.json diff --git a/data/alignment_result.json b/data/alignment_result.json new file mode 100644 index 0000000..9fb2034 --- /dev/null +++ b/data/alignment_result.json @@ -0,0 +1,495 @@ +[ + { + "name": "Diminution du niveau de perception visuelle", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Diminution_du_niveau_de_perception_visuelle" + }, + { + "name": "Psoas", + "wikidata_id": "Q2042809", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Psoas_-28signe-29" + }, + { + "name": "Selles acholiques", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Selles_acholiques" + }, + { + "name": "Allodynie", + "wikidata_id": "Q1888634", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Allodynie" + }, + { + "name": "Gingivorrhagie", + "wikidata_id": "Q4925918", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Gingivorrhagie" + }, + { + "name": "Becker", + "wikidata_id": "Q2484592", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Becker_-28signe-29" + }, + { + "name": "Acrocyanose", + "wikidata_id": "Q202283", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Acrocyanose" + }, + { + "name": "Tirage", + "wikidata_id": "Q3529328", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Tirage" + }, + { + "name": "H\u00e9moptysies", + "wikidata_id": "Q647099", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/H\u00e9moptysies" + }, + { + "name": "Ataxie", + "wikidata_id": "Q213373", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ataxie_-28signe_clinique-29" + }, + { + "name": "Paralysie flasque", + "wikidata_id": "Q5456583", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Paralysie_flasque" + }, + { + "name": "N\u00e9crose acrale", + "wikidata_id": "Q4675039", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/N\u00e9crose_acrale_-28signe-29" + }, + { + "name": "Fi\u00e8vre", + "wikidata_id": "Q38933", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Fi\u00e8vre" + }, + { + "name": "Embolie pulmonaire", + "wikidata_id": "Q220570", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Embolie_pulmonaire" + }, + { + "name": "Constipation", + "wikidata_id": "Q178436", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Constipation_-28sympt\u00f4me-29" + }, + { + "name": "Souffle art\u00e9riel", + "wikidata_id": "Q3491504", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Souffle_art\u00e9riel" + }, + { + "name": "Dipl\u00e9gie", + "wikidata_id": "Q1097302", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Dipl\u00e9gie" + }, + { + "name": "Hypertonie", + "wikidata_id": "Q574942", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Hypertonie" + }, + { + "name": "Obturateur", + "wikidata_id": "Q7075571", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Obturateur_-28signe-29" + }, + { + "name": "Acouph\u00e8ne", + "wikidata_id": "Q192309", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Acouph\u00e8ne" + }, + { + "name": "D\u00e9lirium", + "wikidata_id": "Q160796", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/D\u00e9lirium" + }, + { + "name": "Tachycardie", + "wikidata_id": "Q209583", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Tachycardie" + }, + { + "name": "Agrammatisme", + "wikidata_id": "Q2423084", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Agrammatisme" + }, + { + "name": "Bezold-Jarisch", + "wikidata_id": "Q854434", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Bezold-2DJarisch_-28r\u00e9flexe-29" + }, + { + "name": "Tiroir ant\u00e9rieur du genou", + "wikidata_id": "Q1277600", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Tiroir_ant\u00e9rieur_du_genou_-28test-29" + }, + { + "name": "D\u00e9fense abdominale involontaire", + "wikidata_id": "Q336216", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/D\u00e9fense_abdominale_involontaire" + }, + { + "name": "Alexithymie", + "wikidata_id": "Q1129046", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Alexithymie" + }, + { + "name": "Ondes X exag\u00e9r\u00e9es", + "wikidata_id": "Q5503705", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ondes_X_exag\u00e9r\u00e9es" + }, + { + "name": "Alogie", + "wikidata_id": "Q1484024", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Alogie" + }, + { + "name": "Avolition", + "wikidata_id": "Q609066", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Avolition" + }, + { + "name": "Brown", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Brown_-28signe_clinique-29" + }, + { + "name": "Respiration agonale", + "wikidata_id": "Q1747437", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Respiration_agonale" + }, + { + "name": "Troisi\u00e8me bruit cardiaque", + "wikidata_id": "Q7785063", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Troisi\u00e8me_bruit_cardiaque" + }, + { + "name": "Syncope", + "wikidata_id": "Q180007", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Syncope" + }, + { + "name": "Exsudat", + "wikidata_id": "Q1139400", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Exsudat" + }, + { + "name": "Imp\u00e9tigo non-bulleux", + "wikidata_id": "Q28971", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Imp\u00e9tigo_non-2Dbulleux" + }, + { + "name": "Hypotonie", + "wikidata_id": "Q1753547", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Hypotonie_-28signe_clinique-29" + }, + { + "name": "Pupilles d'Argyll-Robertson", + "wikidata_id": "Q652568", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Pupilles_d-27Argyll-2DRobertson" + }, + { + "name": "Tachypn\u00e9e", + "wikidata_id": "Q570920", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Tachypn\u00e9e" + }, + { + "name": "Signe de Ballance", + "wikidata_id": "Q4851424", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Signe_de_Ballance" + }, + { + "name": "Agueusie", + "wikidata_id": "Q606216", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Agueusie_-28sympt\u00f4me-29" + }, + { + "name": "Moodley", + "wikidata_id": "Q649122", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Moodley_-28signe-29" + }, + { + "name": "D\u00e9cortication", + "wikidata_id": "Q1938321", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/D\u00e9cortication" + }, + { + "name": "Allen", + "wikidata_id": "Q265634", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Allen_-28test-29" + }, + { + "name": "Rovsing", + "wikidata_id": "Q2003715", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Rovsing_-28signe-29" + }, + { + "name": "Ast\u00e9rixis", + "wikidata_id": "Q748444", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ast\u00e9rixis" + }, + { + "name": "Transsudat", + "wikidata_id": "Q498752", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Transsudat" + }, + { + "name": "Test \u00e0 la toux", + "wikidata_id": "Q2500975", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Test_\u00e0_la_toux" + }, + { + "name": "Thorax en tonneau", + "wikidata_id": "Q1531933", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Thorax_en_tonneau" + }, + { + "name": "Ath\u00e9tose", + "wikidata_id": "Q755524", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ath\u00e9tose" + }, + { + "name": "Adynamie", + "wikidata_id": "Q380203", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Adynamie" + }, + { + "name": "Aaron", + "wikidata_id": "Q4661777", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Aaron_-28signe-29" + }, + { + "name": "Asth\u00e9nie", + "wikidata_id": "Q633403", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Asth\u00e9nie" + }, + { + "name": "Anxi\u00e9t\u00e9", + "wikidata_id": "Q154430", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Anxi\u00e9t\u00e9" + }, + { + "name": "Tripl\u00e9gie", + "wikidata_id": "Q7843546", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Tripl\u00e9gie" + }, + { + "name": "Absence de perception visuelle", + "wikidata_id": "Q737460", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Absence_de_perception_visuelle" + }, + { + "name": "Aphonie", + "wikidata_id": "Q514686", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Aphonie" + }, + { + "name": "Apyrexie", + "wikidata_id": "Q2859076", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Apyrexie" + }, + { + "name": "Echolalie", + "wikidata_id": "Q937353", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Echolalie" + }, + { + "name": "Copropraxie", + "wikidata_id": "Q1428295", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Copropraxie" + }, + { + "name": "McBurney", + "wikidata_id": "Q1253803", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/McBurney_-28signe-29" + }, + { + "name": "Vision floue", + "wikidata_id": "Q4930803", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Vision_floue" + }, + { + "name": "Beeturia", + "wikidata_id": "Q4880222", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Beeturia" + }, + { + "name": "Angor animi", + "wikidata_id": "Q4763844", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Angor_animi" + }, + { + "name": "Comportement automatique", + "wikidata_id": "Q19595989", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Comportement_automatique" + }, + { + "name": "Toux", + "wikidata_id": "Q35805", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Toux" + }, + { + "name": "Barclay", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Barclay_-28signe-29" + }, + { + "name": "Adson", + "wikidata_id": "Q378580", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Adson_-28signe-29" + }, + { + "name": "Douleur pleur\u00e9tique", + "wikidata_id": "Q55998", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Douleur_pleur\u00e9tique" + }, + { + "name": "Nystagmus", + "wikidata_id": "Q220989", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Nystagmus" + }, + { + "name": "Anorexie", + "wikidata_id": "Q254327", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Anorexie_-28sympt\u00f4me-29" + }, + { + "name": "Anisocorie", + "wikidata_id": "Q521953", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Anisocorie" + }, + { + "name": "Anasarque", + "wikidata_id": "Q486485", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Anasarque" + }, + { + "name": "Lignes de Beau", + "wikidata_id": "Q4877328", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Lignes_de_Beau" + }, + { + "name": "Ashrafian", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ashrafian_-28signe_clinique-29" + }, + { + "name": "N\u0153uds de Bouchard", + "wikidata_id": "Q2520898", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/N\u0153uds_de_Bouchard" + }, + { + "name": "Boas", + "wikidata_id": "Q4931474", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Boas_-28signe-29" + }, + { + "name": "Dyspn\u00e9e", + "wikidata_id": "Q188008", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Dyspn\u00e9e" + }, + { + "name": "Abarognosie", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Abarognosie" + }, + { + "name": "Endocardite", + "wikidata_id": "Q82504", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Endocardite" + }, + { + "name": "Fasciite n\u00e9crosante", + "wikidata_id": "Q1145674", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Fasciite_n\u00e9crosante" + }, + { + "name": "La belle indiff\u00e9rence", + "wikidata_id": "Q9347500", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/La_belle_indiff\u00e9rence" + }, + { + "name": "D\u00e9c\u00e9r\u00e9bration", + "wikidata_id": "Q3043006", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/D\u00e9c\u00e9r\u00e9bration" + }, + { + "name": "Man\u0153uvre d'Osler", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Man\u0153uvre_d-27Osler" + }, + { + "name": "Aspermie", + "wikidata_id": "Q736599", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Aspermie" + }, + { + "name": "Pertes sanguines en fin de grossesse", + "wikidata_id": "Q184788", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Pertes_sanguines_en_fin_de_grossesse" + }, + { + "name": "Bradycardie", + "wikidata_id": "Q217111", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Bradycardie" + }, + { + "name": "Mydriase", + "wikidata_id": "Q1128283", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Mydriase" + }, + { + "name": "Arthralgie", + "wikidata_id": "Q683498", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Arthralgie" + }, + { + "name": "Douleur osseuse", + "wikidata_id": "Q848085", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Douleur_osseuse" + }, + { + "name": "Folliculite bact\u00e9rienne", + "wikidata_id": "Q942755", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Folliculite_bact\u00e9rienne" + }, + { + "name": "Hypotension", + "wikidata_id": "Q275419", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Hypotension" + }, + { + "name": "Abrasion", + "wikidata_id": "Q247701", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Abrasion" + }, + { + "name": "Acalculie", + "wikidata_id": "Q415556", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Acalculie" + }, + { + "name": "Hyperthermie", + "wikidata_id": "Q242165", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Hyperthermie" + }, + { + "name": "Souffle d'Austin Flint", + "wikidata_id": "Q780829", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Souffle_d-27Austin_Flint" + }, + { + "name": "Colpitis macularis", + "wikidata_id": "Q7622511", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Colpitis_macularis" + }, + { + "name": "Insuffisance cardiaque", + "wikidata_id": "Q181754", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Insuffisance_cardiaque" + }, + { + "name": "Ecthyma", + "wikidata_id": "Q470701", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Ecthyma" + }, + { + "name": "Lipothymie", + "wikidata_id": "Q18054140", + "wikimedica_uri": "https://wikimedi.ca/wiki/Special:ExportRDF/Lipothymie" + } +] \ No newline at end of file From 0d149bc9e7173c616f345a344ebc3d04d9d9061f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20C=C3=A9r=C3=A8s?= Date: Wed, 27 Nov 2019 00:38:13 -0500 Subject: [PATCH 3/3] build_graph: Mise en place algorithme de construction du graph --- data/build_graph.py | 89 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 data/build_graph.py diff --git a/data/build_graph.py b/data/build_graph.py new file mode 100755 index 0000000..470e3e3 --- /dev/null +++ b/data/build_graph.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +from fetch import wikidata +from neo4j import GraphDatabase + + +NEO4J_URI = "bolt://localhost:7687" +NEO4J_USR = "neo4j" +NEO4J_PSW = "test" + +# Conection with Neo4j +driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USR, NEO4J_PSW)) + + + +def define_link_from_type(link_id): + """ + Define typy of link from ID + + :param link_id: id of link + :return: type corresponding + """ + if link_id in ["P780","P1542"]: + return "Sign_symsymptoms" + elif link_id == "P5642": + return "Risk_factor" + else: + raise Exception("Error : unknow link id: " + link_id) + + + +# Create indexes +with driver.session() as session: + session.run("CREATE INDEX ON :Disease(id);") + session.run("CREATE INDEX ON :Sign_symsymptoms(id);") + session.run("CREATE INDEX ON :Risk_factor(id);") + +# Get all diseases, links, symptoms +request_disease_links = (wikidata.request(""" + SELECT ?maladie ?maladieLabel ?link ?linkLabel ?signe_symptome ?signe_symptomeLabel + WHERE { + ?maladie ?link ?signe_symptome. + SERVICE wikibase:label { bd:serviceParam wikibase:language "fr"}. + VALUES ?link {wdt:P780 wdt:p1542 wdt:P5642} + } + ORDER BY ?maladie +""" +))['results']['bindings'] + +for link in request_disease_links: + disease_id = link["maladie"]["value"].split("/")[-1] + disease_label = link["maladieLabel"]["value"].lower() + disease_type = "Disease" + + link_id = link["link"]["value"].split("/")[-1] + link_label = link["linkLabel"]["value"].lower() + link_type = "link_"+define_link_from_type(link_id) + + signe_symptome_id = link["signe_symptome"]["value"].split("/")[-1] + signe_symptome_label = link["signe_symptomeLabel"]["value"].split("/")[-1].lower() + signe_symptome_type = define_link_from_type(link_id) + + with driver.session() as session: + # add dieadiseases + session.run( + "MERGE (d:" + disease_type + " {id:$disease_id, label:$disease_label})", + disease_id=disease_id, + disease_label=disease_label, + ) + + # add symptoms + session.run( + "MERGE (s:" + signe_symptome_type + " {id:$signe_symptome_id, label:$signe_symptome_label})", + signe_symptome_id=signe_symptome_id, + signe_symptome_label=signe_symptome_label, + ) + + # add link + session.run( + "MATCH (d:" + disease_type + " {id:$disease_id})" + "MATCH (s:" + signe_symptome_type + " {id:$signe_symptome_id})" + "MERGE (d)-[l:" + link_type + " {id:$link_id, label:$link_label}]->(s)", + link_id=link_id, + link_label=link_label, + disease_id=disease_id, + signe_symptome_id=signe_symptome_id + ) + +# Close Neo4j connection +driver.close()