Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Last active August 29, 2015 13:56
Show Gist options
  • Save ericmoritz/9290852 to your computer and use it in GitHub Desktop.
Save ericmoritz/9290852 to your computer and use it in GitHub Desktop.
memesearch
.venv/
*~
*.pyc
from rdflib import Graph
import sys
if __name__ == '__main__':
if len(sys.argv) == 3:
file = sys.stdin
in_format = sys.argv[1]
out_format = sys.argv[2]
graph = Graph().parse(file=file, publicID="http://example.com/", format=in_format)
else:
in_uri = sys.argv[1]
in_format = sys.argv[2]
out_format = sys.argv[3]
graph = Graph().parse(in_uri, publicID="http://example.com/", format=in_format)
print graph.serialize(format=out_format),
from pyld import jsonld
import sys
import json
graph = json.load(sys.stdin)
frame = json.load(open(sys.argv[1]))
print json.dumps(jsonld.frame(graph, frame), indent=2)
{
"@context": {
"@vocab": "http://www.w3.org/ns/hydra/core#",
"meme": "http://example.com/vocab/meme#",
"schema": "http://schema.org/",
"search": "meme:search",
"recent": "meme:recent",
"title": "schema:title"
},
"@type": "meme:IndexPage",
"search": {
"@type": "meme:SearchTemplate"
}
}
{
"@context": {
"meme": "http://example.com/vocab/meme#",
"search": "meme:search",
"@vocab": "http://www.w3.org/ns/hydra/core#",
"title": "schema:title",
"recent": "meme:recent",
"schema": "http://schema.org/"
},
"@graph": [
{
"search": {
"@id": "_:b1",
"@type": [
"IriTemplate",
"meme:SearchTemplate"
],
"template": {
"@id": "http://example.com/search{?q}"
},
"mapping": {
"variable": "q",
"property": {
"@id": "http://www.w3.org/ns/hydra/core#freetextQuery"
},
"@id": "_:b2",
"@type": "IriTemplateMapping"
}
},
"title": {
"@value": "Meme Search",
"@language": "en"
},
"@id": "http://example.com/",
"@type": [
"Resource",
"schema:WebPage",
"meme:IndexPage",
"EntryPoint"
],
"recent": {
"member": [
{
"@id": "http://example.com/8WcPWyY"
},
{
"schema:image": {
"@id": "http://example.com/6urDWca.png"
},
"@id": "http://example.com/6urDWca",
"@type": [
"meme:Meme",
"Link",
"meme:RecentMeme",
"schema:CreativeWork"
]
},
{
"@id": "http://example.com/invents-rdf"
}
],
"@id": "_:b0",
"@type": "Collection"
}
}
]
}
@prefix meme: <http://example.com/vocab/meme#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix schema: <http://schema.org/> .
@prefix hydra: <http://www.w3.org/ns/hydra/core#> .
# Index Page
<./>
rdf:type meme:IndexPage ;
rdf:type schema:WebPage ;
rdf:type hydra:Resource ;
schema:title "Meme Search"@en .
# Search Template
<./>
meme:search _:b1 .
_:b1
rdf:type meme:SearchTemplate ;
rdf:type hydra:IriTemplate ;
hydra:mapping _:b2 ;
hydra:template <search{?q}> .
_:b2
rdf:type hydra:IriTemplateMapping ;
hydra:variable "q" ;
hydra:property hydra:freetextQuery .
# Recent Meme
<./>
meme:recent _:b0 .
_:b0
rdf:type hydra:Collection ;
hydra:member <6urDWca> ;
hydra:member <invents-rdf> ;
hydra:member <8WcPWyY> .
<6urDWca>
rdf:type meme:RecentMeme ;
rdf:type meme:Meme ;
rdf:type schema:CreativeWork ;
rdf:type hydra:Link ;
schema:image <6urDWca.png> .
all: index.json
deps:
pip install -r requirements.txt
clean:
rm index.json
index.json:
python convert_rdf.py index.ttl turtle json-ld | python frame_jsonld.py ./index.frame.json > index.json
pyld
rdflib==4.1.0
-e git+https://github.com/RDFLib/rdflib-jsonld.git@e5c4b5f6f4de1e79c93503c0b28d87934a674da1#egg=rdflib_jsonld-master
-e git+https://github.com/edsu/rdflib-microdata.git@2b2eb632a3407bf889d03e1c3dde8657d56c5c6e#egg=rdflib_microdata-master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment