Commit e89d8292 authored by Markus Opolka's avatar Markus Opolka
Browse files

Add feature glossary API endpoint

parent 557bec81
......@@ -177,6 +177,11 @@ app.get('/api/search/:letter?', function (req, res, next) {
res.send(data)
})
app.get('/api/features/', function (req, res) {
// Feature Glossary
res.send(lib.glossary(loadDocuments(DIR)))
})
app.get('/api/features/:letter?', function (req, res) {
// Feature Search Endpoint with query parameters
// <feature category="diakritika" type="akut" subtype="akutstattgravis" ref="26">
......@@ -213,6 +218,7 @@ app.get('/api/features/:letter?', function (req, res) {
res.send(data.sort(lib.compare))
})
app.get('/api/letters/:letter?', function (req, res) {
// Document Endpoint with optional Document name
// Lists either all or one given document
......@@ -242,6 +248,10 @@ app.get('/api/metadata/reload', function (req, res) {
// Loading all documents to cache
loadDocumentsToCache(DIR)
// Loading all documents to cache
// loadGlossaryToCache(DIR)
// Where the magic happens
app.listen(PORT, function () {
console.log('> Listening at http://localhost:' + PORT)
......
......@@ -143,6 +143,59 @@ function searchFeaturesInDocument (document, query) {
return document
}
function featureToString (feature) {
/*
* Turn into feature object into category>type>subtype String
*/
let category = feature.category
let type = feature.type
let subtype = feature.subtype
let ret = ''
if (category !== undefined) {
ret = ret.concat(category)
}
if (type !== undefined) {
ret = ret.concat(' > ', type)
}
if (subtype !== undefined) {
ret = ret.concat(' > ', subtype)
}
return ret
}
function featureGlossary (objs) {
/*
* Turn list of documents objects into a glossary set
*/
let featureList = []
for (let document of objs) {
try {
let documentFeatures = findKeyRecursive(document, 'feature')
for (let feature of documentFeatures) {
delete feature._attributes['ref']
featureList.push(feature._attributes)
}
} catch (err) {
console.log(arguments.callee) // eslint-disable-line no-caller
console.log('>> Error while getting features from' + document.name)
console.log(err)
}
}
let featureArray = []
for (let feat of featureList) {
featureArray.push(featureToString(feat))
}
return Array.from(new Set(featureArray))
}
function metadataTable (objs) {
/*
* Turn list of documents objects into metadata table format
......@@ -189,6 +242,9 @@ module.exports = {
table: function (objs) {
return metadataTable(objs)
},
glossary: function (objs) {
return featureGlossary(objs)
},
compare: function (a, b) {
if (a.hits > b.hits) { return -1 }
if (a.hits < b.hits) { return 1 }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment