better tag handling

This commit is contained in:
Alex Yatskov 2017-09-13 16:42:04 -07:00
parent ba8451f429
commit 13961e6a10
2 changed files with 28 additions and 25 deletions

View File

@ -20,7 +20,6 @@
class Database {
constructor() {
this.db = null;
this.tagCache = {};
}
async prepare() {
@ -38,7 +37,7 @@ class Database {
this.db.version(3).stores({
termFreq: '++,dictionary,expression',
kanjiFreq: '++,dictionary,character',
tagMeta: '++,dictionary'
tagMeta: '++,dictionary,name'
});
await this.db.open();
@ -52,7 +51,6 @@ class Database {
this.db.close();
await this.db.delete();
this.db = null;
this.tagCache = {};
await this.prepare();
}
@ -78,11 +76,6 @@ class Database {
}
});
await this.cacheTagMeta(titles);
for (const result of results) {
result.tagMeta = this.tagCache[result.dictionary] || {};
}
return results;
}
@ -120,11 +113,6 @@ class Database {
}
});
await this.cacheTagMeta(titles);
for (const result of results) {
result.tagMeta = this.tagCache[result.dictionary] || {};
}
return results;
}
@ -143,21 +131,19 @@ class Database {
return results;
}
async cacheTagMeta(titles) {
async findTag(name, titles) {
if (!this.db) {
throw 'database not initialized';
}
for (const title of titles) {
if (!this.tagCache[title]) {
const tagMeta = {};
await this.db.tagMeta.where('dictionary').equals(title).each(row => {
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
});
this.tagCache[title] = tagMeta;
let result = null;
await this.db.tagMeta.where('name').equals(name).each(row => {
if (titles.includes(row.dictionary)) {
result = row;
}
}
});
return result;
}
async getDictionaries() {

View File

@ -60,7 +60,7 @@ class Translator {
let definitions = [];
for (const deinflection of deinflections) {
for (const definition of deinflection.definitions) {
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
const tags = await this.buildTags(definition.tags, titles);
tags.push(dictTagBuildSource(definition.dictionary));
let frequencies = await this.database.findTermFreq(definition.expression, titles);
@ -124,12 +124,29 @@ class Translator {
}
for (const definition of definitions) {
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
const tags = await this.buildTags(definition.tags, titles);
tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags);
definition.frequencies = await this.database.findKanjiFreq(definition.character, titles);
}
return definitions;
}
async buildTags(names, titles) {
const results = [];
for (const name of names) {
const meta = await this.database.findTag(name.split(':')[0], titles);
const result = {name};
for (const prop in meta || {}) {
result[prop] = meta[prop];
}
results.push(dictTagSanitize(result));
}
return results;
}
}