add frequency table support for kanji

This commit is contained in:
Alex Yatskov 2017-09-12 20:20:03 -07:00
parent 3b29893072
commit 79b99131f6
3 changed files with 41 additions and 6 deletions

View File

@ -129,6 +129,21 @@ class Database {
return results; return results;
} }
async findKanjiFreq(kanji, titles) {
if (!this.db) {
throw 'database not initialized';
}
const results = [];
await this.db.kanjiFreq.where('character').equals(kanji).each(row => {
if (titles.includes(row.dictionary)) {
results.push({frequency: row.frequency, dictionary: row.dictionary});
}
});
return results;
}
async cacheTagMeta(titles) { async cacheTagMeta(titles) {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'database not initialized';
@ -186,7 +201,7 @@ class Database {
}); });
} }
await this.db.terms.bulkAdd(utilIsolate(rows)); await this.db.terms.bulkAdd(rows);
}; };
const termFreqDataLoaded = async (title, entries, total, current) => { const termFreqDataLoaded = async (title, entries, total, current) => {
@ -203,7 +218,7 @@ class Database {
}); });
} }
await this.db.termFreq.bulkAdd(utilIsolate(rows)); await this.db.termFreq.bulkAdd(rows);
}; };
const kanjiDataLoaded = async (title, entries, total, current) => { const kanjiDataLoaded = async (title, entries, total, current) => {
@ -223,7 +238,24 @@ class Database {
}); });
} }
await this.db.kanji.bulkAdd(utilIsolate(rows)); await this.db.kanji.bulkAdd(rows);
};
const kanjiFreqDataLoaded = async (title, entries, total, current) => {
if (callback) {
callback(total, current);
}
const rows = [];
for (const [character, frequency] of entries) {
rows.push({
character,
frequency,
dictionary: title
});
}
await this.db.kanjiFreq.bulkAdd(rows);
}; };
const tagDataLoaded = async (title, entries, total, current) => { const tagDataLoaded = async (title, entries, total, current) => {
@ -244,7 +276,7 @@ class Database {
rows.push(row); rows.push(row);
} }
await this.db.tagMeta.bulkAdd(utilIsolate(rows)); await this.db.tagMeta.bulkAdd(rows);
}; };
return await Database.importDictionaryZip( return await Database.importDictionaryZip(
@ -253,7 +285,7 @@ class Database {
termDataLoaded, termDataLoaded,
termFreqDataLoaded, termFreqDataLoaded,
kanjiDataLoaded, kanjiDataLoaded,
null, kanjiFreqDataLoaded,
tagDataLoaded tagDataLoaded
); );
} }

View File

@ -82,7 +82,9 @@ function formUpdateVisibility(options) {
const debug = $('#debug'); const debug = $('#debug');
if (options.general.debugInfo) { if (options.general.debugInfo) {
const text = JSON.stringify(options, null, 4); const temp = utilIsolate(options);
temp.anki.fieldTemplates = '...';
const text = JSON.stringify(temp, null, 4);
debug.html(handlebarsEscape(text)); debug.html(handlebarsEscape(text));
debug.show(); debug.show();
} else { } else {

View File

@ -127,6 +127,7 @@ class Translator {
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
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);
} }
return definitions; return definitions;