add frequency table support for terms

This commit is contained in:
Alex Yatskov 2017-09-12 18:29:16 -07:00
parent 28364b97b0
commit 3b29893072
2 changed files with 40 additions and 1 deletions

View File

@ -87,6 +87,21 @@ class Database {
return results; return results;
} }
async findTermFreq(term, titles) {
if (!this.db) {
throw 'database not initialized';
}
const results = [];
await this.db.termFreq.where('expression').equals(term).each(row => {
if (titles.includes(row.dictionary)) {
results.push({frequency: row.frequency, dictionary: row.dictionary});
}
});
return results;
}
async findKanji(kanji, titles) { async findKanji(kanji, titles) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
@ -174,6 +189,23 @@ class Database {
await this.db.terms.bulkAdd(utilIsolate(rows)); await this.db.terms.bulkAdd(utilIsolate(rows));
}; };
const termFreqDataLoaded = async (title, entries, total, current) => {
if (callback) {
callback(total, current);
}
const rows = [];
for (const [expression, frequency] of entries) {
rows.push({
expression,
frequency,
dictionary: title
});
}
await this.db.termFreq.bulkAdd(utilIsolate(rows));
};
const kanjiDataLoaded = async (title, entries, total, current) => { const kanjiDataLoaded = async (title, entries, total, current) => {
if (callback) { if (callback) {
callback(total, current); callback(total, current);
@ -219,7 +251,7 @@ class Database {
archive, archive,
indexDataLoaded, indexDataLoaded,
termDataLoaded, termDataLoaded,
null, termFreqDataLoaded,
kanjiDataLoaded, kanjiDataLoaded,
null, null,
tagDataLoaded tagDataLoaded

View File

@ -62,7 +62,14 @@ class Translator {
for (const definition of deinflection.definitions) { for (const definition of deinflection.definitions) {
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));
let frequencies = await this.database.findTermFreq(definition.expression, titles);
if (frequencies.length === 0) {
frequencies = await this.database.findTermFreq(definition.reading, titles);
}
definitions.push({ definitions.push({
frequencies,
source: deinflection.source, source: deinflection.source,
reasons: deinflection.reasons, reasons: deinflection.reasons,
score: definition.score, score: definition.score,