Skip to content

Commit

Permalink
use isPartOfSpeech
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Feb 19, 2024
1 parent 88a0fb6 commit ddcbf34
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
32 changes: 22 additions & 10 deletions ext/data/language/japanese-transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"name": "動詞"
}
],
"isPartOfSpeech": false,
"subConditions": ["v1", "v5", "vk", "vs", "vz"]
},
"v1": {
Expand All @@ -19,6 +20,7 @@
"name": "一段動詞"
}
],
"isPartOfSpeech": true,
"subConditions": ["v1d", "v1p"]
},
"v1d": {
Expand All @@ -28,7 +30,8 @@
"language": "ja",
"name": "一段動詞、辞書形"
}
]
],
"isPartOfSpeech": false
},
"v1p": {
"name": "Ichidan verb, progressive or perfect form",
Expand All @@ -37,7 +40,8 @@
"language": "ja",
"name": "一段動詞、進行形または完了形"
}
]
],
"isPartOfSpeech": false
},
"v5": {
"name": "Godan verb",
Expand All @@ -46,7 +50,8 @@
"language": "ja",
"name": "五段動詞"
}
]
],
"isPartOfSpeech": true
},
"vk": {
"name": "Kuru verb",
Expand All @@ -55,7 +60,8 @@
"language": "ja",
"name": "来る動詞"
}
]
],
"isPartOfSpeech": true
},
"vs": {
"name": "Suru verb",
Expand All @@ -64,7 +70,8 @@
"language": "ja",
"name": "する動詞"
}
]
],
"isPartOfSpeech": true
},
"vz": {
"name": "Zuru verb",
Expand All @@ -73,7 +80,8 @@
"language": "ja",
"name": "ずる動詞"
}
]
],
"isPartOfSpeech": true
},
"adj-i": {
"name": "Adjective with i ending",
Expand All @@ -82,16 +90,20 @@
"language": "ja",
"name": "形容詞"
}
]
],
"isPartOfSpeech": true
},
"-te": {
"name": "Intermediate -te endings for progressive or perfect tense"
"name": "Intermediate -te endings for progressive or perfect tense",
"isPartOfSpeech": false
},
"adv": {
"name": "Intermediate -ku endings for adverbs"
"name": "Intermediate -ku endings for adverbs",
"isPartOfSpeech": false
},
"past": {
"name": "-ta past form ending"
"name": "-ta past form ending",
"isPartOfSpeech": false
}
},
"transforms": [
Expand Down
29 changes: 28 additions & 1 deletion ext/js/language/language-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ 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 @@ -74,10 +77,13 @@ export class LanguageTransformer {
this._transforms.push(transform);
}

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

Expand All @@ -90,6 +96,27 @@ export class LanguageTransformer {
return typeof conditionFlags !== 'undefined' ? conditionFlags : 0;
}

/**
* @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;
}

/**
* @param {string[]} conditionTypes
* @returns {number}
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.getConditionFlagsFromConditionTypes(databaseEntry.rules);
const definitionConditions = this._languageTransformer.getConditionFlagsFromPartsOfSpeech(databaseEntry.rules);
for (const deinflection of uniqueDeinflectionArrays[databaseEntry.index]) {
if (!partsOfSpeechFilter || LanguageTransformer.conditionsMatch(deinflection.conditions, definitionConditions)) {
deinflection.databaseEntries.push(databaseEntry);
Expand Down
1 change: 1 addition & 0 deletions types/ext/language-transformer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type ConditionMapEntries = ConditionMapEntry[];
export type Condition = {
name: string;
i18n?: RuleI18n[];
isPartOfSpeech: boolean;
subConditions?: string[];
};

Expand Down

0 comments on commit ddcbf34

Please sign in to comment.