From c7c8b2855257890cf3e3609d7d4e82dd9c891d65 Mon Sep 17 00:00:00 2001 From: zorkow Date: Tue, 2 Apr 2024 11:42:42 +0200 Subject: [PATCH] Ensure domain existence --- package.json | 9 ++++--- ts/common/engine_setup.ts | 55 ++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 93a02ecdc..2bec772ee 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,14 @@ "postcommonjs": "node -e 'require(\"fs\").writeFileSync(\"cjs/package.json\", \"{\\n \\\"type\\\": \\\"commonjs\\\"\\n}\\n\");'", "module": "pnpm tsc --module nodenext --outDir js --moduleResolution nodenext", "postmodule": "node -e 'require(\"fs\").writeFileSync(\"js/package.json\", \"{\\n \\\"type\\\": \\\"module\\\"\\n}\\n\");'", - "pretest": "git submodule update --remote --merge; pnpm run compile; cd sre-tests; ln -s .. speech-rule-engine; npm install", - "compile": "pnpm run module", + "pretest": "git submodule update --remote --merge; pnpm compile; cd sre-tests; ln -s .. speech-rule-engine; npm install", + "compile": "pnpm module", "clean": "rimraf js lib cjs", "actionTest": "cd sre-tests; export SRE_JSON_PATH=../lib/mathmaps; pnpm jest --silent ", "cleanAll": "make clean; make clean_min", - "prepublish": "pnpm run cleanAll; pnpm run build", - "build": "make -j 12 all; pnpm run commonjs; pnpm run module; pnpm webpack" + "prepublish": "pnpm cleanAll; pnpm buildAll", + "build": "make -j 12 all; pnpm module; pnpm webpack", + "buildAll": "make -j 12 all; pnpm commonjs; pnpm module; pnpm webpack" }, "maintainers": [ "Volker Sorge (http://www.cs.bham.ac.uk/~vxs)" diff --git a/ts/common/engine_setup.ts b/ts/common/engine_setup.ts index 964936a6c..a61b28890 100644 --- a/ts/common/engine_setup.ts +++ b/ts/common/engine_setup.ts @@ -26,7 +26,49 @@ import { Engine, EnginePromise } from './engine.js'; import * as FileUtil from './file_util.js'; import { SystemExternal } from './system_external.js'; +const MATHSPEAK_ONLY: string[] = ['ca', 'da', 'es']; + +const EN_RULES: string[] = [ + 'chromevox', 'clearspeak', 'mathspeak', 'emacspeak', 'html' +]; + +function ensureDomain(feature: { [key: string]: boolean | string }) { +// This preserves the possibility to specify default as domain. +// < 3.2 this lead to the use of chromevox rules in English. +// >= 3.2 this defaults to Mathspeak. It also ensures that in other locales +// we get a meaningful output. + if ((feature.modality && feature.modality !== 'speech') || + (!feature.modality && Engine.getInstance().modality !== 'speech')) { + return; + } + if (!feature.domain) { + return; + } + if (feature.domain === 'default') { + feature.domain = 'mathspeak'; + return; + } + const locale = (feature.locale || Engine.getInstance().locale) as string; + const domain = feature.domain as string; + if (MATHSPEAK_ONLY.indexOf(locale) !== -1) { + if (domain !== 'mathspeak') { + feature.domain = 'mathspeak'; + } + return; + } + if (locale === 'en') { + if (EN_RULES.indexOf(domain) === -1) { + feature.domain = 'mathspeak'; + } + return; + } + if (domain !== 'mathspeak' && domain !== 'clearspeak') { + feature.domain = 'mathspeak'; + } +} + // Engine setup method. + /** * Method to setup and initialize the speech rule engine. Currently the * feature parameter is ignored, however, this could be used to fine tune the @@ -36,19 +78,8 @@ import { SystemExternal } from './system_external.js'; * @returns The promise that resolves once setup is complete. */ export async function setup(feature: { [key: string]: boolean | string }) { + ensureDomain(feature); const engine = Engine.getInstance() as any; - // This preserves the possibility to specify default as domain. - // < 3.2 this lead to the use of chromevox rules in English. - // >= 3.2 this defaults to Mathspeak. It also ensures that in other locales - // we get a meaningful output. - if ( - feature.domain === 'default' && - (feature.modality === 'speech' || - !feature.modality || - engine.modality === 'speech') - ) { - feature.domain = 'mathspeak'; - } const setIf = (feat: string) => { if (typeof feature[feat] !== 'undefined') { engine[feat] = !!feature[feat];