semantic_tree.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SemanticTree = void 0;
  4. const DomUtil = require("../common/dom_util.js");
  5. const semantic_annotations_js_1 = require("./semantic_annotations.js");
  6. const semantic_annotator_js_1 = require("./semantic_annotator.js");
  7. const semantic_meaning_js_1 = require("./semantic_meaning.js");
  8. const semantic_mathml_js_1 = require("./semantic_mathml.js");
  9. const semantic_node_js_1 = require("./semantic_node.js");
  10. const SemanticPred = require("./semantic_pred.js");
  11. require("./semantic_heuristics.js");
  12. class SemanticTree {
  13. static empty() {
  14. const empty = DomUtil.parseInput('<math/>');
  15. const stree = new SemanticTree(empty);
  16. stree.mathml = empty;
  17. return stree;
  18. }
  19. static fromNode(semantic, opt_mathml) {
  20. const stree = SemanticTree.empty();
  21. stree.root = semantic;
  22. if (opt_mathml) {
  23. stree.mathml = opt_mathml;
  24. }
  25. return stree;
  26. }
  27. static fromRoot(semantic, opt_mathml) {
  28. let root = semantic;
  29. while (root.parent) {
  30. root = root.parent;
  31. }
  32. const stree = SemanticTree.fromNode(root);
  33. if (opt_mathml) {
  34. stree.mathml = opt_mathml;
  35. }
  36. return stree;
  37. }
  38. static fromXml(xml) {
  39. const stree = SemanticTree.empty();
  40. if (xml.childNodes[0]) {
  41. stree.root = semantic_node_js_1.SemanticNode.fromXml(xml.childNodes[0]);
  42. }
  43. return stree;
  44. }
  45. constructor(mathml) {
  46. this.mathml = mathml;
  47. this.parser = new semantic_mathml_js_1.SemanticMathml();
  48. this.root = this.parser.parse(mathml);
  49. this.collator = this.parser.getFactory().leafMap.collateMeaning();
  50. const newDefault = this.collator.newDefault();
  51. if (newDefault) {
  52. this.parser = new semantic_mathml_js_1.SemanticMathml();
  53. this.parser.getFactory().defaultMap = newDefault;
  54. this.root = this.parser.parse(mathml);
  55. }
  56. unitVisitor.visit(this.root, {});
  57. (0, semantic_annotations_js_1.annotate)(this.root);
  58. }
  59. xml(opt_brief) {
  60. const xml = DomUtil.parseInput('<stree></stree>');
  61. const xmlRoot = this.root.xml(xml.ownerDocument, opt_brief);
  62. xml.appendChild(xmlRoot);
  63. return xml;
  64. }
  65. toString(opt_brief) {
  66. return DomUtil.serializeXml(this.xml(opt_brief));
  67. }
  68. formatXml(opt_brief) {
  69. const xml = this.toString(opt_brief);
  70. return DomUtil.formatXml(xml);
  71. }
  72. displayTree() {
  73. this.root.displayTree();
  74. }
  75. replaceNode(oldNode, newNode) {
  76. const parent = oldNode.parent;
  77. if (!parent) {
  78. this.root = newNode;
  79. return;
  80. }
  81. parent.replaceChild(oldNode, newNode);
  82. }
  83. toJson() {
  84. const json = {};
  85. json['stree'] = this.root.toJson();
  86. return json;
  87. }
  88. }
  89. exports.SemanticTree = SemanticTree;
  90. const unitVisitor = new semantic_annotator_js_1.SemanticVisitor('general', 'unit', (node, _info) => {
  91. if (SemanticPred.isUnitProduct(node)) {
  92. node.role = semantic_meaning_js_1.SemanticRole.UNIT;
  93. }
  94. return false;
  95. });