###前提・実現したいこと
Node.jsを使いServerを構築中。
kuromoji.jsを用いて形態素解析をしたい。
形態素解析は静的なものしか扱わない。
###発生している問題・エラーメッセージ
試しに、これに従い、kuromoji.jsのデモを実行可能にした。
だが、以下のjsの意味がわからないので解説してほしい。
コメントアウトされている部分は大丈夫です。
###該当のソースコード
javascript
1/* 2 * Copyright Copyright 2014 Takuya Asano 3 * Copyright 2010-2014 Atilika Inc. and contributors 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18// require vue as MVVM framework 19// require kuromoji 20 21var DIC_URL = "bower_components/kuromoji/dist/dict/"; 22 23var tokenizer = null; 24// var lattice; // Very large object. Unwatch this object from Model. 25// var renderer = new dagreD3.Renderer(); 26 27 28var vm = new Vue({ 29 el: "#demo", 30 data: { 31 inputText: "", 32 tokens: [], 33 isLoading: true, 34 message: "Loading dictionaries ...", 35 svgStyle: "hidden" 36 }, 37 methods: { 38 /* 39 drawGraph: function () { 40 if (lattice != null) { 41 drawLattice(); 42 vm.svgStyle = "visible"; 43 } 44 }, 45 */ 46 tokenize: function () { 47 if (vm.inputText == "" || tokenizer == null) { 48 vm.tokens = []; 49 // lattice = null; 50 return; 51 } 52 try { 53 // lattice = tokenizer.getLattice(vm.inputText); 54 vm.tokens = tokenizer.tokenize(vm.inputText); 55 } catch (e) { 56 console.log(e); 57 // lattice = null; 58 vm.tokens = []; 59 } 60 } 61 } 62}); 63 64 65// フォームの内容が変化したらtokenizeする 66//vm.$watch("inputText", function (value) { 67// // vm.graphEnabled = false; 68// vm.svgStyle = "hidden"; 69// vm.tokenize(); 70//}); 71 72 73// Load and prepare tokenizer 74kuromoji.builder({ dicPath: DIC_URL }).build(function (error, _tokenizer) { 75 if (error != null) { 76 console.log(error); 77 } 78 tokenizer = _tokenizer; 79 80 vm.message = "Ready"; 81 82 vm.inputText = "すもももももももものうち"; 83 vm.isLoading = false; 84}); 85 86 87/* 88function drawLattice () { 89 // Create a new directed graph 90 var g = new dagreD3.Digraph(); 91 92 // BOS 93 var bos_node = lattice.nodes_end_at[0][0]; 94 g.addNode("0:BOS", { label: "BOS " + bos_node.cost }); 95 96 var i, j, k, nodes, node; 97 98 // Draw node 99 for (i = 1; i <= lattice.eos_pos; i++) { 100 nodes = lattice.nodes_end_at[i]; 101 if (nodes == null) { 102 continue; 103 } 104 for (j = 0; j < nodes.length; j++) { 105 node = nodes[j]; 106 107 // Add nodes to the graph. The first argument is the node id. The second is 108 // metadata about the node. In this case we're going to add labels to each of 109 // our nodes. 110 if (node.name == "EOS") { 111 g.addNode(i + ":" + node.name, { label: node.name + " " + node.cost }); 112 } else { 113 var features = tokenizer.token_info_dictionary.getFeatures(node.name); 114 g.addNode(i + ":" + node.name, { 115 label: "<div>" // + node.left_id + " " + node.name + " " + node.right_id + "<br />" 116 + features[0] + "<br />" + features[1] + "<br />" + features[2] + "<br />" + node.cost + "</div>" 117 }); 118 } 119 } 120 } 121 122 // Draw edge 123 for (i = 1; i <= lattice.eos_pos; i++) { 124 nodes = lattice.nodes_end_at[i]; 125 if (nodes == null) { 126 continue; 127 } 128 for (j = 0; j < nodes.length; j++) { 129 node = nodes[j]; 130 // var cost = Number.MAX_VALUE; 131 // var shortest_prev_node; 132 133 var prev_nodes = lattice.nodes_end_at[node.start_pos - 1]; 134 if (prev_nodes == null) { 135 // TODO process unknown words 136 continue; 137 } 138 for (k = 0; k < prev_nodes.length; k++) { 139 var prev_node = prev_nodes[k]; 140 141 var edge_cost; 142 if (node.left_id == null || prev_node.right_id == null) { 143 console.log("Left or right is null"); 144 edge_cost = 0; 145 } else { 146 edge_cost = tokenizer.viterbi_searcher.connection_costs.get(prev_node.right_id, node.left_id); 147 } 148 149 // Add edges to the graph. The first argument is the edge id. Here we use null 150 // to indicate that an arbitrary edge id can be assigned automatically. The 151 // second argument is the source of the edge. The third argument is the target 152 // of the edge. The last argument is the edge metadata. 153 g.addEdge( 154 (node.start_pos - 1) + ":" + prev_node.name + "-" + i + ":" + node.name, 155 (node.start_pos - 1) + ":" + prev_node.name, 156 i + ":" + node.name, 157 { label: String(edge_cost) }); 158 159 // TODO If best path, strong this edge 160 // edge_metadata.style = "stroke: #f66; stroke-width: 3px;"; 161 } 162 } 163 } 164 165 var layout = dagreD3.layout() 166 .nodeSep(20) 167 .edgeSep(20) 168 .rankDir("LR"); 169 renderer.layout(layout).run(g, d3.select("svg g")); 170} 171*/
###補足情報(言語/FW/ツール等のバージョンなど)
npm install kuromoji
でインストール。
また、上のファイルは/demo/kuromoji.js/demo/js配下にある。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。