semantic_annotator.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. export class SemanticAnnotator {
  2. constructor(domain, name, func) {
  3. this.domain = domain;
  4. this.name = name;
  5. this.func = func;
  6. this.active = false;
  7. }
  8. annotate(node) {
  9. node.childNodes.forEach(this.annotate.bind(this));
  10. node.contentNodes.forEach(this.annotate.bind(this));
  11. node.addAnnotation(this.domain, this.func(node));
  12. }
  13. }
  14. export class SemanticVisitor {
  15. constructor(domain, name, func, def = {}) {
  16. this.domain = domain;
  17. this.name = name;
  18. this.func = func;
  19. this.def = def;
  20. this.active = false;
  21. }
  22. visit(node, info) {
  23. let result = this.func(node, info);
  24. node.addAnnotation(this.domain, result[0]);
  25. for (let i = 0, child; (child = node.childNodes[i]); i++) {
  26. result = this.visit(child, result[1]);
  27. }
  28. for (let i = 0, content; (content = node.contentNodes[i]); i++) {
  29. result = this.visit(content, result[1]);
  30. }
  31. return result;
  32. }
  33. }