tidy結構要求:a table with one-token-per-document-per-row,這使得我們可以使用流行的一整套工具,比如 dplyr歹嘹、tidyr 和 ggplot2來探索和可視化文本數據孔庭。
格式轉換
然而,除了 tidytext 包之外圆到,大多數現有的用于自然語言處理的 r 工具都不兼容這種格式芽淡。 本文討論的就是tm包生成的語料與tidy結構之間的互相轉換。
1挣菲、使用科學辟謠網數據
require(pacman)
p_load(dplyr)
df <- read.csv("./rumors.csv",header = T,stringsAsFactors = F,strip.white = T) %>%
select(title,correction) %>% tbl_df()
df$correction <- df$correction %>% gsub("<U+00A[0-9]>","",.) %>%
# 清除所有括號中的內容
gsub("\\(.*\\)","",.) %>% gsub("(.*)","",.) %>% gsub("\\\n","",.) %>%
gsub("點擊下載本組圖片","",.) %>% textclean::replace_html()
2白胀、構建DTM矩陣
p_load(tm,jiebaR,purrr)
# 中文分詞
wk <- worker(stop_word = "./dict/characters-master/stop_words")
corpus <- df %>% mutate(words = map(correction,segment,wk)) %>%
select(title,words) %>% distinct(title,.keep_all = T)
# 手動拼接為一個長的字符串
txt <- map(corpus$words,paste,collapse=" ")
# 創(chuàng)建語料庫
d.corpus <- Corpus(VectorSource(txt))
# 剔除多余的空白
d.corpus <- tm_map(d.corpus,stripWhitespace)
## Warning in tm_map.SimpleCorpus(d.corpus,
## stripWhitespace): transformation drops documents
# 剔除標點符號
d.corpus <- tm_map(d.corpus,removePunctuation)
## Warning in tm_map.SimpleCorpus(d.corpus,
## removePunctuation): transformation drops documents
# 剔除數字
d.corpus <- tm_map(d.corpus,removeNumbers)
## Warning in tm_map.SimpleCorpus(d.corpus, removeNumbers):
## transformation drops documents
ctrl <- list(wordlengths=c(1,Inf))
dtm <- DocumentTermMatrix(d.corpus,control = ctrl)
print(dtm)
## <<DocumentTermMatrix (documents: 1763, terms: 35923)>>
## Non-/sparse entries: 202385/63129864
## Sparsity : 100%
## Maximal term length: 80
## Weighting : term frequency (tf)
3、轉換為tidy結構哪怔,轉換后其他的操作跟tidytext一致
p_load(tidytext)
# 轉換為one-token-per-document-per-row的數據框
dtm.td <- tidy(dtm)
print(dtm.td)
## # A tibble: 202,385 x 3
## document term count
## <chr> <chr> <dbl>
## 1 1 一是 1
## 2 1 專家 2
## 3 1 個人行為 1
## 4 1 中 2
## 5 1 中華預防醫(yī)學會 1
## 6 1 中國 1
## 7 1 中國科學院 1
## 8 1 中心 1
## 9 1 主任 2
## 10 1 二是 1
## # ... with 202,375 more rows
# 將語料中的document名稱改為文章名稱
name <- df %>% select(title) %>%
mutate(id=as.character(1:length(df$title)))
dtm.td.name <- dtm.td %>% left_join(name,by=c("document"="id"),copy=T) %>%
select(document=title,term,count)
4、tidy結構轉換為DTM矩陣
dtm.matrix <- dtm.td.name %>% cast_dtm(document,term,count)
print(dtm.matrix)
注:目前還不清楚為何轉換后文檔數量會變少胚委,同時也無法確定文檔名稱是否與原有文章一一對應元暴。
## <<DocumentTermMatrix (documents: 1668, terms: 35923)>>
## Non-/sparse entries: 202228/59717336
## Sparsity : 100%
## Maximal term length: 80
## Weighting : term frequency (tf)
inspect(dtm.matrix[5:10, 10:20])
## <<DocumentTermMatrix (documents: 6, terms: 11)>>
## Non-/sparse entries: 10/56
## Sparsity : 85%
## Maximal term length: 3
## Weighting : term frequency (tf)
## Sample :
## Terms
## Docs 出品人 傳播 促使 低 二是
## 愛因斯坦的數學很爛 1 0 0 0 0
## 家養(yǎng)豬籠草可以有效滅蚊 1 0 0 0 0
## 利用磁鐵和鋼絲可制作永動機 1 0 0 0 0
## 月經期間洗頭會致癌 1 0 0 1 0
## 孕媽肚臍凸起生的就是兒子 1 0 0 0 0
## 最聰明的孩子都吃素 1 0 0 1 0
## Terms
## Docs 公用 幾分鐘 免疫學 體液
## 愛因斯坦的數學很爛 0 0 0 0
## 家養(yǎng)豬籠草可以有效滅蚊 0 0 0 0
## 利用磁鐵和鋼絲可制作永動機 0 0 0 0
## 月經期間洗頭會致癌 0 0 0 0
## 孕媽肚臍凸起生的就是兒子 0 0 0 0
## 最聰明的孩子都吃素 0 0 0 3
## Terms
## Docs 信息
## 愛因斯坦的數學很爛 0
## 家養(yǎng)豬籠草可以有效滅蚊 0
## 利用磁鐵和鋼絲可制作永動機 0
## 月經期間洗頭會致癌 0
## 孕媽肚臍凸起生的就是兒子 0
## 最聰明的孩子都吃素 1
5茉盏、使用tidy結構文檔快速創(chuàng)建DTM矩陣
test <- read.csv("./signature.jieba.csv",header = T,stringsAsFactors = F) %>%
dplyr::select(id,content)
signature.dtm <- test %>% unnest_tokens(word,content) %>%
dplyr::count(id,word) %>%
cast_dtm(id,word,n)
print(signature.dtm)
## <<DocumentTermMatrix (documents: 391, terms: 1412)>>
## Non-/sparse entries: 2684/549408
## Sparsity : 100%
## Maximal term length: 4
## Weighting : term frequency (tf)
6、移除稀疏元素
inspect(removeSparseTerms(dtm.matrix,0.5))
## <<DocumentTermMatrix (documents: 1668, terms: 1)>>
## Non-/sparse entries: 900/768
## Sparsity : 46%
## Maximal term length: 1
## Weighting : term frequency (tf)
## Sample :
## Terms
## Docs 中
## “竹炭食物”排毒養(yǎng)顏铜秆? 28
## 2017十大“科學”流言揭曉 19
## 保溫杯里泡枸杞”是萬能養(yǎng)生方式 16
## 吃螃蟹的禁忌 12
## 吃早餐危險讶迁、薯條治脫發(fā) 13
## 發(fā)燒吃點消炎藥 21
## 藿香正氣水和頭孢一起服用會產生劇毒 12
## 諾貝爾獎得主說,牛肉啸驯、牛奶都致癌 12
## 食品標簽會提供消費者需要的所有信息 17
## 與“水”有關的流言 13
7祟峦、查找至少出現450次的詞語
findFreqTerms(dtm.matrix,450)
## [1] "專家" "中" "中國" "發(fā)現" "含量"
## [6] "研究" "科學" "里" "食物" "作用"
## [11] "健康" "發(fā)生" "導致" "年" "影響"
## [16] "情況" "效果" "更" "月" "說"
## [21] "說法" "身體" "一種" "醫(yī)院" "吃"
## [26] "時" "營養(yǎng)" "治療" "皮膚" "真的"
## [31] "疾病" "維生素" "食品" "高" "患者"
## [36] "謠言" "食用" "人體" "含有" "請"
## [41] "來源" "藥物" "五線譜" "作者" "授權"
## [46] "網站" "蝌蚪" "轉載"
8、查找與“冠狀病毒”有0.8以上相關性的詞
findAssocs(dtm.matrix,"冠狀病毒",0.8)
## $冠狀病毒
## ncov 念念不忘 非典
## 0.91 0.91 0.91