speech-rule-engine
Version:
A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.
195 lines • 8.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const engine_js_1 = require("../common/engine.js");
const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
const semantic_annotations_js_1 = require("../semantic_tree/semantic_annotations.js");
const semantic_annotator_js_1 = require("../semantic_tree/semantic_annotator.js");
function isSimpleExpression(node) {
return (isSimpleNumber_(node) ||
isSimpleLetters_(node) ||
isSimpleDegree_(node) ||
isSimpleNegative_(node) ||
isSimpleFunction_(node));
}
function isSimpleFunction_(node) {
return (node.type === semantic_meaning_js_1.SemanticType.APPL &&
(node.childNodes[0].role === semantic_meaning_js_1.SemanticRole.PREFIXFUNC ||
node.childNodes[0].role === semantic_meaning_js_1.SemanticRole.SIMPLEFUNC) &&
(isSimple_(node.childNodes[1]) ||
(node.childNodes[1].type === semantic_meaning_js_1.SemanticType.FENCED &&
isSimple_(node.childNodes[1].childNodes[0]))));
}
function isSimpleNegative_(node) {
return (node.type === semantic_meaning_js_1.SemanticType.PREFIXOP &&
node.role === semantic_meaning_js_1.SemanticRole.NEGATIVE &&
isSimple_(node.childNodes[0]) &&
node.childNodes[0].type !== semantic_meaning_js_1.SemanticType.PREFIXOP &&
node.childNodes[0].type !== semantic_meaning_js_1.SemanticType.APPL &&
node.childNodes[0].type !== semantic_meaning_js_1.SemanticType.PUNCTUATED);
}
function isSimpleDegree_(node) {
return (node.type === semantic_meaning_js_1.SemanticType.PUNCTUATED &&
node.role === semantic_meaning_js_1.SemanticRole.ENDPUNCT &&
node.childNodes.length === 2 &&
node.childNodes[1].role === semantic_meaning_js_1.SemanticRole.DEGREE &&
(isLetter_(node.childNodes[0]) ||
isNumber_(node.childNodes[0]) ||
(node.childNodes[0].type === semantic_meaning_js_1.SemanticType.PREFIXOP &&
node.childNodes[0].role === semantic_meaning_js_1.SemanticRole.NEGATIVE &&
(isLetter_(node.childNodes[0].childNodes[0]) ||
isNumber_(node.childNodes[0].childNodes[0])))));
}
function isSimpleLetters_(node) {
return (isLetter_(node) ||
(node.type === semantic_meaning_js_1.SemanticType.INFIXOP &&
node.role === semantic_meaning_js_1.SemanticRole.IMPLICIT &&
((node.childNodes.length === 2 &&
(isLetter_(node.childNodes[0]) ||
isSimpleNumber_(node.childNodes[0])) &&
isLetter_(node.childNodes[1])) ||
(node.childNodes.length === 3 &&
isSimpleNumber_(node.childNodes[0]) &&
isLetter_(node.childNodes[1]) &&
isLetter_(node.childNodes[2])))));
}
function isSimple_(node) {
return node.hasAnnotation('clearspeak', 'simple');
}
function isLetter_(node) {
return (node.type === semantic_meaning_js_1.SemanticType.IDENTIFIER &&
(node.role === semantic_meaning_js_1.SemanticRole.LATINLETTER ||
node.role === semantic_meaning_js_1.SemanticRole.GREEKLETTER ||
node.role === semantic_meaning_js_1.SemanticRole.OTHERLETTER ||
node.role === semantic_meaning_js_1.SemanticRole.SIMPLEFUNC));
}
function isNumber_(node) {
return (node.type === semantic_meaning_js_1.SemanticType.NUMBER &&
(node.role === semantic_meaning_js_1.SemanticRole.INTEGER || node.role === semantic_meaning_js_1.SemanticRole.FLOAT));
}
function isSimpleNumber_(node) {
return isNumber_(node) || isSimpleFraction_(node);
}
function isSimpleFraction_(node) {
if (hasPreference('Fraction_Over') || hasPreference('Fraction_FracOver')) {
return false;
}
if (node.type !== semantic_meaning_js_1.SemanticType.FRACTION ||
node.role !== semantic_meaning_js_1.SemanticRole.VULGAR) {
return false;
}
if (hasPreference('Fraction_Ordinal')) {
return true;
}
const enumerator = parseInt(node.childNodes[0].textContent, 10);
const denominator = parseInt(node.childNodes[1].textContent, 10);
return (enumerator > 0 && enumerator < 20 && denominator > 0 && denominator < 11);
}
function hasPreference(pref) {
return engine_js_1.Engine.getInstance().options.style === pref;
}
(0, semantic_annotations_js_1.register)(new semantic_annotator_js_1.SemanticAnnotator('clearspeak', 'simple', function (node) {
return isSimpleExpression(node) ? 'simple' : '';
}));
(0, semantic_annotations_js_1.activate)('clearspeak', 'simple');
function isUnitExpression(node) {
return ((node.type === semantic_meaning_js_1.SemanticType.TEXT && node.role !== semantic_meaning_js_1.SemanticRole.LABEL) ||
(node.type === semantic_meaning_js_1.SemanticType.PUNCTUATED &&
node.role === semantic_meaning_js_1.SemanticRole.TEXT &&
isNumber_(node.childNodes[0]) &&
allTextLastContent_(node.childNodes.slice(1))) ||
(node.type === semantic_meaning_js_1.SemanticType.IDENTIFIER &&
node.role === semantic_meaning_js_1.SemanticRole.UNIT) ||
(node.type === semantic_meaning_js_1.SemanticType.INFIXOP &&
(node.role === semantic_meaning_js_1.SemanticRole.IMPLICIT || node.role === semantic_meaning_js_1.SemanticRole.UNIT)));
}
function allTextLastContent_(nodes) {
for (let i = 0; i < nodes.length - 1; i++) {
if (!(nodes[i].type === semantic_meaning_js_1.SemanticType.TEXT && nodes[i].textContent === '')) {
return false;
}
}
return nodes[nodes.length - 1].type === semantic_meaning_js_1.SemanticType.TEXT;
}
(0, semantic_annotations_js_1.register)(new semantic_annotator_js_1.SemanticAnnotator('clearspeak', 'unit', function (node) {
return isUnitExpression(node) ? 'unit' : '';
}));
(0, semantic_annotations_js_1.activate)('clearspeak', 'unit');
const NUMBER_PROPAGATORS = [
semantic_meaning_js_1.SemanticType.MULTIREL,
semantic_meaning_js_1.SemanticType.RELSEQ,
semantic_meaning_js_1.SemanticType.APPL,
semantic_meaning_js_1.SemanticType.ROW,
semantic_meaning_js_1.SemanticType.LINE
];
const NUMBER_INHIBITORS = [
semantic_meaning_js_1.SemanticType.SUBSCRIPT,
semantic_meaning_js_1.SemanticType.SUPERSCRIPT,
semantic_meaning_js_1.SemanticType.OVERSCORE,
semantic_meaning_js_1.SemanticType.UNDERSCORE
];
function checkParent(node, info) {
const parent = node.parent;
if (!parent) {
return false;
}
const type = parent.type;
if (NUMBER_PROPAGATORS.indexOf(type) !== -1 ||
(type === semantic_meaning_js_1.SemanticType.PREFIXOP &&
parent.role === semantic_meaning_js_1.SemanticRole.NEGATIVE &&
!info.script &&
!info.enclosed) ||
(type === semantic_meaning_js_1.SemanticType.PREFIXOP &&
parent.role === semantic_meaning_js_1.SemanticRole.GEOMETRY)) {
return true;
}
if (type === semantic_meaning_js_1.SemanticType.PUNCTUATED) {
if (!info.enclosed || parent.role === semantic_meaning_js_1.SemanticRole.TEXT) {
return true;
}
}
return false;
}
function propagateNumber(node, info) {
if (!node.childNodes.length) {
if (checkParent(node, info)) {
info.number = true;
info.script = false;
info.enclosed = false;
}
return [
info['number'] ? 'number' : '',
{ number: false, enclosed: info.enclosed, script: info.script }
];
}
if (NUMBER_INHIBITORS.indexOf(node.type) !== -1) {
info.script = true;
}
if (node.type === semantic_meaning_js_1.SemanticType.FENCED) {
info.number = false;
info.enclosed = true;
return ['', info];
}
if (node.type === semantic_meaning_js_1.SemanticType.PREFIXOP &&
node.role !== semantic_meaning_js_1.SemanticRole.GEOMETRY &&
node.role !== semantic_meaning_js_1.SemanticRole.NEGATIVE) {
info.number = false;
return ['', info];
}
if (checkParent(node, info)) {
info.number = true;
info.enclosed = false;
}
return ['', info];
}
(0, semantic_annotations_js_1.register)(new semantic_annotator_js_1.SemanticVisitor('nemeth', 'number', propagateNumber, { number: true }));
(0, semantic_annotations_js_1.activate)('nemeth', 'number');
function annotateDepth(node) {
if (!node.parent) {
return [1];
}
const depth = parseInt(node.parent.annotation['depth'][0]);
return [depth + 1];
}
(0, semantic_annotations_js_1.register)(new semantic_annotator_js_1.SemanticVisitor('depth', 'depth', annotateDepth));
(0, semantic_annotations_js_1.activate)('depth', 'depth');
//# sourceMappingURL=special_annotators.js.map