speech-rule-engine
Version:
A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.
75 lines • 2.52 kB
JavaScript
import * as DomUtil from '../common/dom_util.js';
import { markup } from '../audio/aural_rendering.js';
import { Engine } from '../common/engine.js';
export class SpeechStructure {
getSpeechMap(id) {
let map = this.speechMaps.get(id);
if (!map) {
map = new Map();
this.speechMaps.set(id, map);
}
return map;
}
setMap(modality, id, descr) {
const map = this.getSpeechMap(id);
map.set(modality, descr);
}
constructor(node) {
this.node = node;
this.speechMaps = new Map();
this.nodeMap = null;
}
addNode(node, descr, modality = 'speech') {
if (node.nodeType === DomUtil.NodeType.ELEMENT_NODE &&
node.hasAttribute('id')) {
this.setMap(modality, node.getAttribute('id'), descr);
}
}
get(id) {
return this.speechMaps.get(id);
}
getNodeMap() {
if (this.nodeMap) {
return this.nodeMap;
}
this.nodeMap = new Map();
for (const node of DomUtil.querySelectorAllByAttr(this.node, 'id')) {
const id = node.getAttribute('id');
if (!this.nodeMap.has(id)) {
this.nodeMap.set(node.getAttribute('id'), node);
continue;
}
const tag = node.parentNode.tagName;
if (tag === 'children' || tag === 'stree') {
this.nodeMap.set(id, node);
}
}
return this.nodeMap;
}
completeModality(modality, func) {
const oldModality = Engine.getInstance().options.modality;
Engine.getInstance().options.modality = modality;
for (const [id, descrs] of this.getNodeMap()) {
const speechMap = this.getSpeechMap(id);
if (!speechMap.has(modality)) {
func(descrs);
}
}
Engine.getInstance().options.modality = oldModality;
}
json(mls = ['none']) {
const result = {};
const oldMarkup = Engine.getInstance().options.markup;
for (const [id, map] of this.speechMaps) {
const modality = {};
for (const ml of mls) {
Engine.getInstance().options.markup = ml;
map.forEach((x, y) => (modality[`${y}-${ml}`] = markup(x)));
result[id] = modality;
}
}
Engine.getInstance().options.markup = oldMarkup;
return result;
}
}
//# sourceMappingURL=speech_structure.js.map