UNPKG

speech-rule-engine

Version:

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

122 lines 4.47 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.SpeechRuleContext = void 0; const span_js_1 = require("../audio/span.js"); const XpathUtil = __importStar(require("../common/xpath_util.js")); const srf = __importStar(require("./speech_rule_functions.js")); class SpeechRuleContext { constructor() { this.customQueries = new srf.CustomQueries(); this.customStrings = new srf.CustomStrings(); this.contextFunctions = new srf.ContextFunctions(); this.customGenerators = new srf.CustomGenerators(); } applyCustomQuery(node, funcName) { const func = this.customQueries.lookup(funcName); return func ? func(node) : null; } applySelector(node, expr) { const result = this.applyCustomQuery(node, expr); return result || XpathUtil.evalXPath(expr, node); } applyQuery(node, expr) { const results = this.applySelector(node, expr); if (results.length > 0) { return results[0]; } return null; } applyConstraint(node, expr) { const result = this.applyQuery(node, expr); return !!result || XpathUtil.evaluateBoolean(expr, node); } constructString(node, expr) { const result = this.constructString_(node, expr); return Array.isArray(result) ? result.map((x) => x.speech).join('') : result; } constructSpan(node, expr, def) { const result = this.constructString_(node, expr); if (Array.isArray(result)) { const last = result[result.length - 1]; last.attributes = Object.assign({}, def, last.attributes); return result; } else { return [span_js_1.Span.node(result, node, def)]; } } constructString_(node, expr) { if (!expr) { return ''; } if (expr.charAt(0) === '"') { return expr.slice(1, -1); } const func = this.customStrings.lookup(expr); if (func) { return func(node); } return XpathUtil.evaluateString(expr, node); } parse(functions) { const functs = Array.isArray(functions) ? functions : Object.entries(functions); for (const func of functs) { const kind = func[0].slice(0, 3); switch (kind) { case 'CQF': this.customQueries.add(func[0], func[1]); break; case 'CSF': this.customStrings.add(func[0], func[1]); break; case 'CTF': this.contextFunctions.add(func[0], func[1]); break; case 'CGF': this.customGenerators.add(func[0], func[1]); break; default: console.error('FunctionError: Invalid function name ' + func[0]); } } } } exports.SpeechRuleContext = SpeechRuleContext; //# sourceMappingURL=speech_rule_context.js.map