日本語WordnetのJavaフロントエンドを利用して,類義語の検索を行った.

参考にしたページはこちら.
>>http://w-it.jp/shima/2009/03/wordnet_java_api.html
このJAWJAWというJavaAPIを利用する.

準備

JAWJAWを利用するにあたって,必要なライブラリが3つある.

  1. sqlite-jdbc-3.6.11.jar
  2. nestedvm-1.0.jar
    • Makefileからjarを生成する必要がある.
    • 面倒なのでこちらを利用.→NestedVMテスト
      • もしかしてjarでの配布が許可されていないのかも.
  3. junit-4.0.jar

上記のページではMaven2を利用したjarの生成を勧めているが,Maven2の利用方法がわからなかった(調べなかった)し,3つならネットから拾ってくるのも大した労力じゃないと思ったので,Maven2は利用していない.

実装

日本語Wordnetのページで紹介されていたサンプルを利用した.
以下にサンプルの引用を示す.

public class SimpleDemo {
	private static void run( String word, POS pos ) {
		// ファサードから日本語 WordNet にアクセス
		Set<String> hypernyms = JAWJAW.findHypernyms(word, pos);
		Set<String> hyponyms = JAWJAW.findHyponyms(word, pos);
		Set<String> consequents = JAWJAW.findEntailments(word, pos);
		Set<String> translations = JAWJAW.findTranslations(word, pos);
		Set<String> definitions = JAWJAW.findDefinitions(word, pos);
		// 結果表示(多義語はごっちゃになっています)
		System.out.println( "hypernyms of "+word+" : \t"+ hypernyms );
		System.out.println( "hyponyms of "+word+" : \t"+ hyponyms );
		System.out.println( word+" entails : \t\t"+ consequents );
		System.out.println( "translations of "+word+" : \t"+ translations );
		System.out.println( "definitions of "+word+" : \t"+ definitions );		
	}
	public static void main(String[] args) {
		// "買収"(動詞)という単語から得られる関係の一部をデモします
		SimpleDemo.run( "買収", POS.v );
	}
}

もちろんこれだけでは動かない.
今回は上記のコードを利用して以下のコードを記述した.

package similarword_sample;

import edu.cmu.lti.jawjaw.JAWJAW;
import edu.cmu.lti.jawjaw.pobj.*;
import java.util.Set;

public class SimpleDemo_ {

    private static void run( String word, POS pos ) {
        // ファサードから日本語 WordNet にアクセス
        Set<String> hypernyms = JAWJAW.findHypernyms(word, pos);
        Set<String> hyponyms = JAWJAW.findHyponyms(word, pos);
        Set<String> consequents = JAWJAW.findEntailments(word, pos);
        Set<String> translations = JAWJAW.findTranslations(word, pos);
        Set<String> definitions = JAWJAW.findDefinitions(word, pos);
        // 結果表示
        System.out.println( "hypernyms of "+word+" : \t"+ hypernyms );
        System.out.println( "hyponyms of "+word+" : \t"+ hyponyms );
        System.out.println( word+" entails : \t\t"+ consequents );
        System.out.println( "translations of "+word+" : \t"+ translations );
        System.out.println( "definitions of "+word+" : \t"+ definitions );
    }
    public static void main(String[] args) {
        // TODO code application logic here
        // "買収"(動詞)という単語から得られる関係の一部をデモします
	SimpleDemo_.run( "買収" , POS.v );
    }
}

JAWJAWのsrcディレクトリ内のjavaのパッケージを切り,インポートする.
Netbeansを利用したため以下のようなディレクトリ構成になった.

  • similarword_sample
    • ソースパッケージ
      • edu.cmu.lti.jawjaw
      • edu.cmu.lti.jawjaw.db
      • edu.cmu.lti.jawjaw.deno.demo
      • edu.cmu.lti.jawjaw.pobj
      • similarword_sample
    • data
      • デフォルトパッケージ
        • wnjpn-0.9.db
    • ライブラリ

上記のディレクトリ構成で注意するのが,太字表記したwnjpn-0.9.dbの部分である.
配布されているJAWJAWのコードの相対パスを一部確認したところ

sqlite:./data/wnjpn-0.9.db

となっていたため,dbの名前を変更した.今回利用したdbのバージョンは0.9.1である.
本来はコードを修正するのが望ましいが,すべてのコードを確認するのが面倒時間がなかったので,dbの名前を変えた.

これで利用できた.

検索方法

main関数の

SimpleDemo_.run( "買収" , POS.v );

の中身を変えるだけ.
上記であれば,買収という動詞(v)の類義語などを検索する.
POSの設定はedu.cmu.lti.jawjaw.pobjのPOS.javaで確認できる.

public enum POS {
	a, // adjective
	r, // adverb
	n, // noun
	v; // verb
}

上から

  • 形容詞
  • 副詞
  • 名詞
  • 動詞

である.


以上.