Replace 'wildcard' parameter with 'matchType' (#2038)

This commit is contained in:
toasted-nutbread 2021-12-17 16:11:19 -05:00 committed by GitHub
parent 70fa701c90
commit 19ab9df6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 25 deletions

View File

@ -1080,7 +1080,8 @@ class Backend {
const jp = this._japaneseUtil; const jp = this._japaneseUtil;
const mode = 'simple'; const mode = 'simple';
const options = this._getProfileOptions(optionsContext); const options = this._getProfileOptions(optionsContext);
const findTermsOptions = this._getTranslatorFindTermsOptions(mode, {wildcard: null}, options); const details = {matchType: 'exact'};
const findTermsOptions = this._getTranslatorFindTermsOptions(mode, details, options);
const results = []; const results = [];
let previousUngroupedSegment = null; let previousUngroupedSegment = null;
let i = 0; let i = 0;
@ -1958,7 +1959,8 @@ class Backend {
} }
_getTranslatorFindTermsOptions(mode, details, options) { _getTranslatorFindTermsOptions(mode, details, options) {
const {wildcard} = details; let {matchType} = details;
if (typeof matchType !== 'string') { matchType = 'exact'; }
const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options); const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options);
const { const {
general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder}, general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder},
@ -1985,7 +1987,7 @@ class Backend {
excludeDictionaryDefinitions.add(mainDictionary); excludeDictionaryDefinitions.add(mainDictionary);
} }
return { return {
wildcard, matchType,
mainDictionary, mainDictionary,
sortFrequencyDictionary, sortFrequencyDictionary,
sortFrequencyDictionaryOrder, sortFrequencyDictionaryOrder,

View File

@ -850,9 +850,9 @@ class Display extends EventDispatcher {
const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source); const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source);
if (match !== null) { if (match !== null) {
if (match[1]) { if (match[1]) {
findDetails.wildcard = 'prefix'; findDetails.matchType = 'suffix';
} else if (match[3]) { } else if (match[3]) {
findDetails.wildcard = 'suffix'; findDetails.matchType = 'prefix';
} }
source = match[2]; source = match[2];
} }

View File

@ -196,7 +196,7 @@ class DictionaryDatabase {
} }
} }
findTermsBulk(termList, dictionaries, wildcard) { findTermsBulk(termList, dictionaries, matchType) {
const visited = new Set(); const visited = new Set();
const predicate = (row) => { const predicate = (row) => {
if (!dictionaries.has(row.dictionary)) { return false; } if (!dictionaries.has(row.dictionary)) { return false; }
@ -206,17 +206,17 @@ class DictionaryDatabase {
return true; return true;
}; };
const indexNames = (wildcard === 'prefix') ? ['expressionReverse', 'readingReverse'] : ['expression', 'reading']; const indexNames = (matchType === 'suffix') ? ['expressionReverse', 'readingReverse'] : ['expression', 'reading'];
let createQuery; let createQuery;
switch (wildcard) { switch (matchType) {
case 'suffix': case 'prefix':
createQuery = this._createBoundQuery1; createQuery = this._createBoundQuery1;
break; break;
case 'prefix': case 'suffix':
createQuery = this._createBoundQuery2; createQuery = this._createBoundQuery2;
break; break;
default: default: // 'exact'
createQuery = this._createOnlyQuery1; createQuery = this._createOnlyQuery1;
break; break;
} }

View File

@ -63,7 +63,7 @@ class Translator {
* @param options An object using the following structure: * @param options An object using the following structure:
* ``` * ```
* { * {
* wildcard: (enum: null, 'prefix', 'suffix'), * matchType: (enum: 'exact', 'prefix', 'suffix'),
* mainDictionary: (string), * mainDictionary: (string),
* sortFrequencyDictionary: (null or string), * sortFrequencyDictionary: (null or string),
* sortFrequencyDictionaryOrder: (enum: 'ascending', 'descending'), * sortFrequencyDictionaryOrder: (enum: 'ascending', 'descending'),
@ -227,7 +227,6 @@ class Translator {
// Find terms internal implementation // Find terms internal implementation
async _findTermsInternal(text, enabledDictionaryMap, options) { async _findTermsInternal(text, enabledDictionaryMap, options) {
const {wildcard} = options;
if (options.removeNonJapaneseCharacters) { if (options.removeNonJapaneseCharacters) {
text = this._getJapaneseOnlyText(text); text = this._getJapaneseOnlyText(text);
} }
@ -235,9 +234,10 @@ class Translator {
return {dictionaryEntries: [], originalTextLength: 0}; return {dictionaryEntries: [], originalTextLength: 0};
} }
const {matchType} = options;
const deinflections = await ( const deinflections = await (
wildcard ? matchType && matchType !== 'exact' ?
this._findTermsWildcard(text, enabledDictionaryMap, wildcard) : this._findTermsWildcard(text, enabledDictionaryMap, matchType) :
this._findTermDeinflections(text, enabledDictionaryMap, options) this._findTermDeinflections(text, enabledDictionaryMap, options)
); );
@ -259,8 +259,8 @@ class Translator {
return {dictionaryEntries, originalTextLength}; return {dictionaryEntries, originalTextLength};
} }
async _findTermsWildcard(text, enabledDictionaryMap, wildcard) { async _findTermsWildcard(text, enabledDictionaryMap, matchType) {
const databaseEntries = await this._database.findTermsBulk([text], enabledDictionaryMap, wildcard); const databaseEntries = await this._database.findTermsBulk([text], enabledDictionaryMap, matchType);
return databaseEntries.length > 0 ? [this._createDeinflection(text, text, text, 0, [], databaseEntries)] : []; return databaseEntries.length > 0 ? [this._createDeinflection(text, text, text, 0, [], databaseEntries)] : [];
} }

View File

@ -218,15 +218,15 @@ async function testFindTermsBulkTest1(database, titles) {
{ {
inputs: [ inputs: [
{ {
wildcard: null, matchType: null,
termList: ['打', '打つ', '打ち込む'] termList: ['打', '打つ', '打ち込む']
}, },
{ {
wildcard: null, matchType: null,
termList: ['だ', 'ダース', 'うつ', 'ぶつ', 'うちこむ', 'ぶちこむ'] termList: ['だ', 'ダース', 'うつ', 'ぶつ', 'うちこむ', 'ぶちこむ']
}, },
{ {
wildcard: 'suffix', matchType: 'prefix',
termList: ['打'] termList: ['打']
} }
], ],
@ -250,7 +250,7 @@ async function testFindTermsBulkTest1(database, titles) {
{ {
inputs: [ inputs: [
{ {
wildcard: null, matchType: null,
termList: ['込む'] termList: ['込む']
} }
], ],
@ -263,7 +263,7 @@ async function testFindTermsBulkTest1(database, titles) {
{ {
inputs: [ inputs: [
{ {
wildcard: 'prefix', matchType: 'suffix',
termList: ['込む'] termList: ['込む']
} }
], ],
@ -281,7 +281,7 @@ async function testFindTermsBulkTest1(database, titles) {
{ {
inputs: [ inputs: [
{ {
wildcard: null, matchType: null,
termList: [] termList: []
} }
], ],
@ -294,8 +294,8 @@ async function testFindTermsBulkTest1(database, titles) {
]; ];
for (const {inputs, expectedResults} of data) { for (const {inputs, expectedResults} of data) {
for (const {termList, wildcard} of inputs) { for (const {termList, matchType} of inputs) {
const results = await database.findTermsBulk(termList, titles, wildcard); const results = await database.findTermsBulk(termList, titles, matchType);
assert.strictEqual(results.length, expectedResults.total); assert.strictEqual(results.length, expectedResults.total);
for (const [term, count] of expectedResults.terms) { for (const [term, count] of expectedResults.terms) {
assert.strictEqual(countDictionaryDatabaseEntriesWithTerm(results, term), count); assert.strictEqual(countDictionaryDatabaseEntriesWithTerm(results, term), count);