Deinflector refactor (#1501)

* Make Deinflector._ruleTypes private

* Add createDeinflection helper

* Remove unnecessary field assignments from Deinflector

Move them to Translator instead
This commit is contained in:
toasted-nutbread 2021-03-07 14:07:26 -05:00 committed by GitHub
parent 92fe1571ae
commit 7793e14e57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 38 deletions

View File

@ -20,15 +20,8 @@ class Deinflector {
this.reasons = Deinflector.normalizeReasons(reasons); this.reasons = Deinflector.normalizeReasons(reasons);
} }
deinflect(source, rawSource) { deinflect(source) {
const results = [{ const results = [this._createDeinflection(source, 0, [])];
source,
rawSource,
term: source,
rules: 0,
reasons: [],
databaseDefinitions: []
}];
for (let i = 0; i < results.length; ++i) { for (let i = 0; i < results.length; ++i) {
const {rules, term, reasons} = results[i]; const {rules, term, reasons} = results[i];
for (const [reason, variants] of this.reasons) { for (const [reason, variants] of this.reasons) {
@ -41,20 +34,21 @@ class Deinflector {
continue; continue;
} }
results.push({ results.push(this._createDeinflection(
source, term.substring(0, term.length - kanaIn.length) + kanaOut,
rawSource, rulesOut,
term: term.substring(0, term.length - kanaIn.length) + kanaOut, [reason, ...reasons]
rules: rulesOut, ));
reasons: [reason, ...reasons],
databaseDefinitions: []
});
} }
} }
} }
return results; return results;
} }
_createDeinflection(term, rules, reasons) {
return {term, rules, reasons};
}
static normalizeReasons(reasons) { static normalizeReasons(reasons) {
const normalizedReasons = []; const normalizedReasons = [];
for (const [reason, reasonInfo] of Object.entries(reasons)) { for (const [reason, reasonInfo] of Object.entries(reasons)) {
@ -63,8 +57,8 @@ class Deinflector {
variants.push([ variants.push([
kanaIn, kanaIn,
kanaOut, kanaOut,
Deinflector.rulesToRuleFlags(rulesIn), this.rulesToRuleFlags(rulesIn),
Deinflector.rulesToRuleFlags(rulesOut) this.rulesToRuleFlags(rulesOut)
]); ]);
} }
normalizedReasons.push([reason, variants]); normalizedReasons.push([reason, variants]);
@ -73,7 +67,7 @@ class Deinflector {
} }
static rulesToRuleFlags(rules) { static rulesToRuleFlags(rules) {
const ruleTypes = Deinflector.ruleTypes; const ruleTypes = this._ruleTypes;
let value = 0; let value = 0;
for (const rule of rules) { for (const rule of rules) {
const ruleBits = ruleTypes.get(rule); const ruleBits = ruleTypes.get(rule);
@ -84,7 +78,8 @@ class Deinflector {
} }
} }
Deinflector.ruleTypes = new Map([ // eslint-disable-next-line no-underscore-dangle
Deinflector._ruleTypes = new Map([
['v1', 0b00000001], // Verb ichidan ['v1', 0b00000001], // Verb ichidan
['v5', 0b00000010], // Verb godan ['v5', 0b00000010], // Verb godan
['vs', 0b00000100], // Verb suru ['vs', 0b00000100], // Verb suru

View File

@ -250,18 +250,7 @@ class Translator {
async _findTermWildcard(text, enabledDictionaryMap, wildcard) { async _findTermWildcard(text, enabledDictionaryMap, wildcard) {
const databaseDefinitions = await this._database.findTermsBulk([text], enabledDictionaryMap, wildcard); const databaseDefinitions = await this._database.findTermsBulk([text], enabledDictionaryMap, wildcard);
if (databaseDefinitions.length === 0) { return databaseDefinitions.length > 0 ? [this._createDeinflection(text, text, text, 0, [], databaseDefinitions)] : [];
return [];
}
return [{
source: text,
rawSource: text,
term: text,
rules: 0,
reasons: [],
databaseDefinitions
}];
} }
async _findTermDeinflections(text, enabledDictionaryMap, options) { async _findTermDeinflections(text, enabledDictionaryMap, options) {
@ -341,18 +330,22 @@ class Translator {
} }
for (let i = text2.length; i > 0; --i) { for (let i = text2.length; i > 0; --i) {
const text2Substring = text2.substring(0, i); const source = text2.substring(0, i);
if (used.has(text2Substring)) { break; } if (used.has(source)) { break; }
used.add(text2Substring); used.add(source);
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i)); const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
for (const deinflection of this._deinflector.deinflect(text2Substring, rawSource)) { for (const {term, rules, reasons} of this._deinflector.deinflect(source)) {
deinflections.push(deinflection); deinflections.push(this._createDeinflection(source, rawSource, term, rules, reasons, []));
} }
} }
} }
return deinflections; return deinflections;
} }
_createDeinflection(source, rawSource, term, rules, reasons, databaseDefinitions) {
return {source, rawSource, term, rules, reasons, databaseDefinitions};
}
/** /**
* @param definitions An array of 'term' definitions. * @param definitions An array of 'term' definitions.
* @param mainDictionary The name of the main dictionary. * @param mainDictionary The name of the main dictionary.