u++の備忘録

Rを用いて般若心経を感情分析してみた

はじめに

本記事は、hiro.is(@youjo_DS)さんの以下のブログを読んで着想を得ました。

loveshome.hatenablog.jp

感情分析とは

感情分析とは、その名の通り単語や文章内の「感情」を分析する手法です。事前に単語ごとに[-1, 1]で感情に値を付けた辞書を用意しておきます。値が正に大きいほどポジティブ、負に大きいほどネガティブな感情を意味します。文章の場合は単純に足し合わせで「文章としての値」を算出しているようです。

 head(dic)
#        V1       V2     V3       V4
#1   優れる すぐれる   動詞 1.000000
#2     良い     よい 形容詞 0.999995
#3     喜ぶ よろこぶ   動詞 0.999979
#4   褒める   ほめる   動詞 0.999979
#5 めでたい めでたい 形容詞 0.999645
#6     賢い かしこい 形容詞 0.999486

上記の例は、下記の記事から引用しました。

qiita.com

Rのパッケージ

下記のスライドを読んで SentimentAnalysis パッケージで、英語ならば文単位の感情分析が可能だと知りました。

www.slideshare.net

パッケージの詳細は下記の記事が詳しかったです。

https://cran.r-project.org/web/packages/SentimentAnalysis/vignettes/SentimentAnalysis.html

般若心経を感情分析

いくつかの題材で感情分析を実施した結果、「般若心経」で興味深い結果が得られました。

般若心経 - Wikipedia

データの準備

SentimentAnalysis パッケージでは日本語を扱えないため、英語訳を利用します。「heart sutra japanese」でグーグル検索して一番上に来たサイトから英語訳を利用しました。文章が長い場合には、適宜2文以上に分割しています。

www.holymtn.com

documents <- c(
  "When Avalokitesvara Bodhisattva is practising the profound Prajna-paramita.", 
  "He sees and illuminates to the emptiness of the five skandhas", 
  "Thus attains deliverance from all suffering.", 
  "Sariputra, matter is not different from emptiness",
  "Emptiness is not different from matter.",
  "Matter is emptiness and emptiness is matter.",
  "So too are sensation, recognition, volition and consciousness.",
  "Sariputra, the emptiness character of all dharmas, neither arises nor ceases, is neither pure nor impure, and neither increases nor decreases.",
  "Therefore, in emptiness: there is no matter, no sensation, recognition, volition or consciousness, no eye, ear, nose, tongue, body, or mind, no sight, sound, scent, taste, tangibles, or dharma, no field of the eye up to no field of mental consciousness, no suffering, no cause of suffering, no ending of suffering, and no path, no wisdom and also no attainment.",
  "Because there is nothing obtainable.",
  "Bodhisattvas through the reliance on Prajna-paramita have no attachment and hindrance in their minds.",
  "Because there is no more attachment and hindrance.",
  "There is no more fear.",
  "Far away from erroneous views and wishful-thinking, Ultimately : The Final Nirvana.",
  "Therefore, realize that Prajna-paramita is the great wondrous mantra, the great radiant mantra, the unsurpassed mantra, and the unequalled mantra.",
  "It can eradicate all suffering, and It is genuine and not false.",
  "Therefore, utter the Prajna-paramita mantra.",
  "Chant: Gate Gate Paragate Parasamgate Bodhisvaha!"
)

分析結果

以下のように、感情分析を実行できます。

library("SentimentAnalysis")

# Analyze sentiment
sentiment <- analyzeSentiment(documents)

# Extract dictionary-based sentiment according to the QDAP dictionary
sentiment$SentimentQDAP
 [1]  0.20000000  0.00000000 -0.25000000 -0.25000000 -0.33333333 -0.50000000  0.25000000
 [8] -0.07692308 -0.03030303  0.50000000 -0.16666667 -0.50000000 -1.00000000 -0.12500000
[15]  0.38461538 -0.20000000 -0.25000000  0.00000000

ネガティブな文、ニュートラルな文、ポジティブな文がそれぞれ8、5、5個ありました。

table(convertToDirection(sentiment$SentimentGI))
negative  neutral positive 
       8        5        5 

文を時系列順に並び替えた結果をプロットしてみます。

plot(sentiment$SentimentQDAP, type="l")

f:id:upura:20181024062842p:plain

感情の起伏が起承転結に対応していることが読み取れます。お経と言うと一般に「単調」「つまらない」みたいな印象がありますが、感情分析を実行してみると振れが激しい文章だと分かりました。

おわりに

今回は、Rを用いて般若心経を感情分析してみた結果を共有しました。般若心経は感情の起伏が起承転結に対応しており、じっくり読んでみると意外と趣深い文章です。分量も少ないですし、本記事で少しでも興味を持った方は、ぜひご一読してみてください。検索すると現代語訳もたくさん出てきます。

Rスクリプトは下記で公開しています。

github.com

また般若心経はじめ仏教については、下記の本がおすすめです。

http://amzn.asia/d/1ccc1Xmamzn.asia