aboutsummaryrefslogtreecommitdiff
path: root/dump/triples.scm
blob: 1f2ab876a9d8de19e246bfd697f0c06e6ebc9868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(define-module (dump triples)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 match)
  #:use-module (dump utils)
  #:export (ontology
            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 (ontology prefix value)
  (string->symbol
   `,(format #f "~a~a" prefix value)))

(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)))
  (match object
    ((and (?  string? object)
          (?  (lambda (el) (string-match "^\\[ .* \\]$" object))))
     (format #t "~a ~a ~a .~%" subject predicate object))
    ((?  symbol? object)
     (format #t "~a ~a ~a .~%" subject predicate object))
    ((or (?  string? object)
         (?  number? object))
     (format #t "~a ~a ~s .~%" subject predicate object))
    (_ (error "Trible object must be a string, symbol, number or blank node"))))

(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))