きっかけ
文章の解析をしてみたくなったから
環境
- Windows10
- Typescript 2.5.3
$ tsc -version
Version 2.5.3
準備
node, typescriptのインストールは省略。
あと、プロジェクトの初期化も省略
kuromojiのインストール
まずは、kuromoji.jsを入れる。
Typescriptでやるので、型定義ファイルも一緒に入れる。
npm install -S kuromoji @types/kuromoji
実装
index.tsに下記の通りに実装。
import * as kuromoji from 'kuromoji';
class Demo {
constructor() {
const builder = kuromoji.builder({
dicPath: 'node_modules/kuromoji/dict'
})
builder.build((err, tokenizer) => {
if (err) return;
var tokens = tokenizer.tokenize("有給を取るとなぜか雨が降る");
console.dir(tokens);
})
}
}
new Demo();
実装説明
解析器の作成
const builder = kuromoji.builder({
dicPath: 'node_modules/kuromoji/dict'
})
上記で解析器を造っているようです。
dicPathが辞書のあるパスだそうです。
特に強いこだわりがなければ、kuromojiと一緒に入ってくるものを指定すれば問題ないでしょう。
解析
builder.build((err, tokenizer) => {
if (err) return;
var tokens = tokenizer.tokenize("有給を取るとなぜか雨が降る");
console.dir(tokens);
})
最初に、err
を見ているのは、解析器の生成に失敗してないかチェックするため。
解析器の作成のタイミングでエラー出せや!って思わなくもないですが、事情もあるのでしょう。
tokenizer.tokenize
で文章を指定すると、構文解析された結果情報が得られる。
あと、「有給を取るとなぜか雨が降る」に、特に深い意味はありません。
今日、たまたま有給をとったら、そうなってしまったという事実です。
でも、前回の有給を取った日も雨だったような。。。
家から出るなと言う暗示ですかね?
実行結果
tsc index.ts && node index.js
でコンパイル&実行。
そうすると、以下の結果が返ってくる。
[ { word_id: 31860,
word_type: 'KNOWN',
word_position: 1,
surface_form: '有給',
pos: '名詞',
pos_detail_1: '一般',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: '有給',
reading: 'ユウキュウ',
pronunciation: 'ユーキュー' },
{ word_id: 92880,
word_type: 'KNOWN',
word_position: 3,
surface_form: 'を',
pos: '助詞',
pos_detail_1: '格助詞',
pos_detail_2: '一般',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: 'を',
reading: 'ヲ',
pronunciation: 'ヲ' },
{ word_id: 3094480,
word_type: 'KNOWN',
word_position: 4,
surface_form: '取る',
pos: '動詞',
pos_detail_1: '自立',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '五段・ラ行',
conjugated_form: '基本形',
basic_form: '取る',
reading: 'トル',
pronunciation: 'トル' },
{ word_id: 92550,
word_type: 'KNOWN',
word_position: 6,
surface_form: 'と',
pos: '助詞',
pos_detail_1: '接続助詞',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: 'と',
reading: 'ト',
pronunciation: 'ト' },
{ word_id: 11480,
word_type: 'KNOWN',
word_position: 7,
surface_form: 'なぜ',
pos: '副詞',
pos_detail_1: '助詞類接続',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: 'なぜ',
reading: 'ナゼ',
pronunciation: 'ナゼ' },
{ word_id: 92700,
word_type: 'KNOWN',
word_position: 9,
surface_form: 'か',
pos: '助詞',
pos_detail_1: '副助詞/並立助詞/終助詞',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: 'か',
reading: 'カ',
pronunciation: 'カ' },
{ word_id: 465170,
word_type: 'KNOWN',
word_position: 10,
surface_form: '雨',
pos: '名詞',
pos_detail_1: '一般',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: '雨',
reading: 'アメ',
pronunciation: 'アメ' },
{ word_id: 92920,
word_type: 'KNOWN',
word_position: 11,
surface_form: 'が',
pos: '助詞',
pos_detail_1: '格助詞',
pos_detail_2: '一般',
pos_detail_3: '*',
conjugated_type: '*',
conjugated_form: '*',
basic_form: 'が',
reading: 'ガ',
pronunciation: 'ガ' },
{ word_id: 2637510,
word_type: 'KNOWN',
word_position: 12,
surface_form: '降る',
pos: '動詞',
pos_detail_1: '自立',
pos_detail_2: '*',
pos_detail_3: '*',
conjugated_type: '五段・ラ行',
conjugated_form: '基本形',
basic_form: '降る',
reading: 'フル',
pronunciation: 'フル' } ]
かなり詳しい形で解析されているような気がしないでもない。
問題は、これをどう使うかでしょうね。
今後の予定
おそらく、これの解析結果は、コンパイラで言うところの字句解析でしょう。
これを文章解析まで持っていきたいと思っている。
なので、次は、解析した結果をもとに、構文木の構築を考えてみる。
最終的にしたいのは、文章の類似比較。
それができれば、本当にやりたいことができる。
シコシコ、構文木作成の情報収集をしますか。。。
周辺知識
形態素 - Wikipedia
参考サイト
kuromoji.js を使って形態素解析 - zeny.io
次の目標