Skip to content

Commit

Permalink
fix translator bug, simplify language-transformer
Browse files Browse the repository at this point in the history
* simplify transformer

* use isPartOfSpeech

* simplify

* rename to isDictionaryForm

* NOP

---------

Co-authored-by: Darius Jahandarie <djahandarie@gmail.com>

<rikaitan.link>H4sIAAAAAAAAA7NMMk1JNUgxSjFJTjIyMjE3NUw2NU9KSUw1MzW1SDQwMTMGihilmXABACLpidUpAAAA</rikaitan.link>
  • Loading branch information
StefanVukovic99 authored and tatsumoto-ren committed Feb 24, 2024
1 parent 2cffdd9 commit 70d8cb4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
38 changes: 19 additions & 19 deletions ext/data/language/japanese-transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,107 @@
"conditions": {
"v": {
"name": "Verb",
"partsOfSpeech": ["v1", "v5", "vk", "vs", "vz"],
"i18n": [
{
"language": "ja",
"name": "動詞"
}
],
"isDictionaryForm": false,
"subConditions": ["v1", "v5", "vk", "vs", "vz"]
},
"v1": {
"name": "Ichidan verb",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
"name": "一段動詞"
}
],
"isDictionaryForm": true,
"subConditions": ["v1d", "v1p"]
},
"v1d": {
"name": "Ichidan verb, dictionary form",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
"name": "一段動詞、辞書形"
}
]
],
"isDictionaryForm": false
},
"v1p": {
"name": "Ichidan verb, progressive or perfect form",
"partsOfSpeech": ["v1"],
"i18n": [
{
"language": "ja",
"name": "一段動詞、進行形または完了形"
}
]
],
"isDictionaryForm": false
},
"v5": {
"name": "Godan verb",
"partsOfSpeech": ["v5"],
"i18n": [
{
"language": "ja",
"name": "五段動詞"
}
]
],
"isDictionaryForm": true
},
"vk": {
"name": "Kuru verb",
"partsOfSpeech": ["vk"],
"i18n": [
{
"language": "ja",
"name": "来る動詞"
}
]
],
"isDictionaryForm": true
},
"vs": {
"name": "Suru verb",
"partsOfSpeech": ["vs"],
"i18n": [
{
"language": "ja",
"name": "する動詞"
}
]
],
"isDictionaryForm": true
},
"vz": {
"name": "Zuru verb",
"partsOfSpeech": ["vz"],
"i18n": [
{
"language": "ja",
"name": "ずる動詞"
}
]
],
"isDictionaryForm": true
},
"adj-i": {
"name": "Adjective with i ending",
"partsOfSpeech": ["adj-i"],
"i18n": [
{
"language": "ja",
"name": "形容詞"
}
]
],
"isDictionaryForm": true
},
"-te": {
"name": "Intermediate -te endings for progressive or perfect tense",
"partsOfSpeech": []
"isDictionaryForm": false
},
"adv": {
"name": "Intermediate -ku endings for adverbs",
"partsOfSpeech": []
"isDictionaryForm": false
},
"past": {
"name": "-ta past form ending",
"partsOfSpeech": []
"isDictionaryForm": false
}
},
"transforms": [
Expand Down
65 changes: 33 additions & 32 deletions ext/js/language/language-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export class LanguageTransformer {
const rules2 = [];
for (let j = 0, jj = rules.length; j < jj; ++j) {
const {suffixIn, suffixOut, conditionsIn, conditionsOut} = rules[j];
const conditionFlagsIn = this._getConditionFlags(conditionFlagsMap, conditionsIn);
const conditionFlagsIn = this._getConditionFlagsStrict(conditionFlagsMap, conditionsIn);
if (conditionFlagsIn === null) { throw new Error(`Invalid conditionsIn for transform[${i}].rules[${j}]`); }
const conditionFlagsOut = this._getConditionFlags(conditionFlagsMap, conditionsOut);
const conditionFlagsOut = this._getConditionFlagsStrict(conditionFlagsMap, conditionsOut);
if (conditionFlagsOut === null) { throw new Error(`Invalid conditionsOut for transform[${i}].rules[${j}]`); }
rules2.push({
suffixIn,
Expand All @@ -81,52 +81,34 @@ export class LanguageTransformer {
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);
if (condition.isDictionaryForm) {
this._partOfSpeechToConditionFlagsMap.set(type, 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;
return this._getConditionFlags(this._partOfSpeechToConditionFlagsMap, partsOfSpeech);
}

/**
* @param {string} conditionType
* @param {string[]} conditionTypes
* @returns {number}
*/
getConditionFlagsFromConditionType(conditionType) {
const conditionFlags = this._conditionTypeToConditionFlagsMap.get(conditionType);
return typeof conditionFlags !== 'undefined' ? conditionFlags : 0;
getConditionFlagsFromConditionTypes(conditionTypes) {
return this._getConditionFlags(this._conditionTypeToConditionFlagsMap, conditionTypes);
}

/**
* @param {string[]} conditionTypes
* @param {string} conditionType
* @returns {number}
*/
getConditionFlagsFromConditionTypes(conditionTypes) {
let result = 0;
for (const conditionType of conditionTypes) {
result |= this.getConditionFlagsFromConditionType(conditionType);
}
return result;
getConditionFlagsFromConditionType(conditionType) {
return this._getConditionFlags(this._conditionTypeToConditionFlagsMap, [conditionType]);
}

/**
Expand Down Expand Up @@ -182,7 +164,7 @@ export class LanguageTransformer {
flags = 1 << nextFlagIndex;
++nextFlagIndex;
} else {
const multiFlags = this._getConditionFlags(conditionFlagsMap, subConditions);
const multiFlags = this._getConditionFlagsStrict(conditionFlagsMap, subConditions);
if (multiFlags === null) {
nextTargets.push(target);
continue;
Expand All @@ -206,11 +188,30 @@ export class LanguageTransformer {
* @param {string[]} conditionTypes
* @returns {?number}
*/
_getConditionFlags(conditionFlagsMap, conditionTypes) {
_getConditionFlagsStrict(conditionFlagsMap, conditionTypes) {
let flags = 0;
for (const conditionType of conditionTypes) {
const flags2 = conditionFlagsMap.get(conditionType);
if (typeof flags2 === 'undefined') { return null; }
if (typeof flags2 === 'undefined') {
return null;
}
flags |= flags2;
}
return flags;
}

/**
* @param {Map<string, number>} conditionFlagsMap
* @param {string[]} conditionTypes
* @returns {number}
*/
_getConditionFlags(conditionFlagsMap, conditionTypes) {
let flags = 0;
for (const conditionType of conditionTypes) {
let flags2 = conditionFlagsMap.get(conditionType);
if (typeof flags2 === 'undefined') {
flags2 = 0;
}
flags |= flags2;
}
return flags;
Expand Down
2 changes: 1 addition & 1 deletion types/ext/language-transformer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ConditionMapEntries = ConditionMapEntry[];

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

0 comments on commit 70d8cb4

Please sign in to comment.