Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "599aa68f",
"metadata": {},
"outputs": [],
"source": [
"import IPython.display\n",
"import sys\n",
"import json\n",
"from rdflib import *\n",
"import glob\n",
"import os\n",
"import re\n",
"from SPARQLWrapper import SPARQLWrapper, JSON, BASIC"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a2c8bc92",
"metadata": {},
"outputs": [],
"source": [
"def loadAuthFile():\n",
" with open('auth_add_places.txt') as f:\n",
" auth = f.readlines()\n",
" auth = [x.replace('\\n','') for x in auth]\n",
" return auth"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b00c043b",
"metadata": {},
"outputs": [],
"source": [
"def getSPARQLResults(queryString, auth, scene):\n",
" result = None\n",
" sparql = SPARQLWrapper(auth[0] + \"KGRC4SI_20221208_r2\")\n",
" sparql.setHTTPAuth(BASIC)\n",
" sparql.setCredentials(auth[1], auth[2])\n",
" sparql.setQuery(queryString)\n",
" sparql.setReturnFormat(JSON)\n",
" try :\n",
" json = sparql.query().convert()\n",
" bindings = json['results']['bindings']\n",
" result = bindings\n",
" except Exception as e:\n",
" print(e)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "80a5c729",
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'auth_add_places.txt'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m scene \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mscene2\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 2\u001b[0m auth \u001b[38;5;241m=\u001b[39m \u001b[43mloadAuthFile\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m query_different_locations \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;124m PREFIX : <http://example.org/virtualhome2kg/ontology/>\u001b[39m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124m PREFIX ho: <http://www.owl-ontologies.com/VirtualHome.owl#>\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124m }\u001b[39m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[1;32m 35\u001b[0m query_events_and_places \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;124m PREFIX : <http://example.org/virtualhome2kg/ontology/>\u001b[39m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;124m PREFIX ho: <http://www.owl-ontologies.com/VirtualHome.owl#>\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[38;5;124m }\u001b[39m\n\u001b[1;32m 56\u001b[0m \u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n",
"Cell \u001b[0;32mIn[2], line 2\u001b[0m, in \u001b[0;36mloadAuthFile\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mloadAuthFile\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mauth_add_places.txt\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m 3\u001b[0m auth \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[1;32m 4\u001b[0m auth \u001b[38;5;241m=\u001b[39m [x\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m auth]\n",
"File \u001b[0;32m~/anaconda3/envs/virtual_home/lib/python3.8/site-packages/IPython/core/interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 278\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 279\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 280\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 281\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m )\n\u001b[0;32m--> 284\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'auth_add_places.txt'"
]
}
],
"source": [
"scene = \"scene2\"\n",
"auth = loadAuthFile()\n",
"query_different_locations = \"\"\"\n",
" PREFIX : <http://example.org/virtualhome2kg/ontology/>\n",
" PREFIX ho: <http://www.owl-ontologies.com/VirtualHome.owl#>\n",
" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
" PREFIX ex: <http://example.org/virtualhome2kg/instance/>\n",
" PREFIX ob: <http://raw.githubusercontent.com/aistairc/HomeObjectOntology/main/HomeObject.owl#>\n",
" select distinct ?s ?roomBefore ?roomAfter where { \n",
" ?s\t:action ?action ;\n",
" :mainObject ?object ;\n",
" :agent ?agent ;\n",
" :situationBeforeEvent ?before ;\n",
" :situationAfterEvent ?after .\n",
" ?agentBeforeState :isStateOf ?agent ;\n",
" :partOf ?before ;\n",
" :bbox ?agentBeforeBbox .\n",
" ?agentAfterState :isStateOf ?agent ;\n",
" :partOf ?after ;\n",
" :bbox ?agentAfterBbox .\n",
" ?agentBeforeBbox :inside ?roomBeforeBbox .\n",
" ?agentAfterBbox :inside ?roomAfterBbox .\n",
" ?roomBeforeState :bbox ?roomBeforeBbox ;\n",
" :isStateOf ?roomBefore ;\n",
" :partOf ?before .\n",
" ?roomAfterState :bbox ?roomAfterBbox ;\n",
" :isStateOf ?roomAfter ;\n",
" :partOf ?after .\n",
" ?roomBefore a [rdfs:subClassOf :Room ] .\n",
" ?roomAfter a [rdfs:subClassOf :Room ] .\n",
" filter (?roomBefore != ?roomAfter)\n",
" }\n",
" \"\"\"\n",
"\n",
"query_events_and_places = \"\"\"\n",
" PREFIX : <http://example.org/virtualhome2kg/ontology/>\n",
" PREFIX ho: <http://www.owl-ontologies.com/VirtualHome.owl#>\n",
" PREFIX ob: <http://raw.githubusercontent.com/aistairc/HomeObjectOntology/main/HomeObject.owl#>\n",
" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
" select distinct ?s ?room where { \n",
" {\n",
" ?s :mainObject ?object ;\n",
" :situationBeforeEvent ?before .\n",
" ?state :isStateOf ?object ;\n",
" :partOf ?before ;\n",
" :bbox ?shape .\n",
" ?shape :inside ?roomShape .\n",
" ?roomState :bbox ?roomShape ;\n",
" :isStateOf ?room .\n",
" ?room a [rdfs:subClassOf :Room] .\n",
" } UNION {\n",
" ?s :mainObject ?room .\n",
" ?room a [rdfs:subClassOf :Room] .\n",
" }\n",
" }\n",
" \"\"\"\n",
"json_events_from_to = getSPARQLResults(query_different_locations, auth, scene)\n",
"json_events_and_places = getSPARQLResults(query_events_and_places ,auth, scene)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "44dae077",
"metadata": {},
"outputs": [],
"source": [
"base = Namespace(\"http://example.org/virtualhome2kg/instance/\")\n",
"onto = Namespace(\"http://example.org/virtualhome2kg/ontology/\")\n",
"events_different_locatins = {}\n",
"for result in json_events_from_to:\n",
" event = result[\"s\"][\"value\"]\n",
" from_location = result[\"roomBefore\"][\"value\"]\n",
" to_location = result[\"roomAfter\"][\"value\"]\n",
" events_different_locatins[event] = {\"from\": from_location, \"to\": to_location}\n",
"\n",
"g = Graph()\n",
"for result in json_events_and_places:\n",
" event_uri = result[\"s\"][\"value\"]\n",
" place_uri = result[\"room\"][\"value\"]\n",
" event_r = URIRef(event_uri)\n",
" place_r = URIRef(place_uri)\n",
" if event_uri in events_different_locatins:\n",
" from_location_r = URIRef(events_different_locatins[event_uri][\"from\"])\n",
" to_location_r = URIRef(events_different_locatins[event_uri][\"to\"])\n",
" g.add((event_r, onto[\"from\"], from_location_r))\n",
" g.add((event_r, onto[\"to\"], to_location_r))\n",
" else:\n",
" g.add((event_r, onto.place, place_r))\n",
"\n",
"g.bind(\"ex\", base)\n",
"g.bind(\"vh2kg\", onto)\n",
"output_path = \"../dataset/Dataset_12082022_revised2/rdf20230320/add_places.ttl\"\n",
"g.serialize(destination=output_path, format=\"turtle\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce567114",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading