aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/language-transformer.js
diff options
context:
space:
mode:
authorStefanVukovic99 <stefanvukovic44@gmail.com>2024-06-03 19:25:51 +0200
committerGitHub <noreply@github.com>2024-06-03 17:25:51 +0000
commit7955fc85ac089d856b44bdea78eccd26ffbd690c (patch)
tree6a7cfc6464612f9f129b25fa59a46f9c7c0e72b5 /ext/js/language/language-transformer.js
parentd2fce502ecb00e3c9202295c93fbb8c554ddbd69 (diff)
display inflection rule descriptions (#1000)24.6.3.0
* load descriptions in deinflector * description functions in deinflectors * show descriptions in title * use toaster * use names without internal * css lint * reformat transform descriptors * fix merge errors * done? * rename method
Diffstat (limited to 'ext/js/language/language-transformer.js')
-rw-r--r--ext/js/language/language-transformer.js30
1 files changed, 22 insertions, 8 deletions
diff --git a/ext/js/language/language-transformer.js b/ext/js/language/language-transformer.js
index 76a1b583..d80fa73c 100644
--- a/ext/js/language/language-transformer.js
+++ b/ext/js/language/language-transformer.js
@@ -50,16 +50,17 @@ export class LanguageTransformer {
/** @type {import('language-transformer-internal').Transform[]} */
const transforms2 = [];
- for (let i = 0, ii = transforms.length; i < ii; ++i) {
- const {name, rules} = transforms[i];
+
+ for (const [transformId, transform] of Object.entries(transforms)) {
+ const {name, description, rules} = transform;
/** @type {import('language-transformer-internal').Rule[]} */
const rules2 = [];
for (let j = 0, jj = rules.length; j < jj; ++j) {
const {type, isInflected, deinflect, conditionsIn, conditionsOut} = rules[j];
const conditionFlagsIn = this._getConditionFlagsStrict(conditionFlagsMap, conditionsIn);
- if (conditionFlagsIn === null) { throw new Error(`Invalid conditionsIn for transform[${i}].rules[${j}]`); }
+ if (conditionFlagsIn === null) { throw new Error(`Invalid conditionsIn for transform ${transformId}.rules[${j}]`); }
const conditionFlagsOut = this._getConditionFlagsStrict(conditionFlagsMap, conditionsOut);
- if (conditionFlagsOut === null) { throw new Error(`Invalid conditionsOut for transform[${i}].rules[${j}]`); }
+ if (conditionFlagsOut === null) { throw new Error(`Invalid conditionsOut for transform ${transformId}.rules[${j}]`); }
rules2.push({
type,
isInflected,
@@ -70,7 +71,7 @@ export class LanguageTransformer {
}
const isInflectedTests = rules.map((rule) => rule.isInflected);
const heuristic = new RegExp(isInflectedTests.map((regExp) => regExp.source).join('|'));
- transforms2.push({name, rules: rules2, heuristic});
+ transforms2.push({id: transformId, name, description, rules: rules2, heuristic});
}
this._nextFlagIndex = nextFlagIndex;
@@ -123,14 +124,14 @@ export class LanguageTransformer {
for (const transform of this._transforms) {
if (!transform.heuristic.test(text)) { continue; }
- const {name, rules} = transform;
+ const {id, rules} = transform;
for (let j = 0, jj = rules.length; j < jj; ++j) {
const rule = rules[j];
if (!LanguageTransformer.conditionsMatch(conditions, rule.conditionsIn)) { continue; }
const {isInflected, deinflect} = rule;
if (!isInflected.test(text)) { continue; }
- const isCycle = trace.some((frame) => frame.transform === name && frame.ruleIndex === j && frame.text === text);
+ const isCycle = trace.some((frame) => frame.transform === id && frame.ruleIndex === j && frame.text === text);
if (isCycle) {
log.warn(new Error(`Cycle detected in transform[${name}] rule[${j}] for text: ${text}\nTrace: ${JSON.stringify(trace)}`));
continue;
@@ -139,7 +140,7 @@ export class LanguageTransformer {
results.push(LanguageTransformer.createTransformedText(
deinflect(text),
rule.conditionsOut,
- this._extendTrace(trace, {transform: name, ruleIndex: j, text}),
+ this._extendTrace(trace, {transform: id, ruleIndex: j, text}),
));
}
}
@@ -148,6 +149,19 @@ export class LanguageTransformer {
}
/**
+ * @param {string[]} inflectionRules
+ * @returns {import('dictionary').InflectionRuleChain}
+ */
+ getUserFacingInflectionRules(inflectionRules) {
+ return inflectionRules.map((rule) => {
+ const fullRule = this._transforms.find((transform) => transform.id === rule);
+ if (typeof fullRule === 'undefined') { return {name: rule}; }
+ const {name, description} = fullRule;
+ return description ? {name, description} : {name};
+ });
+ }
+
+ /**
* @param {string} text
* @param {number} conditions
* @param {import('language-transformer-internal').Trace} trace