semantic_tree.js 2.8 KB

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