2023-10-30 11:36:28 +01:00

54 lines
1.5 KiB
JavaScript

/**
* @param {cqi.models.corpora.Corpus} corpus
* @param {number[]} cposList
* @returns {Promise<object>}
*/
async function lookupsByCpos(corpus, cposList) {
let lookups = {};
lookups['cpos_lookup'] = {};
for (let cpos in cposList) {
lookups['cpos_lookup'][cpos] = {};
}
let pAttrs = await corpus.positionalAttributes.list();
for (let attr in pAttrs) {
let values = await attr.valuesByCpos(cposList);
for (let i = 0; i < cposList.length; i++) {
let cpos = cposList[i];
let value = values[i];
lookups['cpos_lookup'][cpos][attr.name] = value;
}
}
let sAttrs = await corpus.structuralAttributes.list();
for (let attr in sAttrs) {
// We only want to iterate over non subattributes, identifiable by
// sAttr.hasValues == false
if (attr.hasValues) {continue;}
let idList = await attr.idsByCpos(cposList);
for (let i = 0; i < cposList.length; i++) {
let cpos = cposList[i];
let id = idList[i];
if (id == -1) {continue;}
lookups['cpos_lookup'][cpos][attr.name] = id;
}
let occuredIdList = Array.from(new Set(idList.filter(x => x != -1)));
if (occuredIdList.length == 0) {continue;}
let subattrs = sAttrs.filter(x => x.name.startsWith(`${attr.name}_`));
if (subattrs.length == 0) {continue;}
let lookupName = `${attr.name}_lookup`;
lookups[lookupName] = {};
for (let id in occuredIdList) {
lookups[lookupName][id] = {};
}
}
return lookups;
}