<commit_before>(ns grafter.rdf.io
  "Functions & Protocols for serializing Grafter Statements to (and from)
  any Linked Data format supported by Sesame."
  (:require [clojure.java.io :as io]
            [grafter.rdf.protocols :as pr]
            [grafter.url :refer [->java-uri]]
            [grafter.rdf.protocols :refer [->Quad]]
            [clojure.tools.logging :as log]
            [me.raynes.fs :as fs]
            [pantomime.media :as mime]
            [grafter.url :refer [->url ->grafter-url ->java-uri IURIable ToGrafterURL]])
  (:import (grafter.rdf.protocols IStatement Quad RDFLiteral)
           (grafter.url GrafterURL)
           (java.io File)
           (java.net MalformedURLException URL)
           (java.util GregorianCalendar)
           (javax.xml.datatype DatatypeFactory)
           (org.openrdf.model BNode Literal Resource Statement URI
                              Value)
           (org.openrdf.model.impl BNodeImpl BooleanLiteralImpl
                                   CalendarLiteralImpl
                                   ContextStatementImpl
                                   IntegerLiteralImpl LiteralImpl
                                   NumericLiteralImpl StatementImpl
                                   URIImpl)
           (org.openrdf.repository Repository RepositoryConnection)
           (org.openrdf.rio RDFFormat RDFHandler RDFWriter Rio RDFParserFactory RDFParser)
           (org.openrdf.rio.n3 N3ParserFactory)
           (org.openrdf.rio.nquads NQuadsParserFactory)
           (org.openrdf.rio.ntriples NTriplesParserFactory)
           (org.openrdf.rio.rdfjson RDFJSONParserFactory)
           (org.openrdf.rio.rdfxml RDFXMLParserFactory)
           (org.openrdf.rio.trig TriGParserFactory)
           (org.openrdf.rio.trix TriXParserFactory)
           (org.openrdf.rio.turtle TurtleParserFactory)))

(extend-type Statement
  ;; Extend our IStatement protocol to Sesame's Statements for convenience.
  pr/IStatement
  (subject [this] (.getSubject this))
  (predicate [this] (.getPredicate this))
  (object [this] (.getObject this))
  (context [this] (.getContext this)))

(defprotocol ISesameRDFConverter
  (->sesame-rdf-type [this] "Convert a native type into a Sesame RDF Type")
  (sesame-rdf-type->type [this] "Convert a Sesame RDF Type into a Native Type"))


(defn language
  "Create an RDF langauge string out of a value string and a given
  language tag.  Language tags should be keywords representing the
  country code, e.g.

  (language \"Bonsoir\" :fr)"
  [s lang]
  {:pre [(string? s) (keyword? lang)]}
  (pr/->RDFString s lang))

(defn literal
  "You can use this to declare an RDF typed literal value along with
  its URI.  Note that there are implicit coercions already defined for
  many core clojure/java datatypes, so for common datatypes you
  shounld't need this."

  [val datatype-uri]
  (pr/->RDFLiteral (str val) (->java-uri datatype-uri)))

(defmulti literal-datatype->type
  "A multimethod to convert an RDF literal into a corresponding
  Clojure type.  This method can be extended to provide custom
  conversions."
  (fn [lit]
    (when-let [datatype (pr/datatype-uri lit)]
      (str datatype))))

(defmethod literal-datatype->type nil [literal]
  (s (pr/raw-value literal) (pr/lang literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#boolean" [literal]
  (Boolean/parseBoolean (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#byte" [literal]
  (Byte/parseByte (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#short" [literal]
  (Short/parseShort (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#decimal" [literal]
  ;; Prefer clj's big integer over java's because of hash code issue:
  ;; http://stackoverflow.com/questions/18021902/use-cases-for-bigint-versus-biginteger-in-clojure
  (bigint (java.math.BigInteger. (pr/raw-value literal))))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#double" [literal]
  (Double/parseDouble (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#float" [literal]
  (Float/parseFloat (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#integer" [literal]<commit_msg>fix broken function call s is now language<commit_after>(ns grafter.rdf.io
  "Functions & Protocols for serializing Grafter Statements to (and from)
  any Linked Data format supported by Sesame."
  (:require [clojure.java.io :as io]
            [grafter.rdf.protocols :as pr]
            [grafter.url :refer [->java-uri]]
            [grafter.rdf.protocols :refer [->Quad]]
            [clojure.tools.logging :as log]
            [me.raynes.fs :as fs]
            [pantomime.media :as mime]
            [grafter.url :refer [->url ->grafter-url ->java-uri IURIable ToGrafterURL]])
  (:import (grafter.rdf.protocols IStatement Quad RDFLiteral)
           (grafter.url GrafterURL)
           (java.io File)
           (java.net MalformedURLException URL)
           (java.util GregorianCalendar)
           (javax.xml.datatype DatatypeFactory)
           (org.openrdf.model BNode Literal Resource Statement URI
                              Value)
           (org.openrdf.model.impl BNodeImpl BooleanLiteralImpl
                                   CalendarLiteralImpl
                                   ContextStatementImpl
                                   IntegerLiteralImpl LiteralImpl
                                   NumericLiteralImpl StatementImpl
                                   URIImpl)
           (org.openrdf.repository Repository RepositoryConnection)
           (org.openrdf.rio RDFFormat RDFHandler RDFWriter Rio RDFParserFactory RDFParser)
           (org.openrdf.rio.n3 N3ParserFactory)
           (org.openrdf.rio.nquads NQuadsParserFactory)
           (org.openrdf.rio.ntriples NTriplesParserFactory)
           (org.openrdf.rio.rdfjson RDFJSONParserFactory)
           (org.openrdf.rio.rdfxml RDFXMLParserFactory)
           (org.openrdf.rio.trig TriGParserFactory)
           (org.openrdf.rio.trix TriXParserFactory)
           (org.openrdf.rio.turtle TurtleParserFactory)))

(extend-type Statement
  ;; Extend our IStatement protocol to Sesame's Statements for convenience.
  pr/IStatement
  (subject [this] (.getSubject this))
  (predicate [this] (.getPredicate this))
  (object [this] (.getObject this))
  (context [this] (.getContext this)))

(defprotocol ISesameRDFConverter
  (->sesame-rdf-type [this] "Convert a native type into a Sesame RDF Type")
  (sesame-rdf-type->type [this] "Convert a Sesame RDF Type into a Native Type"))


(defn language
  "Create an RDF langauge string out of a value string and a given
  language tag.  Language tags should be keywords representing the
  country code, e.g.

  (language \"Bonsoir\" :fr)"
  [s lang]
  {:pre [(string? s) (keyword? lang)]}
  (pr/->RDFString s lang))

(defn literal
  "You can use this to declare an RDF typed literal value along with
  its URI.  Note that there are implicit coercions already defined for
  many core clojure/java datatypes, so for common datatypes you
  shounld't need this."

  [val datatype-uri]
  (pr/->RDFLiteral (str val) (->java-uri datatype-uri)))

(defmulti literal-datatype->type
  "A multimethod to convert an RDF literal into a corresponding
  Clojure type.  This method can be extended to provide custom
  conversions."
  (fn [lit]
    (when-let [datatype (pr/datatype-uri lit)]
      (str datatype))))

(defmethod literal-datatype->type nil [literal]
  (language (pr/raw-value literal) (pr/lang literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#boolean" [literal]
  (Boolean/parseBoolean (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#byte" [literal]
  (Byte/parseByte (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#short" [literal]
  (Short/parseShort (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#decimal" [literal]
  ;; Prefer clj's big integer over java's because of hash code issue:
  ;; http://stackoverflow.com/questions/18021902/use-cases-for-bigint-versus-biginteger-in-clojure
  (bigint (java.math.BigInteger. (pr/raw-value literal))))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#double" [literal]
  (Double/parseDouble (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#float" [literal]
  (Float/parseFloat (pr/raw-value literal)))

(defmethod literal-datatype->type "http://www.w3.org/2001/XMLSchema#integer" [literal]
{'fraction_non_alphanumeric': 0.11278883550674902, 'fraction_numerical': 0.01029512697323267, 'mean_word_length': 2.8857777777777778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '649275', 'n_tokens_mistral': 2827, 'n_tokens_neox': 2724, 'n_words': 690}