maxlath / wikibase-sdk

JS utils functions to query a Wikibase instance and simplify its results
MIT License
325 stars 47 forks source link

simplifySparqlResults: respect numeric datatypes #8

Closed nichtich closed 8 years ago

nichtich commented 8 years ago
    "birth" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
        "type" : "literal",
        "value" : "1903"
      }

should be simplified as

   "birth": 1903

instead of

   "birth": "1903"

I don't know whether there can be other numeric types such as xsd:float (?)

maxlath commented 8 years ago

what is the query that generated this result? so far, the value parsing is based on the type (but is probably incomplete): this could be completed (or replaced?) by a switch on the datatype

nichtich commented 8 years ago

Such results can also come from SPARQL functions for instance COUNT:

$ cat query
SELECT (COUNT(?p) AS ?c) WHERE { wd:Q42 ?p ?v }
$ wdsparql query
{
  "head" : {
    "vars" : [ "c" ]
  },
  "results" : {
    "bindings" : [ {
      "c" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
        "type" : "literal",
        "value" : "432"
      }
    } ]
  }
}
$ wdsparql --simplify query
[
  "432"
]

Looking at https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format I found data types xsd:decimal, xsd:integer, and xsd:dateTime (the latter not numeric) to be expected from Wikidata values. For instance this will get you a xsd:decmial:

SELECT ?e WHERE { wd:Q193 wdt:P1096 ?e }

Using other SPARQL features one can also get xsd:float, xsd:double, and (!) xsd:boolean, for instance:

SELECT (bound(?y) AS ?exists) WHERE {
  wd:Q1 wdt:P580 ?x . OPTIONAL { wd:Q1 wdt:P99999 ?y }
} LIMIT 1

I will submit a pull request do handle these cases!

maxlath commented 8 years ago

solved by 8d340da