sentence-tokenization.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: Sentence Tokenization</title>
  4. <link rel="stylesheet" href="demo.css">
  5. <script src="../../node_modules/graphlibrary/dist/graphlib.js"></script>
  6. <script src="../../node_modules/d3/build/d3.js"></script>
  7. <script src="../dagre-d3.js"></script>
  8. <h1>Dagre D3 Demo: Sentence Tokenization</h1>
  9. <style id="css">
  10. /* This sets the color for "TK" nodes to a light blue green. */
  11. g.type-TK > rect {
  12. fill: #00ffd0;
  13. }
  14. text {
  15. font-weight: 300;
  16. font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
  17. font-size: 14px;
  18. }
  19. .node rect {
  20. stroke: #999;
  21. fill: #fff;
  22. stroke-width: 1.5px;
  23. }
  24. .edgePath path {
  25. stroke: #333;
  26. stroke-width: 1.5px;
  27. }
  28. </style>
  29. <svg id="svg-canvas" width=960 height=600></svg>
  30. <section>
  31. <p>An example of visualizing the tokenization of a sentence. This example shows
  32. how CSS classes can be applied to a rendered graph.
  33. </section>
  34. <script id="js">
  35. // Create the input graph
  36. var g = new graphlib.Graph()
  37. .setGraph({})
  38. .setDefaultEdgeLabel(function() { return {}; });
  39. // Here we"re setting nodeclass, which is used by our custom drawNodes function
  40. // below.
  41. g.setNode(0, { label: "TOP", class: "type-TOP" });
  42. g.setNode(1, { label: "S", class: "type-S" });
  43. g.setNode(2, { label: "NP", class: "type-NP" });
  44. g.setNode(3, { label: "DT", class: "type-DT" });
  45. g.setNode(4, { label: "This", class: "type-TK" });
  46. g.setNode(5, { label: "VP", class: "type-VP" });
  47. g.setNode(6, { label: "VBZ", class: "type-VBZ" });
  48. g.setNode(7, { label: "is", class: "type-TK" });
  49. g.setNode(8, { label: "NP", class: "type-NP" });
  50. g.setNode(9, { label: "DT", class: "type-DT" });
  51. g.setNode(10, { label: "an", class: "type-TK" });
  52. g.setNode(11, { label: "NN", class: "type-NN" });
  53. g.setNode(12, { label: "example", class: "type-TK" });
  54. g.setNode(13, { label: ".", class: "type-." });
  55. g.setNode(14, { label: "sentence", class: "type-TK" });
  56. g.nodes().forEach(function(v) {
  57. var node = g.node(v);
  58. // Round the corners of the nodes
  59. node.rx = node.ry = 5;
  60. });
  61. // Set up edges, no special attributes.
  62. g.setEdge(3, 4);
  63. g.setEdge(2, 3);
  64. g.setEdge(1, 2);
  65. g.setEdge(6, 7);
  66. g.setEdge(5, 6);
  67. g.setEdge(9, 10);
  68. g.setEdge(8, 9);
  69. g.setEdge(11,12);
  70. g.setEdge(8, 11);
  71. g.setEdge(5, 8);
  72. g.setEdge(1, 5);
  73. g.setEdge(13,14);
  74. g.setEdge(1, 13);
  75. g.setEdge(0, 1)
  76. // Create the renderer
  77. var render = new dagreD3.render();
  78. // Set up an SVG group so that we can translate the final graph.
  79. var svg = d3.select("svg"),
  80. svgGroup = svg.append("g");
  81. // Run the renderer. This is what draws the final graph.
  82. render(d3.select("svg g"), g);
  83. // Center the graph
  84. var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
  85. svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
  86. svg.attr("height", g.graph().height + 40);
  87. </script>
  88. <script src="demo.js"></script>