Skip to content

Commit

Permalink
simplify transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Feb 14, 2024
1 parent 043ac79 commit 7c5c0e4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 45 deletions.
18 changes: 3 additions & 15 deletions ext/data/language/japanese-transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"conditions": {
"v": {
"name": "Verb",
"partsOfSpeech": ["v1", "v5", "vk", "vs", "vz"],
"i18n": [
{
"language": "ja",
Expand All @@ -14,7 +13,6 @@
},
"v1": {
"name": "Ichidan verb",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
Expand All @@ -25,7 +23,6 @@
},
"v1d": {
"name": "Ichidan verb, dictionary form",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
Expand All @@ -35,7 +32,6 @@
},
"v1p": {
"name": "Ichidan verb, progressive or perfect form",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
Expand All @@ -45,7 +41,6 @@
},
"v5": {
"name": "Godan verb",
"partsOfSpeech": ["v5"],
"i18n": [
{
"language": "ja",
Expand All @@ -55,7 +50,6 @@
},
"vk": {
"name": "Kuru verb",
"partsOfSpeech": ["vk"],
"i18n": [
{
"language": "ja",
Expand All @@ -65,7 +59,6 @@
},
"vs": {
"name": "Suru verb",
"partsOfSpeech": ["vs"],
"i18n": [
{
"language": "ja",
Expand All @@ -75,7 +68,6 @@
},
"vz": {
"name": "Zuru verb",
"partsOfSpeech": ["vz"],
"i18n": [
{
"language": "ja",
Expand All @@ -85,7 +77,6 @@
},
"adj-i": {
"name": "Adjective with i ending",
"partsOfSpeech": ["adj-i"],
"i18n": [
{
"language": "ja",
Expand All @@ -94,16 +85,13 @@
]
},
"-te": {
"name": "Intermediate -te endings for progressive or perfect tense",
"partsOfSpeech": []
"name": "Intermediate -te endings for progressive or perfect tense"
},
"adv": {
"name": "Intermediate -ku endings for adverbs",
"partsOfSpeech": []
"name": "Intermediate -ku endings for adverbs"
},
"past": {
"name": "-ta past form ending",
"partsOfSpeech": []
"name": "-ta past form ending"
}
},
"transforms": [
Expand Down
29 changes: 1 addition & 28 deletions ext/js/language/language-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ export class LanguageTransformer {
this._transforms = [];
/** @type {Map<string, number>} */
this._conditionTypeToConditionFlagsMap = new Map();
/** @type {Map<string, number>} */
this._partOfSpeechToConditionFlagsMap = new Map();
}

/** */
clear() {
this._nextFlagIndex = 0;
this._transforms = [];
this._conditionTypeToConditionFlagsMap.clear();
this._partOfSpeechToConditionFlagsMap.clear();
}

/**
Expand Down Expand Up @@ -77,35 +74,11 @@ export class LanguageTransformer {
this._transforms.push(transform);
}

for (const [type, condition] of conditionEntries) {
for (const [type] of conditionEntries) {
const flags = conditionFlagsMap.get(type);
if (typeof flags === 'undefined') { continue; } // This case should never happen
this._conditionTypeToConditionFlagsMap.set(type, flags);
for (const partOfSpeech of condition.partsOfSpeech) {
this._partOfSpeechToConditionFlagsMap.set(partOfSpeech, this.getConditionFlagsFromPartOfSpeech(partOfSpeech) | flags);
}
}
}

/**
* @param {string} partOfSpeech
* @returns {number}
*/
getConditionFlagsFromPartOfSpeech(partOfSpeech) {
const conditionFlags = this._partOfSpeechToConditionFlagsMap.get(partOfSpeech);
return typeof conditionFlags !== 'undefined' ? conditionFlags : 0;
}

/**
* @param {string[]} partsOfSpeech
* @returns {number}
*/
getConditionFlagsFromPartsOfSpeech(partsOfSpeech) {
let result = 0;
for (const partOfSpeech of partsOfSpeech) {
result |= this.getConditionFlagsFromPartOfSpeech(partOfSpeech);
}
return result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export class Translator {
const entryDictionary = /** @type {import('translation').FindTermDictionary} */ (enabledDictionaryMap.get(databaseEntry.dictionary));
const {partsOfSpeechFilter} = entryDictionary;

const definitionConditions = this._languageTransformer.getConditionFlagsFromPartsOfSpeech(databaseEntry.rules);
const definitionConditions = this._languageTransformer.getConditionFlagsFromConditionTypes(databaseEntry.rules);
for (const deinflection of uniqueDeinflectionArrays[databaseEntry.index]) {
if (!partsOfSpeechFilter || LanguageTransformer.conditionsMatch(deinflection.conditions, definitionConditions)) {
deinflection.databaseEntries.push(databaseEntry);
Expand Down
1 change: 0 additions & 1 deletion types/ext/language-transformer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type ConditionMapEntries = ConditionMapEntry[];

export type Condition = {
name: string;
partsOfSpeech: string[];
i18n?: RuleI18n[];
subConditions?: string[];
};
Expand Down

0 comments on commit 7c5c0e4

Please sign in to comment.