add initial marp implementation with sample content and build configuration
This commit is contained in:
25
node_modules/speech-rule-engine/js/indexing/abstract_trie_node.d.ts
generated
vendored
Normal file
25
node_modules/speech-rule-engine/js/indexing/abstract_trie_node.d.ts
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
import { SpeechRule } from '../rule_engine/speech_rule.js';
|
||||
import { TrieNode, TrieNodeKind } from './trie_node.js';
|
||||
export declare class AbstractTrieNode<T> implements TrieNode {
|
||||
constraint: string;
|
||||
test: ((p1: T) => boolean) | null;
|
||||
kind: TrieNodeKind;
|
||||
private children_;
|
||||
constructor(constraint: string, test: ((p1: T) => boolean) | null);
|
||||
getConstraint(): string;
|
||||
getKind(): TrieNodeKind;
|
||||
applyTest(object: T): boolean;
|
||||
addChild(node: TrieNode): TrieNode;
|
||||
getChild(constraint: string): TrieNode;
|
||||
getChildren(): TrieNode[];
|
||||
findChildren(object: T): TrieNode[];
|
||||
removeChild(constraint: string): void;
|
||||
toString(): string;
|
||||
}
|
||||
export declare class StaticTrieNode extends AbstractTrieNode<Node> {
|
||||
private rule_;
|
||||
constructor(constraint: string, test: ((p1: Element) => boolean) | null);
|
||||
getRule(): SpeechRule | null;
|
||||
setRule(rule: SpeechRule): void;
|
||||
toString(): string;
|
||||
}
|
||||
77
node_modules/speech-rule-engine/js/indexing/abstract_trie_node.js
generated
vendored
Normal file
77
node_modules/speech-rule-engine/js/indexing/abstract_trie_node.js
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StaticTrieNode = exports.AbstractTrieNode = void 0;
|
||||
const debugger_js_1 = require("../common/debugger.js");
|
||||
const trie_node_js_1 = require("./trie_node.js");
|
||||
class AbstractTrieNode {
|
||||
constructor(constraint, test) {
|
||||
this.constraint = constraint;
|
||||
this.test = test;
|
||||
this.children_ = {};
|
||||
this.kind = trie_node_js_1.TrieNodeKind.ROOT;
|
||||
}
|
||||
getConstraint() {
|
||||
return this.constraint;
|
||||
}
|
||||
getKind() {
|
||||
return this.kind;
|
||||
}
|
||||
applyTest(object) {
|
||||
return this.test(object);
|
||||
}
|
||||
addChild(node) {
|
||||
const constraint = node.getConstraint();
|
||||
const child = this.children_[constraint];
|
||||
this.children_[constraint] = node;
|
||||
return child;
|
||||
}
|
||||
getChild(constraint) {
|
||||
return this.children_[constraint];
|
||||
}
|
||||
getChildren() {
|
||||
const children = [];
|
||||
for (const val of Object.values(this.children_)) {
|
||||
children.push(val);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
findChildren(object) {
|
||||
const children = [];
|
||||
for (const val of Object.values(this.children_)) {
|
||||
if (val.applyTest(object)) {
|
||||
children.push(val);
|
||||
}
|
||||
}
|
||||
return children;
|
||||
}
|
||||
removeChild(constraint) {
|
||||
delete this.children_[constraint];
|
||||
}
|
||||
toString() {
|
||||
return this.constraint;
|
||||
}
|
||||
}
|
||||
exports.AbstractTrieNode = AbstractTrieNode;
|
||||
class StaticTrieNode extends AbstractTrieNode {
|
||||
constructor(constraint, test) {
|
||||
super(constraint, test);
|
||||
this.rule_ = null;
|
||||
this.kind = trie_node_js_1.TrieNodeKind.STATIC;
|
||||
}
|
||||
getRule() {
|
||||
return this.rule_;
|
||||
}
|
||||
setRule(rule) {
|
||||
if (this.rule_) {
|
||||
debugger_js_1.Debugger.getInstance().output('Replacing rule ' + this.rule_ + ' with ' + rule);
|
||||
}
|
||||
this.rule_ = rule;
|
||||
}
|
||||
toString() {
|
||||
const rule = this.getRule();
|
||||
return rule
|
||||
? this.constraint + '\n' + '==> ' + this.getRule().action
|
||||
: this.constraint;
|
||||
}
|
||||
}
|
||||
exports.StaticTrieNode = StaticTrieNode;
|
||||
23
node_modules/speech-rule-engine/js/indexing/trie.d.ts
generated
vendored
Normal file
23
node_modules/speech-rule-engine/js/indexing/trie.d.ts
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
import { SpeechRule } from '../rule_engine/speech_rule.js';
|
||||
import { TrieNode } from './trie_node.js';
|
||||
export declare class Trie {
|
||||
root: TrieNode;
|
||||
static collectRules_(root: TrieNode): SpeechRule[];
|
||||
private static printWithDepth_;
|
||||
private static order_;
|
||||
constructor();
|
||||
addRule(rule: SpeechRule): void;
|
||||
lookupRules(xml: Element, dynamic: string[][]): SpeechRule[];
|
||||
hasSubtrie(cstrs: string[]): boolean;
|
||||
toString(): string;
|
||||
collectRules(root?: TrieNode): SpeechRule[];
|
||||
order(): number;
|
||||
enumerate(opt_info?: {
|
||||
[key: string]: any;
|
||||
}): {
|
||||
[key: string]: any;
|
||||
};
|
||||
byConstraint(constraint: string[]): TrieNode;
|
||||
private enumerate_;
|
||||
private addNode_;
|
||||
}
|
||||
139
node_modules/speech-rule-engine/js/indexing/trie.js
generated
vendored
Normal file
139
node_modules/speech-rule-engine/js/indexing/trie.js
generated
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Trie = void 0;
|
||||
const trie_node_js_1 = require("./trie_node.js");
|
||||
const trie_node_factory_js_1 = require("./trie_node_factory.js");
|
||||
class Trie {
|
||||
static collectRules_(root) {
|
||||
const rules = [];
|
||||
let explore = [root];
|
||||
while (explore.length) {
|
||||
const node = explore.shift();
|
||||
if (node.getKind() === trie_node_js_1.TrieNodeKind.QUERY ||
|
||||
node.getKind() === trie_node_js_1.TrieNodeKind.BOOLEAN) {
|
||||
const rule = node.getRule();
|
||||
if (rule) {
|
||||
rules.unshift(rule);
|
||||
}
|
||||
}
|
||||
explore = explore.concat(node.getChildren());
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
static printWithDepth_(node, depth, str) {
|
||||
const prefix = new Array(depth + 2).join(depth.toString()) + ': ';
|
||||
str += prefix + node.toString() + '\n';
|
||||
const children = node.getChildren();
|
||||
for (let i = 0, child; (child = children[i]); i++) {
|
||||
str = Trie.printWithDepth_(child, depth + 1, str);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
static order_(node) {
|
||||
const children = node.getChildren();
|
||||
if (!children.length) {
|
||||
return 0;
|
||||
}
|
||||
const max = Math.max.apply(null, children.map(Trie.order_));
|
||||
return Math.max(children.length, max);
|
||||
}
|
||||
constructor() {
|
||||
this.root = (0, trie_node_factory_js_1.getNode)(trie_node_js_1.TrieNodeKind.ROOT, '', null);
|
||||
}
|
||||
addRule(rule) {
|
||||
let node = this.root;
|
||||
const context = rule.context;
|
||||
const dynamicCstr = rule.dynamicCstr.getValues();
|
||||
for (let i = 0, l = dynamicCstr.length; i < l; i++) {
|
||||
node = this.addNode_(node, dynamicCstr[i], trie_node_js_1.TrieNodeKind.DYNAMIC, context);
|
||||
}
|
||||
node = this.addNode_(node, rule.precondition.query, trie_node_js_1.TrieNodeKind.QUERY, context);
|
||||
const booleans = rule.precondition.constraints;
|
||||
for (let i = 0, l = booleans.length; i < l; i++) {
|
||||
node = this.addNode_(node, booleans[i], trie_node_js_1.TrieNodeKind.BOOLEAN, context);
|
||||
}
|
||||
node.setRule(rule);
|
||||
}
|
||||
lookupRules(xml, dynamic) {
|
||||
let nodes = [this.root];
|
||||
const rules = [];
|
||||
while (dynamic.length) {
|
||||
const dynamicSet = dynamic.shift();
|
||||
const newNodes = [];
|
||||
while (nodes.length) {
|
||||
const node = nodes.shift();
|
||||
const children = node.getChildren();
|
||||
children.forEach((child) => {
|
||||
if (child.getKind() !== trie_node_js_1.TrieNodeKind.DYNAMIC ||
|
||||
dynamicSet.indexOf(child.getConstraint()) !== -1) {
|
||||
newNodes.push(child);
|
||||
}
|
||||
});
|
||||
}
|
||||
nodes = newNodes.slice();
|
||||
}
|
||||
while (nodes.length) {
|
||||
const node = nodes.shift();
|
||||
if (node.getRule) {
|
||||
const rule = node.getRule();
|
||||
if (rule) {
|
||||
rules.push(rule);
|
||||
}
|
||||
}
|
||||
const children = node.findChildren(xml);
|
||||
nodes = nodes.concat(children);
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
hasSubtrie(cstrs) {
|
||||
let subtrie = this.root;
|
||||
for (let i = 0, l = cstrs.length; i < l; i++) {
|
||||
const cstr = cstrs[i];
|
||||
subtrie = subtrie.getChild(cstr);
|
||||
if (!subtrie) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
toString() {
|
||||
return Trie.printWithDepth_(this.root, 0, '');
|
||||
}
|
||||
collectRules(root = this.root) {
|
||||
return Trie.collectRules_(root);
|
||||
}
|
||||
order() {
|
||||
return Trie.order_(this.root);
|
||||
}
|
||||
enumerate(opt_info) {
|
||||
return this.enumerate_(this.root, opt_info);
|
||||
}
|
||||
byConstraint(constraint) {
|
||||
let node = this.root;
|
||||
while (constraint.length && node) {
|
||||
const cstr = constraint.shift();
|
||||
node = node.getChild(cstr);
|
||||
}
|
||||
return node || null;
|
||||
}
|
||||
enumerate_(node, info) {
|
||||
info = info || {};
|
||||
const children = node.getChildren();
|
||||
for (let i = 0, child; (child = children[i]); i++) {
|
||||
if (child.kind !== trie_node_js_1.TrieNodeKind.DYNAMIC) {
|
||||
continue;
|
||||
}
|
||||
info[child.getConstraint()] = this.enumerate_(child, info[child.getConstraint()]);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
addNode_(node, constraint, kind, context) {
|
||||
let nextNode = node.getChild(constraint);
|
||||
if (!nextNode) {
|
||||
nextNode = (0, trie_node_factory_js_1.getNode)(kind, constraint, context);
|
||||
node.addChild(nextNode);
|
||||
}
|
||||
return nextNode;
|
||||
}
|
||||
}
|
||||
exports.Trie = Trie;
|
||||
18
node_modules/speech-rule-engine/js/indexing/trie_node.d.ts
generated
vendored
Normal file
18
node_modules/speech-rule-engine/js/indexing/trie_node.d.ts
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
export interface TrieNode {
|
||||
kind: TrieNodeKind;
|
||||
getConstraint(): string;
|
||||
getKind(): TrieNodeKind;
|
||||
applyTest(object: any): boolean;
|
||||
addChild(node: TrieNode): TrieNode | null;
|
||||
getChild(constraint: string): TrieNode | null;
|
||||
getChildren(): TrieNode[];
|
||||
findChildren(object: any): TrieNode[];
|
||||
removeChild(constraint: string): void;
|
||||
}
|
||||
export declare enum TrieNodeKind {
|
||||
ROOT = "root",
|
||||
DYNAMIC = "dynamic",
|
||||
QUERY = "query",
|
||||
BOOLEAN = "boolean",
|
||||
STATIC = "static"
|
||||
}
|
||||
11
node_modules/speech-rule-engine/js/indexing/trie_node.js
generated
vendored
Normal file
11
node_modules/speech-rule-engine/js/indexing/trie_node.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TrieNodeKind = void 0;
|
||||
var TrieNodeKind;
|
||||
(function (TrieNodeKind) {
|
||||
TrieNodeKind["ROOT"] = "root";
|
||||
TrieNodeKind["DYNAMIC"] = "dynamic";
|
||||
TrieNodeKind["QUERY"] = "query";
|
||||
TrieNodeKind["BOOLEAN"] = "boolean";
|
||||
TrieNodeKind["STATIC"] = "static";
|
||||
})(TrieNodeKind || (exports.TrieNodeKind = TrieNodeKind = {}));
|
||||
3
node_modules/speech-rule-engine/js/indexing/trie_node_factory.d.ts
generated
vendored
Normal file
3
node_modules/speech-rule-engine/js/indexing/trie_node_factory.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { SpeechRuleContext } from '../rule_engine/speech_rule_context.js';
|
||||
import { TrieNode, TrieNodeKind } from './trie_node.js';
|
||||
export declare function getNode(kind: TrieNodeKind, constraint: string, context: SpeechRuleContext): TrieNode | null;
|
||||
162
node_modules/speech-rule-engine/js/indexing/trie_node_factory.js
generated
vendored
Normal file
162
node_modules/speech-rule-engine/js/indexing/trie_node_factory.js
generated
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getNode = getNode;
|
||||
const DomUtil = require("../common/dom_util.js");
|
||||
const XpathUtil = require("../common/xpath_util.js");
|
||||
const grammar_js_1 = require("../rule_engine/grammar.js");
|
||||
const MathCompoundStore = require("../rule_engine/math_compound_store.js");
|
||||
const abstract_trie_node_js_1 = require("./abstract_trie_node.js");
|
||||
const abstract_trie_node_js_2 = require("./abstract_trie_node.js");
|
||||
const trie_node_js_1 = require("./trie_node.js");
|
||||
function getNode(kind, constraint, context) {
|
||||
switch (kind) {
|
||||
case trie_node_js_1.TrieNodeKind.ROOT:
|
||||
return new RootTrieNode();
|
||||
case trie_node_js_1.TrieNodeKind.DYNAMIC:
|
||||
return new DynamicTrieNode(constraint);
|
||||
case trie_node_js_1.TrieNodeKind.QUERY:
|
||||
return new QueryTrieNode(constraint, context);
|
||||
case trie_node_js_1.TrieNodeKind.BOOLEAN:
|
||||
return new BooleanTrieNode(constraint, context);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
class RootTrieNode extends abstract_trie_node_js_1.AbstractTrieNode {
|
||||
constructor() {
|
||||
super('', () => true);
|
||||
this.kind = trie_node_js_1.TrieNodeKind.ROOT;
|
||||
}
|
||||
}
|
||||
class DynamicTrieNode extends abstract_trie_node_js_1.AbstractTrieNode {
|
||||
constructor(constraint) {
|
||||
super(constraint, (axis) => axis === constraint);
|
||||
this.kind = trie_node_js_1.TrieNodeKind.DYNAMIC;
|
||||
}
|
||||
}
|
||||
const comparator = {
|
||||
'=': (x, y) => x === y,
|
||||
'!=': (x, y) => x !== y,
|
||||
'<': (x, y) => x < y,
|
||||
'>': (x, y) => x > y,
|
||||
'<=': (x, y) => x <= y,
|
||||
'>=': (x, y) => x >= y
|
||||
};
|
||||
function constraintTest(constraint) {
|
||||
if (constraint.match(/^self::\*$/)) {
|
||||
return (_node) => true;
|
||||
}
|
||||
if (constraint.match(/^self::\w+$/)) {
|
||||
const tag = constraint.slice(6).toUpperCase();
|
||||
return (node) => node.tagName && DomUtil.tagName(node) === tag;
|
||||
}
|
||||
if (constraint.match(/^self::\w+:\w+$/)) {
|
||||
const inter = constraint.split(':');
|
||||
const namespace = XpathUtil.resolveNameSpace(inter[2]);
|
||||
if (!namespace) {
|
||||
return null;
|
||||
}
|
||||
const tag = inter[3].toUpperCase();
|
||||
return (node) => node.localName &&
|
||||
node.localName.toUpperCase() === tag &&
|
||||
node.namespaceURI === namespace;
|
||||
}
|
||||
if (constraint.match(/^@\w+$/)) {
|
||||
const attr = constraint.slice(1);
|
||||
return (node) => node.hasAttribute && node.hasAttribute(attr);
|
||||
}
|
||||
if (constraint.match(/^@\w+="[\w\d ]+"$/)) {
|
||||
const split = constraint.split('=');
|
||||
const attr = split[0].slice(1);
|
||||
const value = split[1].slice(1, -1);
|
||||
return (node) => node.hasAttribute &&
|
||||
node.hasAttribute(attr) &&
|
||||
node.getAttribute(attr) === value;
|
||||
}
|
||||
if (constraint.match(/^@\w+!="[\w\d ]+"$/)) {
|
||||
const split = constraint.split('!=');
|
||||
const attr = split[0].slice(1);
|
||||
const value = split[1].slice(1, -1);
|
||||
return (node) => !node.hasAttribute ||
|
||||
!node.hasAttribute(attr) ||
|
||||
node.getAttribute(attr) !== value;
|
||||
}
|
||||
if (constraint.match(/^contains\(\s*@grammar\s*,\s*"[\w\d ]+"\s*\)$/)) {
|
||||
const split = constraint.split('"');
|
||||
const value = split[1];
|
||||
return (_node) => !!grammar_js_1.Grammar.getInstance().getParameter(value);
|
||||
}
|
||||
if (constraint.match(/^not\(\s*contains\(\s*@grammar\s*,\s*"[\w\d ]+"\s*\)\s*\)$/)) {
|
||||
const split = constraint.split('"');
|
||||
const value = split[1];
|
||||
return (_node) => !grammar_js_1.Grammar.getInstance().getParameter(value);
|
||||
}
|
||||
if (constraint.match(/^name\(\.\.\/\.\.\)="\w+"$/)) {
|
||||
const split = constraint.split('"');
|
||||
const tag = split[1].toUpperCase();
|
||||
return (node) => {
|
||||
var _a, _b;
|
||||
return ((_b = (_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.tagName) &&
|
||||
DomUtil.tagName(node.parentNode.parentNode) === tag;
|
||||
};
|
||||
}
|
||||
if (constraint.match(/^count\(preceding-sibling::\*\)=\d+$/)) {
|
||||
const split = constraint.split('=');
|
||||
const num = parseInt(split[1], 10);
|
||||
return (node) => { var _a; return ((_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes[num]) === node; };
|
||||
}
|
||||
if (constraint.match(/^.+\[@category!?=".+"\]$/)) {
|
||||
let [, query, equality, category] = constraint.match(/^(.+)\[@category(!?=)"(.+)"\]$/);
|
||||
const unit = category.match(/^unit:(.+)$/);
|
||||
let add = '';
|
||||
if (unit) {
|
||||
category = unit[1];
|
||||
add = ':unit';
|
||||
}
|
||||
return (node) => {
|
||||
const xpath = XpathUtil.evalXPath(query, node)[0];
|
||||
if (xpath) {
|
||||
const result = MathCompoundStore.lookupCategory(xpath.textContent + add);
|
||||
return equality === '=' ? result === category : result !== category;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
if (constraint.match(/^string-length\(.+\)\W+\d+/)) {
|
||||
const [, select, comp, count] = constraint.match(/^string-length\((.+)\)(\W+)(\d+)/);
|
||||
const func = comparator[comp] || comparator['='];
|
||||
const numb = parseInt(count, 10);
|
||||
return (node) => {
|
||||
const xpath = XpathUtil.evalXPath(select, node)[0];
|
||||
if (!xpath) {
|
||||
return false;
|
||||
}
|
||||
return func(Array.from(xpath.textContent).length, numb);
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
class QueryTrieNode extends abstract_trie_node_js_2.StaticTrieNode {
|
||||
constructor(constraint, context) {
|
||||
super(constraint, constraintTest(constraint));
|
||||
this.context = context;
|
||||
this.kind = trie_node_js_1.TrieNodeKind.QUERY;
|
||||
}
|
||||
applyTest(object) {
|
||||
return this.test
|
||||
? this.test(object)
|
||||
: this.context.applyQuery(object, this.constraint) === object;
|
||||
}
|
||||
}
|
||||
class BooleanTrieNode extends abstract_trie_node_js_2.StaticTrieNode {
|
||||
constructor(constraint, context) {
|
||||
super(constraint, constraintTest(constraint));
|
||||
this.context = context;
|
||||
this.kind = trie_node_js_1.TrieNodeKind.BOOLEAN;
|
||||
}
|
||||
applyTest(object) {
|
||||
return this.test
|
||||
? this.test(object)
|
||||
: this.context.applyConstraint(object, this.constraint);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user