きっかけ
文章を解析するようになったけど、意味合い的に同じなのかチェックしたくなるだろうって思って、実装について調べた。
実装
build.gradle
group 'kuropaper' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // https://mvnrepository.com/artifact/de.sciss/ws4j compile group: 'de.sciss', name: 'ws4j', version: '0.1.0' }
resources
src/main/resoces
配下に、下記リンクのjawjaw.conf, similarity.confを置く。
たぶん、クラスパスが通っている箇所なら、どこでも大丈夫だと思う。
ws4j/config at master · Sciss/ws4j · GitHub
そんでもって、同じディレクトリにwnjpn-0.9.dbっていう辞書のDBファイルを置く。
中身は、Sqliteらしい。
http://compling.hss.ntu.edu.sg/wnja/data/1.1/wnjpn.db.gz
かなり大きなファイルなので、githubとかにあげるときは、要注意。
mainファイル
mainとなる実装。
とりあえず、下記をコピペ。
import edu.cmu.lti.jawjaw.JAWJAW; import edu.cmu.lti.jawjaw.pobj.*; import java.util.Set; public class Sample { public static void main(String[] args) { // "買収"(動詞)という単語から得られる関係の一部をデモします new Sample().run( "買収", POS.v ); } private 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 ); } }
思ったこと
似たような文言を取ってこれる。
うまく使えば、文章の近似性を求められそうな気がする。
不満点は、初期化処理に10秒くらい時間がかかること。。。
どうにかならんもんかね?
SqliteのDBファイルをメモリ展開しているみたいだが、毎回DBアクセスだと遅いのだろうか?
実装にかなり疑問がある。
あまり、偉そうなことは言えないが。。。
実運用にするようになったら、考えよう。。。
参考サイト
日本語WordnetのJavaフロントエンドを利用して,類義語の検索を行った. - へっぽこ修士の忘れ物