From 3a225c3f916d435e04fb30afa731c30c4309fc7f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 4 Nov 2019 20:43:40 -0500 Subject: Add details field to apiTermsFind --- ext/bg/js/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js/search.js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index ad579918..abd40640 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -207,7 +207,7 @@ class DisplaySearch extends Display { this.setIntroVisible(!valid, animate); this.updateSearchButton(); if (valid) { - const {definitions} = await apiTermsFind(query, this.optionsContext); + const {definitions} = await apiTermsFind(query, {}, this.optionsContext); this.setContentTerms(definitions, { focus: false, sentence: null, -- cgit v1.2.3 From 7333873244ccaeeefe01bd3a63447f39dd4f3bbe Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 4 Nov 2019 20:52:08 -0500 Subject: Add support for wildcards --- ext/bg/js/database.js | 9 +++++---- ext/bg/js/search.js | 9 ++++++++- ext/bg/js/translator.js | 27 +++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) (limited to 'ext/bg/js/search.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index b6cf9063..9b560f78 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -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) ); } diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index abd40640..aa56d1a6 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -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, diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index dac53f93..583d6e31 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -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); -- cgit v1.2.3