0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rでwordcloud2動かす(Twitterのツイート分析)

Last updated at Posted at 2020-12-04

はじめに

 この記事は、Rを用いてRMeCabで形態素解析を行い、wordcloud2で出力することを目的としています。

 石田基広[2020]『Rによるテキストマイニング:センチメント分析・単語分散表現・機械学習・Pythonラッパー』森北出版を参考にこの記事を作成しました。

 ツイート等のデータをResearchAPIを使用して、取得し終わったという前提で書いてあります。
また、user,idなどの変数名が取得時の設定で違う可能性があるので注意してください。

Rの備忘録(Twitter分析に関すること)
こちらの記事に、データをいじる際に私が役に立ったと思うtipsを載せておきました。

目次

1.はじめに
2.ライブラリーのインストール
3.パッケージの有効化
4.重複した文章やホームページのアドレスの削除
5.形態素分析解析
6.wordcloud2で出力
7.おわりに

ライブラリーのインストール

#パッケージのインストール(初回だけ行うこと)。
#RやRStudioのバージョンが古いとインストールできない場合があります。
#RMeCabのインストールはご自身で調べてください。
install.packages("magrittr")
install.packages("dplyr")
install.packages("stringr")
install.packages("wordcloud2")
install.packages("purrr")
install.packages("openxlsx")

パッケージの有効化

library(RMeCab)
library(magrittr)
library(dplyr)
library(stringr)
library(wordcloud2)
library(purrr)
library(openxlsx)

重複した文章やホームページのアドレスの削除

old.op <- options(max.print=999999)

#csvファイルを開く場合
file <- read.csv("ファイル名.csv")


#xlsxファイルを開く場合
file <- read.xlsx("ファイル名.xlsx")


#重複した文章の削除
file <- file %>% distinct(text,.keep_all=TRUE) %>%
    select(user, text)


#ホームページのアドレスなどを削除
file <- file %>% map_dfr(., {
    ~ filter(file, user == .) %>%
        mutate(text = str_remove_all(text, "https?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+"), 
               text = str_remove_all(text, "hxxps?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+"),
               text=str_remove_all(text,"\n"), 
               #返信時に表示される@userを削除
               text=str_remove_all(text,"@[a-zA-Z0-9]*"),
              # #の削除(#に日本語が続く場合は#だけ消えて日本語は残る)
         text=str_remove_all(text,"#[a-zA-Z0-9]*"),
               #XXXXのところに消したいものを突っ込めば消えるはずです。
               text = str_remove_all(text, "XXXX?[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+"),
               text = str_remove_all(text, "\\p{So}|\\p{Cn}") )
}) 


#特定の単語及び、文を消す(上記でもこの方法でもできる)
#スパム等に含まれる定型文を消す時に便利
#うまくスパムとかが消せない時は、csvファイルを開き直接消すのも一つの手
file <- filter(file, !text %in% c("消したい単語", "消したい文"))


#一応保存。
file %>% select(text) %>% pull() %>% write("ファイル名3.csv")

形態素分析解析

txt_df <- docDF("ファイル名3.csv", type = 1)
txt_df %>% select(POS1) %>% distinct() %>% pull()
txt_df %>% select(POS2) %>% distinct() %>% pull()
txt_df <- txt_df %>% filter(POS1 %in% c("名詞","形容詞","動詞"), 
                            POS2 %in% c("一般","自立" ,"非自立",
                                        "助詞類接続"))
txt_df %>% NROW()

wordcloud2で出力

#tail(n)n個の単語を表示する
tzt_words100 <- txt_df %>% arrange("ファイル名3.csv") %>% tail(100) %>% select(TERM, FREQ = "ファイル名3.csv") 

tzt_words100 %>% wordcloud2()


#特定の単語を消す
tzt_words100 %>% filter(!TERM %in% c("消したい単語", "消したい単語2")) %>% wordcloud2()


#特定の単語を消して、ファイルに保存する場合
#試行回数増やすごとにファイル名の後の数字増やす
tzt_words100 %>% filter(!TERM %in% c("消したい単語", "消したい単語2")) %>% write("ファイル名4.csv")


#wordcloud2の画像の保存のやり方
tzt_words100 <- tzt_words100 %>% wordcloud2()
saveWidget(tzt_df, "ファイル名.html", selfcontained = F)

おわりに

 wordcloud2で図に表示させる文字数であったり、図の形をいろいろと変更できるので調べてみると面白いかもしれません。

メモ(スパムツイートを処理するとき)

スパムツイートはほとんど同じ文書で書かれていますが、一部異なる箇所があります。
なので、先に異なっている部分を消してから重複文を消す作業をした方が効率よく消えます。

問題点

一度、file %>% select(text) %>% pull() %>% write("ファイル名3.csv")
でファイルを保存すると元々あったtextという列名が消えてしまうため、あとで「ファイル名3.csv」を読み込み、file <- filter(file, !text %in% c("消したい単語", "消したい文"))
を動かそうとしても動かない。

Ver1.01

file <- file %>% distinct(text,.keep_all=TRUE) %>%
    select(user, text) の追加。

Ver1.02

file %>% count(user) %>% arrange(n) %>% tail(999999)の追加

Ver1.03

file <- userIDS %>% map_dfr()内に
text=str_remove_all(text,"\n"),
text=str_remove_all(text,"@[a-zA-Z0-9]*"),
text=str_remove_all(text,"#[a-zA-Z0-9]*"),

を追加。改行、@user,#を削除(しかし#の後ろに日本語が続く場合は#だけ消えて日本語は残る)。

file <- filter(file, !text %in% c("消したい単語", "消したい文")

を追加。

userIDS <- file %>% count(user) %>% filter(n >= 1) %>% select(user) %>% pull() 

の削除。

Ver1.04
混乱を招きそうな記述を複数削除

Ver1.05
wordcloud2の画像の保存のやり方の追加。

tzt_words100 <- tzt_words100 %>% wordcloud2()
saveWidget(tzt_df, "ファイル名.html", selfcontained = F)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?