보자. 분명 RDF 는 막연한 것인가? 아니다. 어제 중앙 도서관에서 제공하는 데이터를 보았다. 그렇다면 Emacs 에서 어떻게 다룰 수 있는가? 여기에 대한 답을 찾아 내자. 패키지가 있었는데 안 보인다.

여기서 가져왔다. 이게 도움이 될 것인가?! 필요한 오퍼레이션은 다음과 같다.

#관련노트

파일 읽고 쓰기

위키데이터 검색하고 가져오기

https://emacsconf.org/2023/talks/collab/ 이 활용법을 보라.

spacemacs : semantic-web layer

~/spacemacs/layers/+lang/semantic-web/packages.el

#Gregoryg #전문가

~/sync/man/dotsamples/vanilla/gregoryg-dotfiles-gpt/README.org ~/sync/man/dotsamples/vanilla/gregoryg-dotfiles-gpt/gjg-functions.org

RDF 를 읽고 쓰는 것이 가능해야 한다.

Org Mode to RDF and (maybe) RDBMS

TODO: return results as property lists rather than positional values use org-entry-get

Code to return (very) simplifed headline parse of current Org buffer

     ;; tags are being used only at file level for now (#+FILETAGS)
     (defun gjg/parse-org-buffer-for-rdf (&optional SIMPLE-LIST)
       "Parse current Org buffer, returning elements for my ontology.
     Set SIMPLE-LIST to t to return ordered list rather than keyed property list."
       (org-element-map (org-element-parse-buffer 'object) '( headline)
         (lambda (hl)
           (let* ((element-type   (car hl))
                  (body           (cadr hl))
                  (pos            (org-element-property :robust-begin hl))
                  (properties     (org-entry-properties pos))
                  (title          (org-element-property :raw-value hl))
                  (level          (org-element-property :level hl))
                  (todo-state     (org-element-property :todo-type hl))
                  (parent         (org-element-property :parent hl))
                  (parent-id      (when parent (org-element-property :ID (assoc 'headline parent))))
                  (id             (org-element-property :ID hl))
                  (category       (cdr (assoc "CATEGORY" properties))) ;; inherited or local
                  (contents-begin (org-element-property :contents-begin hl))
                  (contents-end (org-element-property :contents-end hl)))
             (message "DEBUG Level %d, id %s, parent id %s  \"%s\"" level id parent-id title)
             (setq gort parent)
             ;; TODO nab links in *only* the current heading/topic
             ;; (org-with-point-at contents-begin
             ;;   (org-narrow-to-subtree)
             ;;   (org-fold-hide-subtree)
             ;;   (org-show-entry)
             ;;   (org-element-map (org-element-parse-buffer '(object) t) '(link)
             ;;     (lambda (linky)
             ;;       (message "Ohai: link discovered: %s %s" (plist-get linky :type) (plist-get linky :path)
             ;;     )))
             ;;   (widen)
             ;;   )
             (when title
               (if SIMPLE-LIST
                   (list
                    element-type level title todo-state id parent-id category
                    )
                 (list :type element-type
                       :level level
                       :title title
                       :todo-state todo-state
                       :id id
                       :parent-id parent-id
                       :category category
                       ))
               )))))
     (defun gjg/parse-org-buffer-links-for-rdf ()
       "Parse current Org buffer, returning links for my ontology."
       (setq-local element-type nil)
       (org-element-map (org-element-parse-buffer) '(link headline)
         ;; assume elements will appear in order
         (lambda (hl)
           (if (eq 'headline (car hl))
               (setq-local headline-id (plist-get (cadr hl) :ID))
             (let* ((body         (cadr hl))
                    (element-type (car hl))
                    (link-type    (plist-get body :type))
                    (link-format  (plist-get body :format))
                    (link-path    (plist-get body :path))
                    )
               )
             (when (and element-type (eq 'link element-type))
               (list
                (headline-id link-type link-path link-format)
                )))))
       )

Return string with full RDF set based on my Org Mode ontology

     (defun gjg/generate-rdf-from-org ()
       "Return Turtle format RDF for a graph based on my Org Mode ontology."
       ;; using concat instead of format due to conditional lines
       (let* ((orgparse (gjg/parse-org-buffer-for-rdf))
              (todo-states (delete-dups (mapcar (lambda (hl) (nth 3 hl)) orgparse)))
              (categories (delete-dups (mapcar (lambda (hl) (nth 6 hl)) orgparse)))
              (properties (org-entry-properties 0))
              (alltags (when (and properties (assoc "ALLTAGS" properties))
                         (s-split ":" (substring-no-properties (cdr (assoc "ALLTAGS" properties))) t)))
              (file-name (buffer-file-name))
              (file-id (s-replace "/" "_" file-name))
              (todo-state-data (mapcar
                                (lambda (todo-state)
                                  (format "
     :%s a :Todo . \n" todo-state )) todo-states
                                ))
              (file-level-data (format "
     PREFIX : <urn:orgmode:model#>
     :File-%s a :File ;
          rdfs:label \"%s\" ;
          :file_path \"%s\" ;
          :file_type \"%s\" .\n"
                                       file-id
                                       (file-name-base file-name)
                                       file-name
                                       "org"))
              (filetag-data (mapcar
                             (lambda (tag)
                               (format "
     :Tag-%s a :Tag ;
          rdfs:label \"%s\" .\n
     :File-%s a :File ;
          :file_tag :Tag-%s . \n"
                                       tag
                                       tag
                                       file-id
                                       tag
                                       ))
                             alltags))
              (category-data (mapcar
                              (lambda (cat)
                                (format "
     :Category-%s a :Category ;
     rdfs:label \"%s\" .\n"
                                        cat  cat))
                              categories))
 
              (topic-level-data (mapcar
                                 (lambda (hl)
                                   (let ((level (nth 1 hl))
                                         (title (s-replace "\"" "\\\"" (nth 2 hl)))
                                         (id (nth 4 hl))
                                         (category (nth 6 hl))
                                         (todo-data (when (nth 3 hl) (concat "     :todo_state :" (symbol-name (nth 3 hl))  " ;\n")))
                                         (child-data (when (nth 5 hl) (concat "     :child_of :Topic-" (nth 5 hl)  " ;\n")))
                                         )
                                     (concat "
     :Topic-" id " a :Topic ;\n"
     "     rdfs:label \"" title "\" ;\n"
     "     :topic_level " (int-to-string level) " ;\n"
     "     :topic_headline \"" title "\" ;\n"
     "     :topic_category :Category-" category " ;\n"
     todo-data
     child-data
     "     :from_file :File-" file-id " .\n"
     )
                                     ))
                                 orgparse)
 
                                )
 
              ) ; now we're in the let*
         (concat
          (format "# Data from file %s\n" file-name)
          file-level-data
          "# Unique file tags\n"
          (s-join "\n" filetag-data)
          "# Unique TODO states in file\n"
          (s-join "\n" todo-state-data)
          "# Unique categories in file\n"
          (s-join "\n" category-data)
          "# Topics (Org Mode headlines)\n"
          (s-join "\n" topic-level-data)
          )
         ) ;; end of let*
       )

Write RDF as turtle for directory of Org files

     (defun gjg/write-org-to-rdf (&optional dir)
       "Build RDF and write TURTLE file for all Org Mode files in notes directory.
     Write all TURTLE files to DIR, or `/tmp/' if not specified."
       (interactive)
       (let ((files   (directory-files-recursively "~/gregj-notes/" org-agenda-file-regexp))
             (targetdir (or dir "/tmp/")))
         (dolist (file files)
           (message "Processing file %s" file)
           (let ((buffer (or (find-buffer-visiting file) (find-file-noselect file))))
             (with-current-buffer buffer
               (org-mode)
               (gjg/add-org-header-ids)
               (f-write (gjg/generate-rdf-from-org) 'utf-8 (concat targetdir "/" (file-name-sans-extension (file-name-nondirectory file)) ".ttl")))))))

TTL - Ticky Tacky Lunacy

(use-package ttl-mode :straight t
  :init
  (add-to-list 'auto-mode-alist '("\\.$n3\\|ttl\\|trig$\\'" . ttl-mode))
  :config
  (setq ttl-indent-level 2))

Cypher/OpenCypher for Neo4j

  • Cypher mode
    (use-package cypher-mode :straight t)
  • ob-cypher
    (use-package ob-cypher
      :straight t
      :config
      (add-to-list 'org-babel-load-languages '(cypher . t))
      (org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages)
      (add-to-list 'org-babel-tangle-lang-exts '("cypher" . "cypher")))

SPARQL

     (straight-use-package
      '(sparql-mode :type git :host github :repo "ljos/sparql-mode"
                    :fork (:host github :branch "stardog-babel" :repo "gregoryg/sparql-mode")))
     (add-to-list 'auto-mode-alist '("\\.$sparql\\|rq$\\'" . sparql-mode))
     (load-library "~/projects/emacs/stardog-sparql/stardog-sparql")

FINAL step after all language modes have been configured

     (use-package ob-http :straight t)
     (org-babel-do-load-languages
      'org-babel-load-languages
      (append org-babel-load-languages
              '(
                (sparql . t)
                (clojure . t)
                (http . t) ;; curl
                (js . t)
                (latex . t)
                (lisp . t)
                ;;                (jupyter . t) ;; makes Emacs core dump??? (related to compiled zmq)
                (plantuml . t)
                (lilypond . t)
                (python . t)
                (shell  . t)
                (sql . t)
                )))

#위키데이터: #스콜리아 - #인포그래픽 #서비스

RDF2vec

http://rdf2vec.org/

Related-Notes

References