-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
304 lines (284 loc) · 12.2 KB
/
setup.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import config from "./config/index.js"
import jskos from "jskos-tools"
import { cdk } from "cocoda-sdk"
import fs from "fs/promises"
import { downloadFile } from "./lib/utils.js"
import * as anystream from "json-anystream"
const bartocRegistry = cdk.initializeRegistry(config.schemeRegistry)
const mappingRegistries = config.mappingRegistries.map(registry => cdk.initializeRegistry(registry))
const [,, uri, downloadUrl] = process.argv
// Certain vocabulary downloads are hardcoded
const vocabularyDownloads = {
"http://bartoc.org/en/node/18785": "https://api.dante.gbv.de/export/download/bk/default/bk__default.jskos.ndjson",
}
if (uri && downloadUrl) {
vocabularyDownloads[uri] = downloadUrl
}
function getDownloadUrl(scheme) {
if (!scheme) {
return null
}
if (vocabularyDownloads[scheme.uri]) {
return vocabularyDownloads[scheme.uri]
}
// Try to find distribution (only those hosted on the main coli-conc API for now)
const distribution = scheme.distributions.find(d => d.format === "http://format.gbv.de/jskos" && d.mimetype === "application/x-ndjson; charset=utf-8" && d.download?.startsWith("https://coli-conc.gbv.de/api/"))
if (distribution) {
return distribution.download
}
return null
}
import typesense from "./lib/typesense.js"
function isCombinedConcept(concept) {
return (concept?.type || []).includes("http://rdf-vocabulary.ddialliance.org/xkos#CombinedConcept")
}
/**
* Maps a concept to a document for importing it into Typesense. The document will have the following structure:
*
* {
* id: string (URI),
* concept: unmodified JSKOS concept data,
* identifier: list of strings (URI, identifier, notations),
* prefLabel: list of strings (all preferred labels); empty for combined concepts,
* altLabel: list of strings (all alternative labels); empty for combined concepts,
* mappingLabels: list of strings (labels of mappings for this concept); empty for combined concepts,
* notes: list of strings (notes = scopeNote and editorialNote); empty for combined concepts,
* }
*/
function mapConcept(concept) {
if (!concept || !concept.uri || !concept.prefLabel) {
return null
}
const document = {
id: concept.uri,
concept,
identifier: [concept.uri].concat(concept.identifier || [], concept.notation),
prefLabel: [],
altLabel: [],
mappingLabelsExactClose: [],
mappingLabelsNarrowBroad: [],
mappingLabelsOther: [],
notes: [],
}
if (!isCombinedConcept(concept)) {
document.prefLabel = Object.values(concept.prefLabel)
document.altLabel = [].concat(...Object.values(concept.altLabel || {}))
document.mappingLabelsExactClose = concept._mappings.exactClose.map(mapping => mapping.label).filter(Boolean)
document.mappingLabelsNarrowBroad = concept._mappings.narrowBroad.map(mapping => mapping.label).filter(Boolean)
document.mappingLabelsOther = concept._mappings.other.map(mapping => mapping.label).filter(Boolean)
document.notes = [].concat(...Object.values(concept.scopeNote || {}), ...Object.values(concept.editorialNote || {}))
}
return document
}
const mappingTypeMap = {
"http://www.w3.org/2004/02/skos/core#mappingRelation": "other",
"http://www.w3.org/2004/02/skos/core#closeMatch": "exactClose",
"http://www.w3.org/2004/02/skos/core#exactMatch": "exactClose",
"http://www.w3.org/2004/02/skos/core#broadMatch": "narrowBroad",
"http://www.w3.org/2004/02/skos/core#narrowMatch": "narrowBroad",
"http://www.w3.org/2004/02/skos/core#relatedMatch": "other",
}
// SQLite database used to cache concept data loaded for mappings
import Database from "better-sqlite3"
if (uri) {
main(uri)
} else {
if (!config.schemes?.length) {
console.error("Neither URI (as param) nor `config.schemes` are given, nothing to import.")
process.exit(1)
}
;(async () => {
for (const uri of config.schemes) {
await main(uri)
}
})()
}
async function main(uri) {
// Init all registries
console.log("Initialize all registries...")
await Promise.all(mappingRegistries.concat(bartocRegistry).map(registry => registry.init()))
console.log("... all registries initialized.")
const schemes = await bartocRegistry.getSchemes({ params: { limit: 3000 }})
console.log(`Loaded ${schemes.length} compatible vocabularies.`)
const scheme = schemes.find(s => jskos.compare(s, { uri }))
const notation = jskos.notation(scheme)
console.log()
if (!notation) {
console.error(`Vocabulary with URI ${uri} not compatible, not importing.`)
console.error()
return
}
console.log(`##### Importing ${notation} #####`)
// Download all concepts of scheme, if necessary
const conceptsFile = `${config.cache}/${notation}-concepts.ndjson`
try {
// Check if file exists
await fs.stat(conceptsFile)
console.log(`Using already downloaded concept data for ${notation} in ${conceptsFile}.`)
} catch (error) {
// If not, download the data
const download = getDownloadUrl(scheme)
if (!download) {
console.error(`No download URL available for ${notation}. Download URLs for ndjson data are retrieved from BARTOC, but can also be given as the second parameter.`)
process.exit(1)
}
console.log(`Downloading ${notation} from ${download}...`)
await downloadFile(download, conceptsFile)
console.log("... download of concept data done.")
}
// Download mapping data
// Note: We're not using cocoda-sdk here because we want to download the whole file.
// TODO: Once everything works, we want to redownload the mappings to update the data.
await Promise.all(mappingRegistries.map(async (registry, index) => {
const mappingFile = `${config.cache}/${notation}-mappings-${index}.ndjson`
try {
// Check if file exists
await fs.stat(mappingFile)
console.log(`Using already downloaded mapping data for ${notation} in ${mappingFile} (${index}).`)
} catch (error) {
// If not, download the data
const download = `${registry._api.mappings}?properties=annotations&toScheme=${encodeURIComponent(scheme.uri)}&direction=both&download=ndjson`
console.log(`Downloading ${notation} mappings from ${download} (${index})...`)
await downloadFile(download, mappingFile)
console.log(`... download of mappings done (${index}).`)
}
}))
// Load concept data into memory
console.log(`Loading concept data from ${conceptsFile} into memory...`)
const conceptData = {}
const conceptDataStream = await anystream.make(conceptsFile)
for await (const concept of conceptDataStream) {
conceptData[concept.uri] = concept
concept._mappings = {
exactClose: [],
narrowBroad: [],
other: [],
}
}
console.log(`... concept data loaded (${Object.keys(conceptData).length} concepts).`)
// Attaching mappings to concept data
await Promise.all(mappingRegistries.map(async (registry, index) => {
const mappingFile = `${config.cache}/${notation}-mappings-${index}.ndjson`
console.log(`Loading mappings data from ${mappingFile} into the concept data (${index})...`)
const mappingDataStream = await anystream.make(mappingFile)
let count = 0
for await (const mapping of mappingDataStream) {
count += 1
if (!mapping) {
continue
}
// Make sure mapping type is given
if (!mapping.type?.[0]) {
mapping.type = ["http://www.w3.org/2004/02/skos/core#mappingRelation"]
}
const side = jskos.compare(mapping.fromScheme, scheme) ? "from" : "to"
const otherSide = side === "from" ? "to" : "from"
const otherScheme = mapping[`${otherSide}Scheme`]
// Find concept(s) in our scheme
const concepts = jskos.conceptsOfMapping(mapping, side).map(({ uri }) => conceptData[uri]).filter(Boolean)
// Find other concepts
const otherConcepts = jskos.conceptsOfMapping(mapping, otherSide).map(({ uri }) => ({ uri, inScheme: [{ uri: otherScheme.uri }]}))
// Attach otherConcepts to all concepts
concepts.forEach(concept => {
// We're attaching the concepts in the `mappings` field, but that's okay. 😅
concept._mappings[mappingTypeMap[mapping.type[0]]] = concept._mappings[mappingTypeMap[mapping.type[0]]].concat(otherConcepts)
})
}
console.log(`... mapping data loaded (${count} mappings, ${index}).`)
}))
// Prepare SQLite database for mapping concept cache
const db = new Database(config.database)
db.pragma("journal_mode = WAL")
db.prepare(`CREATE TABLE IF NOT EXISTS mapping_concepts (
uri TEXT PRIMARY KEY,
label TEXT
)`).run()
db.prepare(`CREATE TABLE IF NOT EXISTS schemes (
key TEXT PRIMARY KEY,
json TEXT
)`).run()
// Add compatible scheme to database
db.prepare("INSERT INTO schemes (key, json) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET json=excluded.json").run(notation, JSON.stringify((({ uri, identifier }) => ({ uri, identifier, notation: [notation] }))(scheme)))
// Load concept data for attached mappings (either from cache or API)
const incompatibleSchemes = [], conceptsToLoad = {}
let totalCount = 0, cachedCount = 0, incompatibleCount = 0, failedCount = 0, loadedCount = 0
const loadConcepts = async (scheme) => {
const concepts = conceptsToLoad[scheme.uri]
try {
const results = await scheme._registry.getConcepts({ concepts })
for (const mappingConcept of concepts) {
const result = results.find(c => jskos.compare(c, mappingConcept))
const labels = Object.values(result?.prefLabel || {}).join(" ")
if (!result || !labels.length) {
failedCount += 1
continue
}
db.prepare("INSERT INTO mapping_concepts (uri, label) VALUES (?, ?) ON CONFLICT(uri) DO UPDATE SET label=excluded.label").run(mappingConcept.uri, labels)
loadedCount += 1
mappingConcept.label = labels
}
} catch (error) {
failedCount += concepts.length
}
conceptsToLoad[scheme.uri] = []
}
console.log("Loading concept data for mappings...")
for (const concept of Object.values(conceptData)) {
for (const mappingConcept of [].concat(concept._mappings.exactClose, concept._mappings.narrowBroad, concept._mappings.other)) {
const inScheme = mappingConcept.inScheme[0]
const scheme = schemes.find(s => jskos.compare(s, inScheme))
if (!scheme?._registry?.getConcepts) {
if (!incompatibleSchemes.includes(inScheme.uri)) {
incompatibleSchemes.push(inScheme.uri)
}
incompatibleCount += 1
} else {
// First, try the cache database
const labels = db.prepare("SELECT * FROM mapping_concepts WHERE uri = ?").get(mappingConcept.uri)?.label
if (!labels?.length) {
if (!conceptsToLoad[scheme.uri]) {
conceptsToLoad[scheme.uri] = []
}
conceptsToLoad[scheme.uri].push(mappingConcept)
if (conceptsToLoad[scheme.uri].length >= 20) {
await loadConcepts(scheme)
}
} else {
cachedCount += 1
mappingConcept.label = labels
}
}
totalCount += 1
if (totalCount % 500 === 0) {
console.log(`- ${totalCount} (${loadedCount} loaded, ${cachedCount} cached, ${incompatibleCount} incompatible, ${failedCount} failed)`)
}
}
}
for (let uri of Object.keys(conceptsToLoad)) {
const scheme = schemes.find(s => s.uri === uri)
await loadConcepts(scheme)
}
console.log(`... loaded ${loadedCount} concepts from API.`)
console.log(`... loaded ${cachedCount} concepts from cache.`)
console.log(`... failed to load ${failedCount} concepts.`)
console.log(`... ${incompatibleCount} concepts incompatible.`)
incompatibleSchemes.length > 0 && console.log(`... ${incompatibleSchemes.length} incompatible vocabularies: ${incompatibleSchemes}`)
// Prepare Typesense backend
const collection = `${notation}-suggestions`
if (!(await typesense.exists(collection))) {
// Create collection
await typesense.create(collection)
}
// Import into Typesense
console.log("Importing data into Typesense backend...")
let count = 0
const chunkSize = 5000
for (let i = 0; i < Object.values(conceptData).length; i += chunkSize) {
const chunk = Object.values(conceptData).slice(i, i + chunkSize).map(mapConcept).filter(Boolean)
await typesense.import(collection, chunk)
count += chunk.length
console.log(`... ${count} documents imported.`)
}
console.log("... import into Typesense complete.")
console.log()
}