Created
November 28, 2018 02:12
-
-
Save arademaker/fe6b31d25f12eb307ed6cbea4395a357 to your computer and use it in GitHub Desktop.
Wilbur vs RDFLib
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
(ql:quickload '(:yason :wilbur :alexandria) :silent t) | |
(wilbur:add-namespace "du" "http://br.ibm.com/document-understanding/") | |
(defun get-relations (sent) | |
(let ((rels (wilbur:all-values sent '!du:hasRelation))) | |
(mapcar (lambda (rel) | |
(alexandria:plist-hash-table | |
(list "origin" (wilbur:value rel '(:seq !du:origin)) | |
"subject" (wilbur:value rel '(:seq !du:subject)) | |
"predicate" (wilbur:value rel '(:seq !du:predicate)) | |
"object" (wilbur:value rel '(:seq !du:object))))) | |
rels))) | |
(defun get-tokens (sent) | |
(let ((os (wilbur:all-values sent '(:inv !du:sentence))) | |
(res)) | |
(dolist (o os (reverse res)) | |
(let ((type (wilbur:value o '!rdf:type)) | |
(lbl (wilbur:value o '!rdfs:label))) | |
(if (equal type !du:Token) | |
(let ((wn (wilbur:value o '!du:wn30_sense)) | |
(geo (wilbur:value o '!du:geoname_sense))) | |
(if (or wn geo) | |
(push (alexandria:plist-hash-table | |
(list "txt" lbl | |
"type" (wilbur:node-uri type) | |
"wn" (if wn (wilbur:node-uri wn)) | |
"geo" (if geo (wilbur:node-uri geo)))) | |
res))) | |
(push (alexandria:plist-hash-table (list "txt" lbl | |
"type" (wilbur:node-uri type))) | |
res)))))) | |
(defun sentence-json (sent out) | |
(yason:encode (alexandria:plist-hash-table | |
(list | |
"text" (wilbur:value sent '!rdfs:label) | |
"status" (wilbur:value sent '!du:complete) | |
"relations" (get-relations sent) | |
"tokens" (get-tokens sent))) | |
out)) | |
(defun load-data (file) | |
(with-open-file (in file) | |
(setf wilbur:*db* | |
(wilbur:parse-db-from-stream in "http://example.com")))) | |
(defun main (input output) | |
(load-data input) | |
(with-open-file (out output :direction :output :if-exists :supersede) | |
(let ((triples (wilbur:query nil !rdf:type !du:Sentence))) | |
(dolist (tr triples triples) | |
(sentence-json (wilbur:triple-subject tr) out) | |
(format out "~%"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment