Skip to content

Commit

Permalink
add part of speech filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Dec 24, 2023
1 parent 769e9e5 commit c8b8c6e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
12 changes: 9 additions & 3 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,8 @@ export class Backend {
convertHiraganaToKatakana,
convertKatakanaToHiragana,
collapseEmphaticSequences,
textReplacements: textReplacementsOptions
textReplacements: textReplacementsOptions,
partsOfSpeechFilter
}
} = options;
const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions);
Expand All @@ -2456,7 +2457,7 @@ export class Backend {
excludeDictionaryDefinitions = new Set();
excludeDictionaryDefinitions.add(mainDictionary);
}
return {
const findTermsOptions = {
matchType,
deinflect,
mainDictionary,
Expand All @@ -2471,8 +2472,13 @@ export class Backend {
collapseEmphaticSequences,
textReplacements,
enabledDictionaryMap,
excludeDictionaryDefinitions
excludeDictionaryDefinitions,
partsOfSpeechFilter
};

console.log('findTermsOptions', findTermsOptions);

return findTermsOptions;
}

/**
Expand Down
18 changes: 16 additions & 2 deletions ext/js/language/deinflector.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ export class Deinflector {
/**
* Deinflects a Japanese term to all of its possible dictionary forms.
* @param {string} source The source term to deinflect.
* @param {import('translation-internal').DeinflectionOptions} options
* @returns {import('translation-internal').Deinflection[]}
* @example
* const deinflector = new Deinflector(deinflectionReasons);
* // [{ term: '食べた', rules: 0, reasons: [] }, { term: '食べる', rules: 1, reasons: ['past'] }, { term: '食ぶ', rules: 2, reasons: ['potential', 'past'] }]
* console.log(deinflector.deinflect('食べさせられる'));
*/
deinflect(source) {
deinflect(source, options) {
const {partsOfSpeechFilter} = options;
const results = [this._createDeinflection(source, 0, [])];
for (let i = 0; i < results.length; ++i) {
const {rules, term, reasons} = results[i];
for (const [reason, variants] of this.reasons) {
for (const [kanaIn, kanaOut, rulesIn, rulesOut] of variants) {
if (
(rules !== 0 && (rules & rulesIn) === 0) ||
!Deinflector.rulesCheck(partsOfSpeechFilter, rules, rulesIn) ||
!term.endsWith(kanaIn) ||
(term.length - kanaIn.length + kanaOut.length) <= 0
) {
Expand All @@ -77,6 +79,18 @@ export class Deinflector {
return results;
}

/**
*
* @param {boolean} checkRules
* @param {import('translation-internal').DeinflectionRuleFlags} rules1
* @param {import('translation-internal').DeinflectionRuleFlags} rules2
* @returns {boolean}
*/
static rulesCheck(checkRules, rules1, rules2){
if (!checkRules) { return true; }
return rules1 === 0 || (rules1 & rules2) !== 0;
}

/**
* @param {string} term
* @param {import('translation-internal').DeinflectionRuleFlags} rules
Expand Down
8 changes: 5 additions & 3 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class Translator {
* @returns {Promise<{dictionaryEntries: import('dictionary').TermDictionaryEntry[], originalTextLength: number}>} An object containing dictionary entries and the length of the original source text.
*/
async findTerms(mode, text, options) {
console.log('findTerms', mode, text, options);
const {enabledDictionaryMap, excludeDictionaryDefinitions, sortFrequencyDictionary, sortFrequencyDictionaryOrder} = options;
const tagAggregator = new TranslatorTagAggregator();
let {dictionaryEntries, originalTextLength} = await this._findTermsInternalWrapper(text, enabledDictionaryMap, options, tagAggregator);
Expand Down Expand Up @@ -265,14 +266,15 @@ export class Translator {
deinflectionArray.push(deinflection);
}

const {matchType} = options;
const {matchType, partsOfSpeechFilter} = options;

const databaseEntries = await this._database.findTermsBulk(uniqueDeinflectionTerms, enabledDictionaryMap, matchType);

for (const databaseEntry of databaseEntries) {
const definitionRules = Deinflector.rulesToRuleFlags(databaseEntry.rules);
for (const deinflection of uniqueDeinflectionArrays[databaseEntry.index]) {
const deinflectionRules = deinflection.rules;
if (deinflectionRules === 0 || (definitionRules & deinflectionRules) !== 0) {
if (Deinflector.rulesCheck(partsOfSpeechFilter, deinflectionRules, definitionRules)) {
deinflection.databaseEntries.push(databaseEntry);
}
}
Expand Down Expand Up @@ -334,7 +336,7 @@ export class Translator {
if (used.has(source)) { break; }
used.add(source);
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
for (const {term, rules, reasons} of /** @type {Deinflector} */ (this._deinflector).deinflect(source)) {
for (const {term, rules, reasons} of /** @type {Deinflector} */ (this._deinflector).deinflect(source, options)) {
deinflections.push(this._createDeinflection(rawSource, source, term, rules, reasons));
}
}
Expand Down

0 comments on commit c8b8c6e

Please sign in to comment.