Add support for wildcards

This commit is contained in:
toasted-nutbread 2019-11-04 20:52:08 -05:00
parent 3a225c3f91
commit 7333873244
3 changed files with 36 additions and 9 deletions

View File

@ -130,7 +130,7 @@ class Database {
await Promise.all(promises);
}
async findTermsBulk(termList, titles) {
async findTermsBulk(termList, titles, wildcard) {
this.validate();
const promises = [];
@ -149,10 +149,11 @@ class Database {
const dbIndex2 = dbTerms.index('reading');
for (let i = 0; i < termList.length; ++i) {
const only = IDBKeyRange.only(termList[i]);
const term = termList[i];
const query = wildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term);
promises.push(
Database.getAll(dbIndex1, only, i, processRow),
Database.getAll(dbIndex2, only, i, processRow)
Database.getAll(dbIndex1, query, i, processRow),
Database.getAll(dbIndex2, query, i, processRow)
);
}

View File

@ -203,11 +203,18 @@ class DisplaySearch extends Display {
async onSearchQueryUpdated(query, animate) {
try {
const details = {};
const match = /[\*\uff0a]+$/.exec(query);
if (match !== null) {
details.wildcard = true;
query = query.substr(0, query.length - match[0].length);
}
const valid = (query.length > 0);
this.setIntroVisible(!valid, animate);
this.updateSearchButton();
if (valid) {
const {definitions} = await apiTermsFind(query, {}, this.optionsContext);
const {definitions} = await apiTermsFind(query, details, this.optionsContext);
this.setContentTerms(definitions, {
focus: false,
sentence: null,

View File

@ -227,9 +227,12 @@ class Translator {
}
}
const textHiragana = jpKatakanaToHiragana(text);
const titles = Object.keys(dictionaries);
const deinflections = await this.findTermDeinflections(text, textHiragana, titles);
const deinflections = (
details.wildcard ?
await this.findTermWildcard(text, titles) :
await this.findTermDeinflections(text, titles)
);
let definitions = [];
for (const deinflection of deinflections) {
@ -265,7 +268,23 @@ class Translator {
return [definitions, length];
}
async findTermDeinflections(text, text2, titles) {
async findTermWildcard(text, titles) {
const definitions = await this.database.findTermsBulk([text], titles, true);
if (definitions.length === 0) {
return [];
}
return [{
source: text,
term: text,
rules: 0,
definitions,
reasons: []
}];
}
async findTermDeinflections(text, titles) {
const text2 = jpKatakanaToHiragana(text);
const deinflections = (text === text2 ? this.getDeinflections(text) : this.getDeinflections2(text, text2));
if (deinflections.length === 0) {
@ -289,7 +308,7 @@ class Translator {
deinflectionArray.push(deinflection);
}
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles);
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, false);
for (const definition of definitions) {
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);