UNPKG

wikidata-jskos

Version:
85 lines (71 loc) 2.15 kB
import { sparqlRequest } from "../request.js" import { mappingTypes } from "../types.js" /** * Get total number of Wikidata statements a property is used in. */ async function countPropertyUse (property) { const query = `SELECT (COUNT(*) AS ?count) { [] p:${property} [] }` return sparqlRequest(query).then(result => result[0].count.value) } /** * Get a list of mappings from Wikidata to concepts of toScheme. */ async function getMappingList (args, schemes) { let { fromScheme, toScheme, limit, offset } = args offset ||= 0 limit ||= 1000 // only support mappings from Wikidata if (fromScheme && fromScheme !== "http://bartoc.org/en/node/1940") { return [] } toScheme = schemes.schemes[toScheme] if (!toScheme) { return [] } const prop = toScheme.PROPERTY const totalCount = await countPropertyUse(prop) // build and execute SPARQL query const order = totalCount <= 10000 ? "ORDER BY ?value" : "" const query = ` SELECT ?item ?value ?mappingType ?statement WHERE { ?item p:${prop} ?statement . ?statement ps:${prop} ?value . OPTIONAL { ?statement pq:P4390 ?mappingType } } ${order} LIMIT ${limit} OFFSET ${offset}` const mappings = await sparqlRequest(query) .then(rows => rows.map(row => { const to = toScheme.conceptFromNotation(row.value.value) if (!to) { return } // malformed notation in Wikidata let type if (row.mappingType) { type = mappingTypes[row.mappingType.value.split("/").pop()] } if (!type) { type = "http://www.w3.org/2004/02/skos/core#mappingRelation" } return { uri: row.statement.value, from: { memberSet: [ { uri: row.item.value, } ], }, to: { memberSet: [ to ] }, fromScheme: { uri: "http://bartoc.org/en/node/1940", notation: ["WD"], }, toScheme: { uri: toScheme.uri, notation: toScheme.notation, }, type: [ type ], } }).filter(Boolean)) mappings.totalCount = totalCount return mappings } export default getMappingList