見出し画像

#D-TW09 テキスト分析を初めてみたころの話 (Let me touch a bit about analytics too)

(English follows)
前回の記事では、やはり繰り返しやることなのでデータ収集部分はツール化したい、そして古臭いインターフェイスのデータ収集アプリを作って運用してますなんていう話をしました。いやいや、自分だけが使うツールとなるとどうしても、得意な言語とかに偏ってしまって大体後で後悔するパターンです。今回は少しはテキスト分析というやったことの無かったジャンルをこのツイッターデータで初めてみたころ、最初に何をしたかなんていう話をしたいと思います。いつものことですがコードや分析の詳細は詳しい方がいろいろな記事を残していてくれるのでそちらをご参照いただければと思います!

  1. 単語のランキングを作りたい

  2. データを削る(再び)

  3. TF-IDF法

  4. 締めの手作業

単語のランキングを作りたい
さてさて、テキストのクリーニングが終わって、形態素解析が終わって、最初になんとなく思ったのは頻出単語のランキングを作りたい!でした。なのでシンプルに単語ごとに出現した回数を数えて、それが多い順に並べてみようと思いました。

その結果が上のようなランキングになります!えーっと。。。それはそうですよね。そうなんです、何も考えずにやろうとするとこうなってしまうのです。となるとランキングを作るには、単語の「選別」をしないと行けない、「選別」するとなるとそのための「基準」を作らないといけない、「基準」を作るとなるとなんでランキングが欲しいのかという「理由」に立ち返らないといけない。ということで、なんとなくランキングが見てみたいと思っただけなのに、なんで単語ランキングが欲しいのかとい理由を考える羽目になってしまいました。何年分析者をやってたんだろう。。。(恥)

せっかく時系列データなども見ようと思っていたので、当面ランキングの目的を「会社に行きたくない理由を見つける」ということに設定しました。そしてそれにいらないものは消そうと。

データを削る
削る方法はいろいろありました。シンプルなのは形態素解析をしたときに品詞の情報がでるので、句読点や助詞などいらないものは単語抽出時に削るということでした。それ以外にも理由が大体わかれば良いので、わかりにくいもの、数字とかなどは削除するとか、あとツイートにありがちな 「……………..」みたいなやつとか。でもそれだけではたりませんでした。

TF-IDF法
こんどはどうしても品詞では削れない頻度の高いものが上の方に来てしまうのです。「思う」、「明日」とか「夜」などなど。それを削除する方法として有名な分析にTF-IDF法というのがあるらしいといことを調べました。文章がたくさんある中で単語にスコアを付ける手法で、すごく簡単に言うと、一つの文章を特徴づける単語であり、他の文章ではあまり使われていない単語であればあるほど高いという感じでしょうか。TFさんが一つの文章におけるその単語の出現率で、IDFさんがその単語の入った文章がいくつあるかでTFさんとIDFさんの掛け算です。TFさんは実はツイートのように文章が短い場合にはあまり有効ではなさそうではあります。でもこれでどの文章にも必ずでてくるようなやつは削れました。

締めの手作業
だいたいが上記でいい感じになったのですが、どうしても削りきれなかったものは削除リストを作って、強制的に削除。あとは理由をわかりやすくするために「ない」などの否定語はもとの言葉にくっつけて表示するようにしてようやくランキングができました。。。単語ランキング恐るべし。一つの表を出すだけでこんなに時間かかるとは。。。

The last article was about data collection automation tool which looked a bit old fashioned… This is the bad habit but I rely too much on my comfortable programming when it is my own tool even if its too old…
This post is about my very first text data analysis, word ranking. Again I'm trying not to show actual codes. Also please refer to other articles if you need the detail about logics of the analysis.

  1. I want simple "word ranking"

  2. Data cleaning (again)

  3. TF-IDF method

  4. Finalization

I want simple "word ranking"
After text cleaning and tokenization, what I wanted to have at the first place was word frequency ranking. So I simply count frequency by word and showed it in descendent order.

Above chart is the result. It doesn't make sense to translate the chart above but basically most of them are like… punctuation for example. Well, Yes of course, this analysis is correct but… Ok it comes like this when you don't think much about what you want. Then to get what you want, you have to think about what you want… (shame). How long did I work as a data analyst…

I was going to do chronological analysis, so this time I set the purpose of the ranking as to show the reasons of "Don't want to work" (too late). Then I decided to delete words which it not necessary for the purpose.

Data cleaning (Again)
I found many ways to delete unnecessary words. At the beginning I deleted words by part information which I got when I tokenized tweets. For examples, punctuation, particles and so on. On top of those, I deleted things which is unique to tweets like "………………", also numbers which are not considered to be important to understand the reasons. However those are not enough.

TF-IDF method
What I found after doing above cleaning is that general words which are used often comes to top like "think", "tomorrow" or "Night" etc. One of the famous method I found is called "TF-IDF" method. It is the method to score each word when you have many documents. Roughly, the score is high when the words is often used in a particular documents and not used frequently in other documents. TF (term frequency) is  the frequency of the word in a documents. IDF (Inverse Document Frequency) is how many documents use the word (Less is strong). While TF is not really appropriate for short documents like tweets, this help me a lot to delete general words.

Finalization
In the end, I added minor processing to make it better. I made termination list for particular words which were not deleted by methods above. Also when there are "Negative" words like "Not", I tried to put it with the word which is suggested to make the "reason" clearer. Then I finally get the "simple" word ranking… I didn't expect that it took this long to just get a simple ranking. I should not have underestimated it (Sorry)…

reference (参考)
Wikipedia (2022), https://en.wikipedia.org/wiki/Tf%E2%80%93idf

この記事が気に入ったらサポートをしてみませんか?