speech-rule-engine
Version:
A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.
76 lines (75 loc) • 2.27 kB
JavaScript
import { SemanticType } from './semantic_meaning.js';
import { SemanticDefault } from './semantic_default.js';
import { SemanticNodeCollator } from './semantic_default.js';
import { SemanticNode } from './semantic_node.js';
export class SemanticNodeFactory {
constructor() {
this.leafMap = new SemanticNodeCollator();
this.defaultMap = new SemanticDefault();
this.idCounter_ = -1;
}
makeNode(id) {
return this.createNode_(id);
}
makeUnprocessed(mml) {
const node = this.createNode_();
node.mathml = [mml];
node.mathmlTree = mml;
return node;
}
makeEmptyNode() {
const node = this.createNode_();
node.type = SemanticType.EMPTY;
return node;
}
makeContentNode(content) {
const node = this.createNode_();
node.updateContent(content);
return node;
}
makeMultipleContentNodes(num, content) {
const nodes = [];
for (let i = 0; i < num; i++) {
nodes.push(this.makeContentNode(content));
}
return nodes;
}
makeLeafNode(content, font) {
if (!content) {
return this.makeEmptyNode();
}
const node = this.makeContentNode(content);
node.font = font || node.font;
const meaning = this.defaultMap.getNode(node);
if (meaning) {
node.type = meaning.type;
node.role = meaning.role;
node.font = meaning.font;
}
this.leafMap.addNode(node);
return node;
}
makeBranchNode(type, children, contentNodes, opt_content) {
const node = this.createNode_();
if (opt_content) {
node.updateContent(opt_content);
}
node.type = type;
node.childNodes = children;
node.contentNodes = contentNodes;
children.concat(contentNodes).forEach(function (x) {
x.parent = node;
node.addMathmlNodes(x.mathml);
});
return node;
}
createNode_(id) {
if (typeof id !== 'undefined') {
this.idCounter_ = Math.max(this.idCounter_, id);
}
else {
id = ++this.idCounter_;
}
return new SemanticNode(id);
}
}