move translator to async
This commit is contained in:
parent
f49a69c993
commit
b6f3919ef6
@ -69,14 +69,14 @@ class Database {
|
|||||||
await this.prepare();
|
await this.prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
async findTerms(term, dictionaries) {
|
async findTerms(term, titles) {
|
||||||
if (!this.db) {
|
if (!this.db) {
|
||||||
throw 'database not initialized';
|
throw 'database not initialized';
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = [];
|
const results = [];
|
||||||
await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
|
await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
|
||||||
if (dictionaries.includes(row.dictionary)) {
|
if (titles.includes(row.dictionary)) {
|
||||||
results.push({
|
results.push({
|
||||||
expression: row.expression,
|
expression: row.expression,
|
||||||
reading: row.reading,
|
reading: row.reading,
|
||||||
@ -90,7 +90,7 @@ class Database {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.cacheTagMeta(dictionaries);
|
await this.cacheTagMeta(titles);
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
result.tagMeta = this.tagCache[result.dictionary] || {};
|
result.tagMeta = this.tagCache[result.dictionary] || {};
|
||||||
}
|
}
|
||||||
@ -98,14 +98,14 @@ class Database {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findKanji(kanji, dictionaries) {
|
async findKanji(kanji, titles) {
|
||||||
if (!this.db) {
|
if (!this.db) {
|
||||||
return Promise.reject('database not initialized');
|
return Promise.reject('database not initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = [];
|
const results = [];
|
||||||
await this.db.kanji.where('character').equals(kanji).each(row => {
|
await this.db.kanji.where('character').equals(kanji).each(row => {
|
||||||
if (dictionaries.includes(row.dictionary)) {
|
if (titles.includes(row.dictionary)) {
|
||||||
results.push({
|
results.push({
|
||||||
character: row.character,
|
character: row.character,
|
||||||
onyomi: dictFieldSplit(row.onyomi),
|
onyomi: dictFieldSplit(row.onyomi),
|
||||||
@ -117,7 +117,7 @@ class Database {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.cacheTagMeta(dictionaries);
|
await this.cacheTagMeta(titles);
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
result.tagMeta = this.tagCache[result.dictionary] || {};
|
result.tagMeta = this.tagCache[result.dictionary] || {};
|
||||||
}
|
}
|
||||||
@ -125,29 +125,29 @@ class Database {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async cacheTagMeta(dictionaries) {
|
async cacheTagMeta(titles) {
|
||||||
if (!this.db) {
|
if (!this.db) {
|
||||||
throw 'database not initialized';
|
throw 'database not initialized';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const dictionary of dictionaries) {
|
for (const title of titles) {
|
||||||
if (!this.tagCache[dictionary]) {
|
if (!this.tagCache[title]) {
|
||||||
const tagMeta = {};
|
const tagMeta = {};
|
||||||
await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
|
await this.db.tagMeta.where('dictionary').equals(title).each(row => {
|
||||||
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
|
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
|
||||||
});
|
});
|
||||||
|
|
||||||
this.tagCache[dictionary] = tagMeta;
|
this.tagCache[title] = tagMeta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDictionaries() {
|
async getDictionaries() {
|
||||||
if (!this.db) {
|
if (this.db) {
|
||||||
|
return this.db.dictionaries.toArray();
|
||||||
|
} else {
|
||||||
throw 'database not initialized';
|
throw 'database not initialized';
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.db.dictionaries.toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async importDictionary(archive, callback) {
|
async importDictionary(archive, callback) {
|
||||||
|
@ -19,47 +19,41 @@
|
|||||||
|
|
||||||
class Translator {
|
class Translator {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.loaded = false;
|
|
||||||
this.ruleMeta = null;
|
|
||||||
this.database = new Database();
|
this.database = new Database();
|
||||||
this.deinflector = new Deinflector();
|
this.deinflector = new Deinflector();
|
||||||
|
this.loaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
prepare() {
|
async prepare() {
|
||||||
if (this.loaded) {
|
if (!this.loaded) {
|
||||||
return Promise.resolve();
|
const reasons = await jsonLoadInt('/bg/lang/deinflect.json');
|
||||||
}
|
|
||||||
|
|
||||||
const promises = [
|
|
||||||
jsonLoadInt('/bg/lang/deinflect.json'),
|
|
||||||
this.database.prepare()
|
|
||||||
];
|
|
||||||
|
|
||||||
return Promise.all(promises).then(([reasons]) => {
|
|
||||||
this.deinflector.setReasons(reasons);
|
this.deinflector.setReasons(reasons);
|
||||||
|
await this.database.prepare();
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerms(text, dictionaries, alphanumeric) {
|
async findTermsGrouped(text, dictionaries, alphanumeric) {
|
||||||
const titles = Object.keys(dictionaries);
|
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
|
||||||
const cache = {};
|
return {length, definitions: dictTermsGroup(definitions, dictionaries)};
|
||||||
|
}
|
||||||
|
|
||||||
|
async findTerms(text, dictionaries, alphanumeric) {
|
||||||
if (!alphanumeric && text.length > 0) {
|
if (!alphanumeric && text.length > 0) {
|
||||||
const c = text[0];
|
const c = text[0];
|
||||||
if (!jpIsKana(c) && !jpIsKanji(c)) {
|
if (!jpIsKana(c) && !jpIsKanji(c)) {
|
||||||
return Promise.resolve({length: 0, definitions: []});
|
return {length: 0, definitions: []};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => {
|
const cache = {};
|
||||||
|
const titles = Object.keys(dictionaries);
|
||||||
|
let deinflections = await this.findTermsDeinflected(text, titles, cache);
|
||||||
const textHiragana = jpKatakanaToHiragana(text);
|
const textHiragana = jpKatakanaToHiragana(text);
|
||||||
if (text === textHiragana) {
|
if (text !== textHiragana) {
|
||||||
return deinfLiteral;
|
deinflections = deinflections.concat(await this.findTermsDeinflected(textHiragana, titles, cache));
|
||||||
} else {
|
|
||||||
return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana));
|
|
||||||
}
|
}
|
||||||
}).then(deinflections => {
|
|
||||||
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) {
|
||||||
@ -88,69 +82,47 @@ class Translator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {length, definitions};
|
return {length, definitions};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
findTermsGrouped(text, dictionaries, alphanumeric) {
|
async findTermsDeinflected(text, dictionaries, cache) {
|
||||||
return this.findTerms(text, dictionaries, alphanumeric).then(({length, definitions}) => {
|
await this.prepare();
|
||||||
return {length, definitions: dictTermsGroup(definitions, dictionaries)};
|
|
||||||
});
|
const definer = async term => {
|
||||||
|
if (cache.hasOwnProperty(term)) {
|
||||||
|
return cache[term];
|
||||||
|
} else {
|
||||||
|
return cache[term] = await this.database.findTerms(term, dictionaries);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let deinflections = [];
|
||||||
|
for (let i = text.length; i > 0; --i) {
|
||||||
|
const textSlice = text.slice(0, i);
|
||||||
|
deinflections = deinflections.concat(await this.deinflector.deinflect(textSlice, definer));
|
||||||
}
|
}
|
||||||
|
|
||||||
findKanji(text, dictionaries) {
|
return deinflections;
|
||||||
const titles = Object.keys(dictionaries);
|
}
|
||||||
|
|
||||||
|
async findKanji(text, dictionaries) {
|
||||||
|
await this.prepare();
|
||||||
|
|
||||||
|
let definitions = [];
|
||||||
const processed = {};
|
const processed = {};
|
||||||
const promises = [];
|
const titles = Object.keys(dictionaries);
|
||||||
|
|
||||||
for (const c of text) {
|
for (const c of text) {
|
||||||
if (!processed[c]) {
|
if (!processed[c]) {
|
||||||
promises.push(this.database.findKanji(c, titles));
|
definitions = definitions.concat(await this.database.findKanji(c, titles));
|
||||||
processed[c] = true;
|
processed[c] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(promises).then(defSets => {
|
|
||||||
const definitions = defSets.reduce((a, b) => a.concat(b), []);
|
|
||||||
for (const definition of definitions) {
|
for (const definition of 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));
|
||||||
definition.tags = dictTagsSort(tags);
|
definition.tags = dictTagsSort(tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
return definitions;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
findTermsDeinflected(text, dictionaries, cache) {
|
|
||||||
const definer = term => {
|
|
||||||
if (cache.hasOwnProperty(term)) {
|
|
||||||
return Promise.resolve(cache[term]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.database.findTerms(term, dictionaries).then(definitions => cache[term] = definitions);
|
|
||||||
};
|
|
||||||
|
|
||||||
const promises = [];
|
|
||||||
for (let i = text.length; i > 0; --i) {
|
|
||||||
promises.push(this.deinflector.deinflect(text.slice(0, i), definer));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(promises).then(results => {
|
|
||||||
let deinflections = [];
|
|
||||||
for (const result of results) {
|
|
||||||
deinflections = deinflections.concat(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
return deinflections;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
processKanji(definitions) {
|
|
||||||
for (const definition of definitions) {
|
|
||||||
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
|
|
||||||
definition.tags = dictTagsSort(tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
return definitions;
|
return definitions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user