semantic_annotator.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SemanticVisitor = exports.SemanticAnnotator = void 0;
  4. class SemanticAnnotator {
  5. constructor(domain, name, func) {
  6. this.domain = domain;
  7. this.name = name;
  8. this.func = func;
  9. this.active = false;
  10. }
  11. annotate(node) {
  12. node.childNodes.forEach(this.annotate.bind(this));
  13. node.contentNodes.forEach(this.annotate.bind(this));
  14. node.addAnnotation(this.domain, this.func(node));
  15. }
  16. }
  17. exports.SemanticAnnotator = SemanticAnnotator;
  18. class SemanticVisitor {
  19. constructor(domain, name, func, def = {}) {
  20. this.domain = domain;
  21. this.name = name;
  22. this.func = func;
  23. this.def = def;
  24. this.active = false;
  25. }
  26. visit(node, info) {
  27. let result = this.func(node, info);
  28. node.addAnnotation(this.domain, result[0]);
  29. for (let i = 0, child; (child = node.childNodes[i]); i++) {
  30. result = this.visit(child, result[1]);
  31. }
  32. for (let i = 0, content; (content = node.contentNodes[i]); i++) {
  33. result = this.visit(content, result[1]);
  34. }
  35. return result;
  36. }
  37. }
  38. exports.SemanticVisitor = SemanticVisitor;