UNPKG

speech-rule-engine

Version:

A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.

189 lines 6.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.EnginePromise = exports.Engine = exports.SREError = void 0; const Dcstr = __importStar(require("../rule_engine/dynamic_cstr.js")); const EngineConst = __importStar(require("./engine_const.js")); const FileUtil = __importStar(require("./file_util.js")); const system_external_js_1 = require("./system_external.js"); const debugger_js_1 = require("./debugger.js"); const variables_js_1 = require("./variables.js"); const options_js_1 = require("./options.js"); class SREError extends Error { constructor(message = '') { super(); this.message = message; this.name = 'SRE Error'; } } exports.SREError = SREError; class Engine { set defaultLocale(loc) { this._defaultLocale = variables_js_1.Variables.ensureLocale(loc, this._defaultLocale); } get defaultLocale() { return this._defaultLocale; } static getInstance() { Engine.instance = Engine.instance || new Engine(); return Engine.instance; } static defaultEvaluator(str, _cstr) { return str; } static evaluateNode(node) { return Engine.nodeEvaluator(node); } getRate() { const numeric = parseInt(this.options.rate, 10); return isNaN(numeric) ? 100 : numeric; } setDynamicCstr(opt_dynamic) { if (this.defaultLocale) { Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE] = this.defaultLocale; } if (opt_dynamic) { const keys = Object.keys(opt_dynamic); for (let i = 0; i < keys.length; i++) { const feature = keys[i]; if (Dcstr.DynamicCstr.DEFAULT_ORDER.indexOf(feature) !== -1) { const value = opt_dynamic[feature]; this.options[feature] = value; } } } const dynamic = [ this.options.locale, this.options.modality, this.options.domain, this.options.style ].join('.'); const fallback = Dcstr.DynamicProperties.createProp([Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.MODALITY]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.DOMAIN]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.STYLE]]); const comparator = this.comparators[this.options.domain]; const parser = this.parsers[this.options.domain]; this.parser = parser ? parser : this.defaultParser; this.dynamicCstr = this.parser.parse(dynamic); this.dynamicCstr.updateProperties(fallback.getProperties()); this.comparator = comparator ? comparator() : new Dcstr.DefaultComparator(this.dynamicCstr); } constructor() { this.options = new options_js_1.Options(); this.config = false; this.customLoader = null; this.parsers = {}; this.comparator = null; this.mode = EngineConst.Mode.SYNC; this.init = true; this.comparators = {}; this._defaultLocale = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE]; this.options.locale = this.defaultLocale; this.evaluator = Engine.defaultEvaluator; this.defaultParser = new Dcstr.DynamicCstrParser(Dcstr.DynamicCstr.DEFAULT_ORDER); this.parser = this.defaultParser; this.dynamicCstr = Dcstr.DynamicCstr.defaultCstr(); } configurate(feature) { if (this.mode === EngineConst.Mode.HTTP && !system_external_js_1.SystemExternal.webworker && !this.config) { configBlocks(feature); this.config = true; } configFeature(feature); } setCustomLoader(fn) { if (fn) { this.customLoader = fn; } } setup(feature) { if (typeof feature['mode'] !== 'undefined') { this.mode = feature['mode']; } this.configurate(feature); this.options.set(feature); if (feature.json) { system_external_js_1.SystemExternal.jsonPath = FileUtil.makePath(feature.json); } this.setCustomLoader(feature.custom); } json() { return Object.assign({ 'mode': this.mode }, this.options.json()); } reset() { this.options = new options_js_1.Options(); } } exports.Engine = Engine; Engine.nodeEvaluator = function (_node) { return []; }; function configFeature(feature) { if (typeof SREfeature !== 'undefined') { for (const [name, feat] of Object.entries(SREfeature)) { feature[name] = feat; } } } function configBlocks(feature) { const scripts = document.documentElement.querySelectorAll('script[type="text/x-sre-config"]'); for (let i = 0, m = scripts.length; i < m; i++) { let inner; try { inner = scripts[i].innerHTML; const config = JSON.parse(inner); for (const [key, val] of Object.entries(config)) { feature[key] = val; } } catch (_err) { debugger_js_1.Debugger.getInstance().output('Illegal configuration ', inner); } } } class EnginePromise { static get(locale = Engine.getInstance().options.locale) { return EnginePromise.promises[locale] || Promise.resolve(''); } static getall() { return Promise.all(Object.values(EnginePromise.promises)); } } exports.EnginePromise = EnginePromise; EnginePromise.loaded = {}; EnginePromise.promises = {}; //# sourceMappingURL=engine.js.map