about summary refs log tree commit diff
path: root/dump
diff options
context:
space:
mode:
authorArun Isaac2022-10-30 20:47:37 +0530
committerArun Isaac2022-10-30 23:58:36 +0530
commit25f9279b0078ef0cc93507776c9c8413a0a73a00 (patch)
tree17db69ff002a6f28debd998d281665d4756bd962 /dump
parenta1f21d10c1b21ce054b5750e6d0748ae9ad7557f (diff)
downloadgn-transform-databases-25f9279b0078ef0cc93507776c9c8413a0a73a00.tar.gz
Move triple utilities to new module.
* dump.scm (string->identifier, string-blank?, triple, prefix): Move
to ...
* dump/triples.scm: ... new file.
* dump.scm: Import (dump triples).
Diffstat (limited to 'dump')
-rw-r--r--dump/triples.scm48
1 files changed, 48 insertions, 0 deletions
diff --git a/dump/triples.scm b/dump/triples.scm
new file mode 100644
index 0000000..bb2acdc
--- /dev/null
+++ b/dump/triples.scm
@@ -0,0 +1,48 @@
+(define-module (dump triples)
+  #:use-module (ice-9 match)
+  #:use-module (dump utils)
+  #:export (string->identifier
+            prefix
+            triple
+            scm->triples))
+
+(define (string->identifier prefix str)
+  "Convert STR to a turtle identifier after replacing illegal
+characters with an underscore and prefixing with gn:PREFIX."
+  (string->symbol
+   (string-append "gn:" prefix "_"
+                  (string-map (lambda (c)
+                                (case c
+                                  ((#\/ #\< #\> #\+ #\( #\) #\space #\@) #\_)
+                                  (else c)))
+                              (string-downcase
+                               (string-trim-right str #\.))))))
+
+(define (prefix prefix iri)
+  (format #t "@prefix ~a ~a .~%" prefix iri))
+
+(define (triple subject predicate object)
+  (unless (or (string? subject)
+              (symbol? subject))
+    (error "Triple subject not a string or symbol:"
+           (list subject predicate object)))
+  (unless (or (string? predicate)
+              (symbol? predicate))
+    (error "Triple predicate not a string or symbol:"
+           (list subject predicate object)))
+  (unless (or (string? object)
+              (symbol? object)
+              (number? object))
+    (error "Triple object not a string, symbol or number:"
+           (list subject predicate object)))
+  (format #t "~a ~a ~s .~%" subject predicate object))
+
+(define (scm->triples alist id)
+  (for-each (match-lambda
+              ((predicate . object)
+               (when (cond
+                      ((string? object)
+                       (not (string-blank? object)))
+                      (else object))
+                 (triple id predicate object))))
+            alist))