Scalaでテキスト処理

参照:

日本語シソーラスの入手など Python による日本語自然言語処理

日本語 WordNet http://compling.hss.ntu.edu.sg/wnja/ ※読み、には未対応

日本語形態素解析システム JUMAN http://nlp.ist.i.kyoto-u.ac.jp/index.php?JUMAN

JUMAN++ http://nlp.ist.i.kyoto-u.ac.jp/index.php?JUMAN%2B%2B

NAIST Japanese Dictionary https://ja.osdn.net/projects/naist-jdic/wiki/FrontPage

NAIST Japanese Dictionary https://ja.osdn.net/projects/naist-jdic/releases/53500 ダウンロード元

IPADIC(IPA辞書)とはなにものか? http://parame.mwj.jp/blog/0209

Scala でファイル読み込み https://www.qoosky.io/techs/f7851bb2e4

ファイルの読み込みと表示例)

※ 文字コードの指定は、 UTF-8 EUC-JP Shift_JIS など。

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val lines = source.getLines().take(100)

val lines2 = source.getLines().drop(200).take(100)

lines.foreach(println)

lines2.foreach(println)

}

naist-jdic.csv には 11カラム目に「読み」が入っているので抽出する例)

※ 辞書ファイルは src フォルダの中ではなく、プロジェクトのフォルダ(アプリケーションが実行されるフォルダ?)に入れる(eclipse経由で)。

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val words = source.getLines().map(_.split(",")(11))

words.take(300).foreach(println)

}

単語を構成する文字を順に並べ替え、見出しとする例)

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val words = source.getLines().map(_.split(",")(11))

words.take(300).map(e => (e.sorted,e)).foreach(println)

}

見出しが共通する単語をまとめて辞書を作成する例1)

※ 可変Mapを辞書として準備。語群から要素を追加

辞書:可変Map (キー 見出し文字列, 値 文字列のSet(単語1, 単語2, ...) )

import scala.io.Source

import scala.collection.mutable.Map

import scala.collection.mutable.Set

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val words = source.getLines().map(_.split(",")(11))

val index_words = words.map(e => (e.sorted,e))

val dict = Map.empty[String,Set[String]].withDefault(e => Set.empty[String])

index_words.foreach({case (k,v) => dict(k) = dict(k)+v})

dict.foreach({case (k,v) => println(k + ":" + v)})

}

見出しが共通する単語をまとめて辞書を作成する例2)

※ 見出し付き語群 index_words から 重複 を除去(Set化)し、見出しでまとめたMapに変換し、Mapの値を取り出し、まとめられた見出しつき語群から値を取り出す例)

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val words = source.getLines().map(_.split(",")(11))

val index_words = words.map(e => (e.sorted,e))

index_words.toSet.groupBy[String](_._1).values.map(_.map(_._2)).foreach(println)

}

アナグラムの辞書から、最大のアナグラムの個数、7個以上のアナグラムを持つ単語を抽出

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

val words = source.getLines().map(_.split(",")(11))

val index_words = words.map(e => (e.sorted,e))

val dict = index_words.toSet.groupBy[String](_._1).values.map(_.map(_._2))

println(dict.map(_.size).max)

dict.filter(_.size > 6).foreach(println)

}

結果:

7

Set(シジュン, シュンジ, シュジン, ジュシン, シンジュ, ジュンシ, ジンシュ)

Set(ジュウショ, シュジョウ, ジュショウ, ジョウシュ, ショウジュ, ジョシュウ, シュウジョ)

Set(タカマサ, タマサカ, カタサマ, サカマタ, マサカタ, マサタカ, マタサカ)

Set(タキカワ, カワタキ, カタワキ, キタカワ, ワキカタ, カワキタ, キカワタ)

並列化の実験:

import scala.io.Source

object Test2 extends App {

val source = Source.fromFile("naist-jdic.csv", "EUC-JP")

var wc = 0

val words = source.getLines().map({e => wc+=1; e.split(",")(11)})

//println(wc)

val start = System.currentTimeMillis

//val index_words = words.map(e => (e.sorted,e)).toSeq.par

val index_words = words.map(e => (e.sorted,e))

//println(wc)

val dict = index_words.toSet.groupBy[String](_._1).values.map(_.map(_._2))

println((System.currentTimeMillis - start) + "msec 辞書生成時間")

println(wc)

dict.filter(_.size > 6).foreach(println)

println(dict.size)

println(dict.map(_.size).max)

val start2 = System.currentTimeMillis

Range(0,1000).foreach(e => dict.map(_.size).max)

println((System.currentTimeMillis - start2) + "msec 辞書利用時間")

}

並列化無し

1481msec 辞書生成時間

3870msec 辞書利用時間

並列化有り

1889msec 辞書生成時間

818msec 辞書利用時間