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

View File

@ -60,7 +60,7 @@ class Translator {
let definitions = []; let definitions = [];
for (const deinflection of deinflections) { for (const deinflection of deinflections) {
for (const definition of deinflection.definitions) { 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)); tags.push(dictTagBuildSource(definition.dictionary));
let frequencies = await this.database.findTermFreq(definition.expression, titles); let frequencies = await this.database.findTermFreq(definition.expression, titles);
@ -124,12 +124,29 @@ class Translator {
} }
for (const definition of definitions) { 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)); tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags); definition.tags = dictTagsSort(tags);
definition.frequencies = await this.database.findKanjiFreq(definition.character, titles); definition.frequencies = await this.database.findKanjiFreq(definition.character, titles);
} }
return definitions; 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;
}
} }