系統參數設定

Sys.setlocale(category = "LC_ALL", locale = "zh_TW.UTF-8") # 避免中文亂碼
## [1] ""

安裝需要的packages

# echo = T,results = 'hide'
packages = c("dplyr", "tidytext", "stringr", "wordcloud2", "ggplot2",'readr','data.table','reshape2','wordcloud','tidyr','scales')
existing = as.character(installed.packages()[,1])
for(pkg in packages[!(packages %in% existing)]) install.packages(pkg)

讀進library

library(dplyr)
library(stringr)
library(tidytext)
library(wordcloud2)
library(data.table)
library(ggplot2)
library(reshape2)
library(wordcloud)
library(tidyr)
library(readr)
library(scales)
require(jiebaR)

資料基本介紹

  • 資料來源: 文字平台收集PTT Gossip版2020-01-01 ~ 2021-03-23 所有文章
  • 資料集: salmon_articleMetaData.csv
  • 關鍵字:雞排妹 鄭家純
  • 去除關鍵字:深夜保健室
  • 資料時間:2020-01-01 ~ 2021-03-23

這次我們主要以雞排妹性騷擾事件,主要分析ptt上網友的相關討論,並對比dcard上大家的討論情形。本次主要針對以下方向分析:

1.性騷擾事件的討論大概出現在哪個時間點,話題高峰在哪裡?
2.正面和負面的討論內容各是甚麼,有沒有時間點上的差異?
3.正面和負面討論的情緒分數大約多少?

# 把文章讀進來
MetaData = fread('kkkkkk.csv',encoding = 'UTF-8')
Reviews  = fread('kkkkkk_articleReviews.csv',encoding = 'UTF-8')

# 再篩一次文章
keywords = c('性騷擾','翁立友')
toMatch = paste(keywords,collapse="|")
MetaData = with(MetaData,
MetaData[grepl(toMatch,artTitle)|grepl(toMatch,artContent),])

# 挑選文章對應的留言
Reviews = left_join(MetaData, Reviews[,c("artUrl", "cmtContent")], by = "artUrl")

1. 資料前處理

(1). 文章斷詞

設定斷詞引擎

# 加入自定義的字典
jieba_tokenizer <- worker(user="user_dict.txt", stop_word = "stop_words.txt")

# 設定斷詞function
customized_tokenizer <- function(t) {
  lapply(t, function(x) {
    tokens <- segment(x, jieba_tokenizer)
    return(tokens)
  })
}
# 把文章和留言的斷詞結果併在一起
MToken <- MetaData %>% unnest_tokens(word, artContent, token=customized_tokenizer)
RToken <- Reviews %>% unnest_tokens(word, cmtContent, token=customized_tokenizer)

# 把資料併在一起
data <- rbind(MToken[,c("artDate","artUrl", "word")],RToken[,c("artDate","artUrl", "word")]) 

(2). 資料基本清理

  • 日期格式化
  • 去除特殊字元、詞頻太低的字
# 格式化日期欄位
data$artDate= data$artDate %>% as.Date("%Y/%m/%d")

# 過濾特殊字元
data_select = data %>% 
  filter(!grepl('[[:punct:]]',word)) %>% # 去標點符號
  filter(!grepl("['^0-9a-z']",word)) %>% # 去英文、數字
  filter(nchar(.$word)>1) 
  
# 算每天不同字的詞頻
# word_count:artDate,word,count
word_count <- data_select %>%
  select(artDate,word) %>%
  group_by(artDate,word) %>%
  summarise(count=n()) %>%  # 算字詞單篇總數用summarise
  filter(count>3) %>%  # 過濾出現太少次的字
  arrange(desc(count))
## `summarise()` has grouped output by 'artDate'. You can override using the `.groups` argument.
word_count
## # A tibble: 10,360 x 3
## # Groups:   artDate [20]
##    artDate    word   count
##    <date>     <chr>  <int>
##  1 2021-02-05 雞排妹   980
##  2 2021-02-05 性騷擾   861
##  3 2021-02-03 雞排妹   744
##  4 2021-02-03 性騷擾   696
##  5 2021-02-05 道歉     669
##  6 2021-02-05 真的     628
##  7 2021-02-04 雞排妹   594
##  8 2021-02-04 性騷擾   581
##  9 2021-02-06 雞排妹   571
## 10 2021-02-03 飛機杯   557
## # ... with 10,350 more rows

2. 準備LIWC字典

全名Linguistic Inquiry and Word Counts,由心理學家Pennebaker於2001出版 分為正向情緒與負向情緒

讀檔,字詞間以“,”將字分隔

P <- read_file("positive.txt") # 正向字典txt檔
N <- read_file("negative.txt") # 負向字典txt檔

#字典txt檔讀進來是一整個字串
typeof(P)
## [1] "character"

分割字詞,並將兩個情緒字典併在一起

# 將字串依,分割
# strsplit回傳list , 我們取出list中的第一個元素
P = strsplit(P, ",")[[1]]
N = strsplit(N, ",")[[1]]

# 建立dataframe 有兩個欄位word,sentiments,word欄位內容是字典向量
P = data.frame(word = P, sentiment = "positive") #664
N = data.frame(word = N, sentiment = "negative") #1047

# 把兩個字典拼在一起
LIWC = rbind(P, N)

# 檢視字典
head(LIWC)
##       word sentiment
## 1     一流  positive
## 2 下定決心  positive
## 3 不拘小節  positive
## 4   不費力  positive
## 5     不錯  positive
## 6     主動  positive

3. 將文章和與LIWC情緒字典做join

在畫出情緒之前,先看看每天的發文情形,大約在2/1之後才有較多的討論。

正負情緒發文折線圖

MetaData$artDate= MetaData$artDate %>% as.Date("%Y/%m/%d")
MetaData %>%
  group_by(artDate) %>%
  summarise(count = n()) %>%
  ggplot()+
    geom_line(aes(x=artDate,y=count))+
    scale_x_date(labels = date_format("%m/%d"))

找出文集中,對於LIWC字典是positive和negative的字

算出每天情緒總和(sentiment_count)

# sentiment_count:artDate,sentiment,count
sentiment_count = data_select %>%
  select(artDate,word) %>%
  inner_join(LIWC) %>% 
  group_by(artDate,sentiment) %>%
  summarise(count=n())  
## Joining, by = "word"
## `summarise()` has grouped output by 'artDate'. You can override using the `.groups` argument.

畫出每天的情緒總分數,可以看到在討論的高峰期時(2/1~2/7),情緒都是以負面為主。約在 8 號之後討論度逐漸下降。

正負情緒分數折線圖

# 檢視資料的日期區間
range(sentiment_count$artDate) #"2021-01-30" "2021-03-26"
## [1] "2021-01-30" "2021-03-26"
sentiment_count %>%
  ggplot()+
  geom_line(aes(x=artDate,y=count,colour=sentiment))+
  scale_x_date(labels = date_format("%m/%d"),
               limits = as.Date(c('2021-01-30','2021-03-26'))
               )

將情緒分數標準化後再畫一次圖,除了2/1時正面情緒超過負面情緒一點點,其餘時間還是以負面情緒為主。

正負情緒比例折線圖

sentiment_count %>% 
  # 標準化的部分
  group_by(artDate) %>%
  mutate(ratio = count/sum(count)) %>%
  # 畫圖的部分
  ggplot()+
  geom_line(aes(x=artDate,y=ratio,colour=sentiment))+
  scale_x_date(labels = date_format("%m/%d"),
               limits = as.Date(c('2021-01-30','2021-03-26'))
               )

我們挑出幾個情緒高點的日期 觀察每日情緒分數,約從 2 號開始議題被大量討論,5 號達到議題高峰,之後就慢慢下降。

# 查看每天的情緒分數排名
sentiment_count %>%
  select(count,artDate) %>%
  group_by(artDate) %>%
  summarise(sum = sum(count)) %>%
  arrange(desc(sum))
## # A tibble: 21 x 2
##    artDate      sum
##    <date>     <int>
##  1 2021-02-05  6106
##  2 2021-02-03  4538
##  3 2021-02-04  3543
##  4 2021-02-06  2735
##  5 2021-02-02  1861
##  6 2021-02-07  1549
##  7 2021-02-08   950
##  8 2021-02-15   534
##  9 2021-02-17   334
## 10 2021-02-16   268
## # ... with 11 more rows

4. 畫出文字雲

挑出有興趣的日期,畫出文字雲看看都在討論甚麼主題。

先從2021-02-05的情緒高點看起,呼應上面負面的情緒分析,出現「性騷擾」、「噁心」、「騷擾」、「垃圾」等詞彙。推測是因許多網友抨擊與雞排妹相關之性騷擾事件,而其中「翁立友」一詞出現了 249 次,可推測翁立友為此事件之重要利害關係人之一。

2021-02-05 文字雲

# 畫出文字雲

word_count %>% 
  filter(!(word %in% c("雞排妹"))) %>%
  filter(artDate == as.Date('2021-02-05')) %>% 
  select(word,count) %>% 
  group_by(word) %>% 
  summarise(count = sum(count)) %>%
  arrange(desc(count)) %>%
  filter(count>30) %>%   # 過濾出現太少次的字
  wordcloud2()
## Adding missing grouping variables: `artDate`

看前後兩天的討論情況

2021-02-03的文字雲,可發現「飛機杯」一詞出現了557次,經查資料後發現2/03當日記排妹在記者會上放了兩個飛機杯,因此在論壇中被大量討論,同時也可看到「自殺」、「提告」等負面詞彙出現多次。

2021-03-17 文字雲

# 畫出文字雲
plot_0317 = word_count %>% 
  filter(!(word %in% c("雞排妹"))) %>%
  filter(artDate == as.Date('2021-02-03')) %>% 
  select(word,count) %>% 
  group_by(word) %>% 
  summarise(count = sum(count)) %>%
  arrange(desc(count)) %>%
  filter(count>30) %>%   # 過濾出現太少次的字
  wordcloud2()
## Adding missing grouping variables: `artDate`
plot_0317

2021-03-18的文字雲,可以看出此時出現「阿北」(經查證原文後,此指柯文哲)、「民進黨」等詞彙,可見此時風向偏向討論雞排妹在政治相關的議題,同時「噁心」一負面情緒詞彙也出現多次。

2021-03-18 文字雲

# 畫出文字雲
plot_0318 = word_count %>% 
  filter(!(word %in% c("雞排妹"))) %>%
  filter(artDate == as.Date('2021-02-07')) %>% 
  select(word,count) %>% 
  group_by(word) %>% 
  summarise(count = sum(count)) %>%
  arrange(desc(count)) %>%
  filter(count>30) %>%   # 過濾出現太少次的字
  wordcloud2()
plot_0318

5.找出情緒字典代表字

算出所有字詞的詞頻(sentiment_sum),找出情緒代表字

正負情緒代表字

# sentiment_sum:word,sentiment,sum
sentiment_sum <- 
  word_count %>%
    inner_join(LIWC) %>%
    group_by(word,sentiment) %>%
  summarise(
    sum = sum(count)
  ) %>% 
  arrange(desc(sum)) %>%
  data.frame() 
## Joining, by = "word"
## `summarise()` has grouped output by 'word'. You can override using the `.groups` argument.
sentiment_sum %>%
  top_n(30,wt = sum) %>%
  mutate(word = reorder(word, sum)) %>%
  ggplot(aes(word, sum, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to sentiment",
       x = NULL) +
  theme(text=element_text(size=14))+
  coord_flip()

另外一種呈現方式

正負情緒文字雲

sentiment_sum %>%
  acast(word ~ sentiment, value.var = "sum", fill = 0) %>%
  comparison.cloud(
    colors = c("salmon", "#72bcd4"), # positive negative
                   max.words = 50)

另外,也可以依據不同日期觀察情緒代表字的變化

2021-02-03 正負情緒代表字

sentiment_sum_select <- 
word_count %>%
  filter(artDate == as.Date('2021-02-03')) %>% 
    inner_join(LIWC) %>%
    group_by(word,sentiment) %>%
  summarise(
    sum = sum(count)
  ) %>% 
  arrange(desc(sum)) %>%
  data.frame() 
## Joining, by = "word"
## `summarise()` has grouped output by 'word'. You can override using the `.groups` argument.
sentiment_sum_select   %>%
  top_n(30,wt = sum) %>%
  ungroup() %>% 
  mutate(word = reorder(word, sum)) %>%
  ggplot(aes(word, sum, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to sentiment 0203",
       x = NULL) +
  theme(text=element_text(size=14))+
  coord_flip()

2021-02-03 正負情緒文字雲

sentiment_sum_select %>%
  acast(word ~ sentiment, value.var = "sum", fill = 0) %>%
  comparison.cloud(
    colors = c("salmon", "#72bcd4"), # positive negative
                   max.words = 50)

6.歸類正負面文章

之前的情緒分析大部分是全部的詞彙加總,接下來將正負面情緒的文章分開,看看能不能發現一些新的東西。接下來歸類文章,將每一篇文章正負面情緒的分數算出來,然後大概分類文章屬於正面還是負面。

# 依據情緒值的正負比例歸類文章
article_type = 
  data_select %>%
  inner_join(LIWC) %>% 
  group_by(artUrl,sentiment) %>%
  summarise(count=n()) %>%
  spread(sentiment,count,fill = 0) %>% #把正負面情緒展開,缺值補0
  mutate(type = case_when(positive > negative ~ "positive", 
                             TRUE ~ "negative")) %>%
  data.frame() 
## Joining, by = "word"
## `summarise()` has grouped output by 'artUrl'. You can override using the `.groups` argument.
# 看一下正負比例的文章各有幾篇
article_type %>%
  group_by(type) %>%
  summarise(count = n())
## # A tibble: 2 x 2
##   type     count
##   <chr>    <int>
## 1 negative   483
## 2 positive   124

可以看到在2/5號負面文章來到最高。

正負情緒文章數量統計圖

# 
article_type_date = left_join(article_type[,c("artUrl", "type")], MetaData[,c("artUrl", "artDate")], by = "artUrl")


article_type_date %>%
  group_by(artDate,type) %>%
  summarise(count = n()) %>%
  ggplot(aes(x = artDate, y = count, fill = type)) + 
  geom_bar(stat = "identity", position = "dodge")+
  scale_x_date(labels = date_format("%m/%d"),
               limits = as.Date(c('2021-01-30','2021-02-10'))
               )
## `summarise()` has grouped output by 'artDate'. You can override using the `.groups` argument.
## Warning: Removed 15 rows containing missing values (geom_bar).

把正面和負面的文章挑出來,並和斷詞結果合併。

# negative_article:artUrl,word
negative_article <-
article_type %>%
  filter(type=="negative")%>%
  select(artUrl) %>%
  left_join(data_select[,c("artUrl", "word")], by = "artUrl")

# positive_article:artUrl,word
positive_article <-
article_type %>%
  filter(type=="positive")%>%
  select(artUrl) %>%
  left_join(data_select[,c("artUrl", "word")], by = "artUrl")

畫出正負面文章情緒貢獻度較高的關鍵字

情緒關鍵字:負面情緒文章

# 負面情緒關鍵字貢獻圖
negative_article %>%
inner_join(LIWC) %>%
    group_by(word,sentiment) %>%
  summarise(
    sum = n()
    )%>% 
  arrange(desc(sum)) %>%
  data.frame() %>%
  top_n(30,wt = sum) %>%
  ungroup() %>% 
  mutate(word = reorder(word, sum)) %>%
  ggplot(aes(word, sum, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to negative sentiment",
       x = NULL) +
  theme(text=element_text(size=14))+
  coord_flip()
## Joining, by = "word"
## `summarise()` has grouped output by 'word'. You can override using the `.groups` argument.

情緒關鍵字:正面情緒文章

# 正面情緒關鍵字貢獻圖
positive_article %>%
inner_join(LIWC) %>%
    group_by(word,sentiment) %>%
  summarise(
    sum = n()
    )%>% 
  arrange(desc(sum)) %>%
  data.frame() %>%
  top_n(30,wt = sum) %>%
  ungroup() %>% 
  mutate(word = reorder(word, sum)) %>%
  ggplot(aes(word, sum, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to positive sentiment",
       x = NULL) +
  theme(text=element_text(size=14))+
  coord_flip()
## Joining, by = "word"
## `summarise()` has grouped output by 'word'. You can override using the `.groups` argument.

從正負面情緒圖觀察發現,負面情緒較高的文章比較常出現「性騷擾」、「噁心」、「受害者」等討論性騷擾負面影響的字詞,比較著重在批評性騷擾者本身;正面情緒較高的文章出現較多的「支持」、「相信」、「勇敢」等字詞,著重在安慰、鼓勵被性騷擾者。

7.加入其他資料來源比較

#加入dcard資料作比較

Dcard  = fread('ilidcard_articleMetaData.csv',encoding = 'UTF-8')
DToken <- Dcard %>% unnest_tokens(word, sentence, token=customized_tokenizer)
PTT_Token <- rbind(MToken[,c("artDate","artUrl", "word")],RToken[,c("artDate","artUrl", "word")])

PTT_Token = PTT_Token %>% mutate(source = "ptt")
Dcard_Token = DToken %>% mutate(source = "dcard")

# 把資料併在一起
data_combine = rbind(PTT_Token,Dcard_Token[,c("artDate","artUrl", "word","source")])

data_combine$artDate= data_combine$artDate %>% as.Date("%Y/%m/%d")

ptt和dcard的情緒分布直方圖,可以發現dcard相較於ptt討論次數較少,但此兩個社群媒體對於此事件的情緒大都為負面情緒居多。

ptt、dcard情緒分數比較

range(Dcard$artDate)
## [1] "2021/02/01" "2021/02/24"
data_combine %>%
  inner_join(LIWC) %>%
  group_by(artDate,sentiment,source) %>%
  summarise(count = n()) %>%
  filter(artDate>='2021-01-30') %>%

  # 畫圖的部分
  ggplot(aes(x= artDate,y=count,fill=sentiment)) +
  scale_color_manual() +
  geom_col(position="dodge") +
  scale_x_date(labels = date_format("%m/%d")) +
  labs(title = "sentiment of ptt & dcard",color = "情緒類別") +
  facet_wrap(~source, ncol = 1, scales="free_y")  # scale可以調整比例尺
## Joining, by = "word"
## `summarise()` has grouped output by 'artDate', 'sentiment'. You can override using the `.groups` argument.

總結

最後總結一下之前提出的問題:

1.雞排妹性騷擾事件的討論大概出現在哪個時間點,話題高峰在哪裡?

大概在2/01有較熱烈的討論,話題高峰出現在2/05,8 號後討論熱度慢慢下降

2.正面和負面的討論內容各是甚麼,有沒有時間點上的差異?

大致上此段時間之正負面內容都差不多,比較有差異的是討論的內容,因為在發生雞排妹性騷擾事件後,大眾討論的事除了原本發生的事,後續雞排妹本人在公眾場合的一舉一動也會成為接續的話題,因此由不同時間的文字雲可看出討論內容由一開始的性騷擾事件到後來的飛機杯事件,最後也有延續到與政治相關的議題。

3.正面和負面討論的情緒分數哪個較高?

正面情緒分數和負面情緒分數都隨著討論度上升而增加,但此段時間負面情緒始終維持著高過於正面情緒的狀態。