質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

4173閲覧

kuromoji.jsで形態素解析を行いたい

iwanharts

総合スコア32

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2016/05/27 18:00

###前提・実現したいこと
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配下にある。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

そのサンプルコードのコメント外の部分では

  1. 入力制御の定義
  2. 辞書の読み込み

を行っています。単純にkuromojiで形態素解析したい場合は、後者が終わったあと、tokenizer.tokenizeを実行してください。例:

js

1kuromoji.builder({dicPath:'bower_components/kuromoji/dist/dict/'}).build(function(error,tokenizer){ 2 if(error){ 3 console.error(error.message) 4 } 5 else{ 6 console.log(tokenizer.tokenize('すもももももももものうち')) 7 } 8})

また、前者はVueに委任しているので、ここでは解説を割愛します。

DEMO

投稿2016/05/28 19:07

編集2016/05/28 19:14
horse_n_deer

総合スコア452

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問