Rで英文テキストマイニング

ここ数年,特に英文のテキストマイニング環境の整備が著しい.
Rでも,tidytextパッケージなんて白眉なものも出てきているし.
tidytextは,ここで素人がいろいろ言うよりも,こちらがおすすめ.
qiita.com

ここでは,ネットサーフィンしていて知った恐ろしく簡単なOpiniton miningの野良パッケージでセンチメント分析をする.
ちなみに,ここで挙げる例は下のリンクの丸パクりなので新味はない.あくまで個人用メモ.
github.com

実行するとわかるが著しくもっさり.気合いの入った文章で実行するには忍耐か別の方法をおすすめする.
まず,githubからインストールするのでdevtoolsが入っていない方はインストールする.
依存関係のあるNLPパッケージも同様.

# install.packages(c("NLP","devtools"))
devtools::install_github("sfeuerriegel/SentimentAnalysis")

インストールしたらパッケージを呼び出して,軽くテスト.
analyzeSentiment関数の入力は,ここでは文字列ベクトルにしてるが,document-term matrixでもコーパスでもいいらしい.

> require(SentimentAnalysis)
> require(NLP)
> sentiment <- analyzeSentiment("Yeah, this was a great soccer game of the German team!")

さて,この文章はどんなセンチメントか?

> convertToBinaryResponse(sentiment)$SentimentGI
[1] positive
Levels: negative positive

Sugeeeee!!
ちなみに,心理社会学で広く用いられている辞書;Harvard-IVをつかった辞書ベースのオピニオンマイニングをしている.

複数でもOK

> documents <- c("Wow, I really like the new light sabers!",
+                "That book was excellent.",
+                "R is a fantastic language.",
+                "The service in this restaurant was miserable.",
+                "This is neither positive or negative.",
+                "The waiter forget about my a dessert -- what a poor service!")
> sentiment <- analyzeSentiment(documents)
> sentiment$SentimentGI
[1]  0.3333333  0.5000000  0.5000000 -0.6666667  0.0000000 -0.6000000
> convertToDirection(sentiment$SentimentGI)
[1] positive positive positive negative neutral  negative
Levels: negative neutral positive

こんな感じで各文書のセンチメントスコアやオピニオンを出してくれる.