Created
August 26, 2011 01:47
-
-
Save shirok/1172508 to your computer and use it in GitHub Desktop.
converting json to N-triples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gosh | |
;; A quick-and-dirty hack to convert json to N-triples format. | |
;; Usage: <script-name> x.json y.json ... | |
;; The output is written to stdout. | |
;; NB: JSON itself isn't as strict as other format; especially, | |
;; the meanings of literals and predicates aren't uniquely identifierd | |
;; in the json file (i.e. json object may have an property "title", but | |
;; does it mean <http://purl.org/dc/elements/1.1/title>, or some application- | |
;; specific thingy?) You want to tweak this script so that the output | |
;; will suit your application. | |
(use srfi-1) | |
(use srfi-13) | |
(use rfc.json) | |
(use gauche.sequence) | |
(use gauche.generator) | |
(use file.util) | |
(use util.match) | |
(define *namespace* "http://example.com/#") | |
(define new-blank-node-name | |
(gmap (cut format "_:x~x" <>) (giota +inf.0))) | |
;; returns a list of ntriple lines and the blank-node name | |
(define (alist->ntriples alist) | |
(let1 bnode (new-blank-node-name) | |
(values (append-map (^p (property->ntriple bnode (car p) (cdr p))) alist) | |
bnode))) | |
(define (property->ntriple subj pred obj) | |
(if (vector? obj) | |
(append-map (cut property->ntriple subj pred <>) (vector->list obj)) | |
(receive (extra lit) (object->ntriple obj) | |
(let1 line (format "~a <~a~a> ~a .\n" subj *namespace* pred lit) | |
(cons line extra))))) | |
(define (object->ntriple obj) | |
(cond [(and (exact? obj) (integer? obj)) | |
(values '() (format "~s^^<http://www.w3.org/2001/XMLSchema#integer>" | |
(number->string obj)))] | |
[(real? obj) | |
(values '() (format "~s^^<http://www.w3.org/2001/XMLSchema#double>" | |
(number->string obj)))] | |
[(string? obj) (values '() (escape-nt-string obj))] | |
[(list? obj) (alist->ntriples obj)] | |
[else (error "unsupported literal:" obj)])) | |
;; http://www.w3.org/TR/rdf-testcases/#ntriples | |
(define (escape-nt-string str) | |
(string-concatenate | |
`("\"" | |
,@(map (^c (let1 n (char->ucs c) | |
(cond [(>= n #x10000) (format "\\U~8,'0X" n)] | |
[(>= n #x7f) (format "\\u~4,'0X" n)] | |
[(= n #x09) "\\t"] | |
[(= n #x0a) "\\n"] | |
[(= n #x0d) "\\r"] | |
[(= n #x22) "\\\""] | |
[(= n #x5c) "\\\\"] | |
[(>= n #x20) (string c)] | |
[else (format "\\u~4,'0X" n)]))) | |
str) | |
"\""))) | |
(define (main args) | |
(dolist [json-file (cdr args)] | |
(receive (lines _) | |
(alist->ntriples (parse-json-string (file->string json-file))) | |
(for-each display lines))) | |
0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment