|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 43, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import requests # best library to manage HTTP transactions\n", |
| 10 | + "from bs4 import BeautifulSoup # web-scraping library\n", |
| 11 | + "import json\n", |
| 12 | + "from time import sleep\n", |
| 13 | + "import csv\n", |
| 14 | + "import math\n", |
| 15 | + "from fuzzywuzzy import fuzz # fuzzy logic matching\n", |
| 16 | + "from fuzzywuzzy import process\n", |
| 17 | + "import xml.etree.ElementTree as et # library to traverse XML tree\n", |
| 18 | + "import urllib\n", |
| 19 | + "import datetime\n", |
| 20 | + "import string\n", |
| 21 | + "from pathlib import Path\n", |
| 22 | + "\n", |
| 23 | + "# ---------------\n", |
| 24 | + "# Configuration data\n", |
| 25 | + "# ---------------\n", |
| 26 | + "\n", |
| 27 | + "graph_name = 'http://nursing'\n", |
| 28 | + "accept_media_type = 'text/turtle'\n", |
| 29 | + "sparql_endpoint = \"https://sparql.vanderbilt.edu/sparql\"\n", |
| 30 | + "request_header_dictionary = {\n", |
| 31 | + " #'Content-Type': 'application/sparql-query',\n", |
| 32 | + " 'Accept' : accept_media_type\n", |
| 33 | + "}\n", |
| 34 | + "\n", |
| 35 | + "# Load endpoint password from file in home directory\n", |
| 36 | + "directory = 'home'\n", |
| 37 | + "filename = 'sparql_vanderbilt_edu_password.txt'\n", |
| 38 | + "pwd = load_credential(filename, directory)\n", |
| 39 | + "\n", |
| 40 | + "# ---------------\n", |
| 41 | + "# Function definitions\n", |
| 42 | + "# ---------------\n", |
| 43 | + "\n", |
| 44 | + "# Load password from local drive\n", |
| 45 | + "# value of directory should be either 'home' or 'working'\n", |
| 46 | + "def load_credential(filename, directory):\n", |
| 47 | + " cred = ''\n", |
| 48 | + " # to change the script to look for the credential in the working directory, change the value of home to empty string\n", |
| 49 | + " if directory == 'home':\n", |
| 50 | + " home = str(Path.home()) #gets path to home directory; supposed to work for Win and Mac\n", |
| 51 | + " credential_path = home + '/' + filename\n", |
| 52 | + " else:\n", |
| 53 | + " directory = 'working'\n", |
| 54 | + " credential_path = filename\n", |
| 55 | + " try:\n", |
| 56 | + " with open(credential_path, 'rt', encoding='utf-8') as file_object:\n", |
| 57 | + " cred = file_object.read()\n", |
| 58 | + " except:\n", |
| 59 | + " print(filename + ' file not found - is it in your ' + directory + ' directory?')\n", |
| 60 | + " exit()\n", |
| 61 | + " return(cred)\n", |
| 62 | + "\n", |
| 63 | + "def retrieve_direct_statements(sparql_endpoint):\n", |
| 64 | + " query = '''\n", |
| 65 | + "construct {?item ?directProp ?value.}\n", |
| 66 | + "from <''' + graph_name + '''>\n", |
| 67 | + "where {\n", |
| 68 | + " ?item ?p ?statement.\n", |
| 69 | + " ?statement ?ps ?value.\n", |
| 70 | + " filter(substr(str(?ps),1,39)=\"http://www.wikidata.org/prop/statement/\")\n", |
| 71 | + " bind(substr(str(?ps),40) as ?id)\n", |
| 72 | + " bind(substr(str(?p),30) as ?id)\n", |
| 73 | + " bind(iri(concat(\"http://www.wikidata.org/prop/direct/\", ?id)) as ?directProp)\n", |
| 74 | + " }\n", |
| 75 | + "'''\n", |
| 76 | + " results = []\n", |
| 77 | + " r = requests.get(sparql_endpoint, params={'query' : query}, headers=request_header_dictionary)\n", |
| 78 | + " return r.text\n", |
| 79 | + "\n", |
| 80 | + "def perform_sparql_update(sparql_endpoint, pwd, update_command):\n", |
| 81 | + " # SPARQL Update requires HTTP POST\n", |
| 82 | + " hdr = {'Content-Type' : 'application/sparql-update'}\n", |
| 83 | + " r = requests.post(sparql_endpoint, auth=('admin', pwd), headers=hdr, data = update_command)\n", |
| 84 | + " print(str(r.status_code) + ' ' + r.url)\n", |
| 85 | + " print(r.text)\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 44, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "# ---------------\n", |
| 95 | + "# Construct the direct property statements entailed by the Wikibase model and retrieve from endpoint \n", |
| 96 | + "# ---------------\n", |
| 97 | + "\n", |
| 98 | + "graph_text = retrieve_direct_statements(sparql_endpoint)\n", |
| 99 | + "#print(graph_text)\n", |
| 100 | + "print('constructed triples retrieved')" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 45, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "# remove prefixes from response Turtle, which are not necessary since IRIs are unabbreviated\n", |
| 110 | + "graph_text_list = graph_text.split('\\n')\n", |
| 111 | + "# print(graph_text_list)\n", |
| 112 | + "graph_text = ''\n", |
| 113 | + "for line in graph_text_list:\n", |
| 114 | + " try:\n", |
| 115 | + " if line[0] != '@':\n", |
| 116 | + " graph_text += line + '\\n'\n", |
| 117 | + " except:\n", |
| 118 | + " pass\n", |
| 119 | + "#print()\n", |
| 120 | + "#print(graph_text)" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": 46, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [ |
| 128 | + { |
| 129 | + "name": "stdout", |
| 130 | + "output_type": "stream", |
| 131 | + "text": [ |
| 132 | + "200 https://sparql.vanderbilt.edu/sparql\n", |
| 133 | + "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"><title>blazegraph™ by SYSTAP</title\n", |
| 134 | + "></head\n", |
| 135 | + "><body<p>totalElapsed=1ms, elapsed=1ms, connFlush=0ms, batchResolve=0, whereClause=0ms, deleteClause=0ms, insertClause=0ms</p\n", |
| 136 | + "><hr><p>COMMIT: totalElapsed=356ms, commitTime=1596944443099, mutationCount=776</p\n", |
| 137 | + "></html\n", |
| 138 | + ">\n", |
| 139 | + "\n", |
| 140 | + "done\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "source": [ |
| 145 | + "# Send SPARQL 1.1 UPDATE to endpoint to add the constructed triples into the graph\n", |
| 146 | + "\n", |
| 147 | + "update_command = '''INSERT DATA\n", |
| 148 | + "{ GRAPH <''' + graph_name + '''> { \n", |
| 149 | + "''' + graph_text + '''\n", |
| 150 | + "}}'''\n", |
| 151 | + "\n", |
| 152 | + "#print(update_command)\n", |
| 153 | + "\n", |
| 154 | + "perform_sparql_update(sparql_endpoint, pwd, update_command)\n", |
| 155 | + "\n", |
| 156 | + "print()\n", |
| 157 | + "print('done')" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [] |
| 166 | + } |
| 167 | + ], |
| 168 | + "metadata": { |
| 169 | + "kernelspec": { |
| 170 | + "display_name": "Python 3", |
| 171 | + "language": "python", |
| 172 | + "name": "python3" |
| 173 | + }, |
| 174 | + "language_info": { |
| 175 | + "codemirror_mode": { |
| 176 | + "name": "ipython", |
| 177 | + "version": 3 |
| 178 | + }, |
| 179 | + "file_extension": ".py", |
| 180 | + "mimetype": "text/x-python", |
| 181 | + "name": "python", |
| 182 | + "nbconvert_exporter": "python", |
| 183 | + "pygments_lexer": "ipython3", |
| 184 | + "version": "3.7.1" |
| 185 | + } |
| 186 | + }, |
| 187 | + "nbformat": 4, |
| 188 | + "nbformat_minor": 2 |
| 189 | +} |
0 commit comments