37-基于R:《亮劍》和《血色浪漫》用詞對(duì)比

1、數(shù)據(jù)整理

library(pacman)
p_load(dplyr,stringr)
lj <- readLines("./亮劍.txt",n=-1L,ok=T,warn=F,skipNul = T) %>% 
  gsub("[《亮劍》 作者:都梁 | 更多精彩,更多好書,盡在www.qisuu.com]","",.) %>%
  str_trim(side = "both")

lj <- lj[which(nchar(lj) > 0)]

id <- lj %>% str_match("^第.*章$|^尾聲$")
chapter <- id[which(nchar(id)>0)]

txt <- lj %>% gsub("^第.*章$|^尾聲$","★",.) %>% 
  paste0(collapse = "") %>% str_split("★") %>% unlist()

df <- tibble(chapter=chapter,content=txt[which(nchar(txt)>0)])

write.csv(df,"亮劍.csv",row.names = F)
整理前

整理后

2、讀取小說文本

lj <- read.csv("亮劍.csv",header = T,stringsAsFactors = F) %>% tbl_df()
names(lj)
## [1] "chapter" "content"
xslm <- read.csv("血色浪漫-合并章節(jié).csv",header = T,stringsAsFactors = F) %>% 
  select(chapter,content) %>% tbl_df()
names(xslm)
## [1] "chapter" "content"
levels <- c("引子","第一章","第二章","第三章","第四章","第五章","第六章","第七章","第八章","第九章","第十章","第十一章","第十二章","第十三章","第十四章","第十五章","第十六章","第十七章","第十八章","第十九章","第二十章","第二十一章","第二十二章","第二十三章","第二十四章","第二十五章","第二十六章","第二十七章","第二十八章","第二十九章","第三十章","第三十一章","第三十二章","第三十三章","第三十四章","第三十五章","第三十六章","第三十七章","第三十八章","第三十九章","第四十章","第四十一章","第四十二章","第四十三章","尾聲")

lj$chapter <- factor(lj$chapter,levels = levels)
xslm$chapter <- factor(xslm$chapter,levels = levels)

3、各章節(jié)字?jǐn)?shù)對(duì)比

p_load(ggplot2)

bind_rows(lj %>% mutate(name = "亮劍"),
          xslm %>% mutate(name = "血色浪漫")) %>% 
 mutate(n=nchar(content)) %>%
 ggplot(aes(chapter,n,fill=name)) +
   geom_col(position="stack") +
   labs(y=NULL,x=NULL) +
   theme(axis.text.x = element_text(angle = 90,hjust = 1),
        legend.position = "top",
        legend.title = element_blank())
章節(jié)字?jǐn)?shù)對(duì)比

4、中文分詞

p_load(jiebaR,purrr)
user <- "./dict/characters-master/xslm"
stopwords <- "./dict/stopwords_wf.txt"
wk <- worker(user = user,stop_word = stopwords)
tok_fun <- function(strings) {map(strings,segment,wk)}

lj$words <- lj$content %>% tok_fun
xslm$words <- xslm$content %>% tok_fun

5、詞頻對(duì)比

p_load(text2vec)

# 預(yù)處理函數(shù),英文轉(zhuǎn)小寫,并清除非字符串(標(biāo)點(diǎn)符號(hào)等)
preprocessor = function(x) {
  gsub("[^[:alnum:]\\s]", replacement = " ", tolower(x))
  }

txt.lj <- paste0(lj$words,collapse = " ")
txt.xslm <- paste0(xslm$words,collapse = " ")
df <- tibble(name ="亮劍",words=txt.lj) %>%
  bind_rows(tibble(name="血色浪漫",words=txt.xslm))

  
it <- itoken(df$words,
             ids = df$name,
             preprocessor = preprocessor,
             progressbar = F)

vocab <- create_vocabulary(it)

freq <- vocab %>% mutate(name=case_when(
  doc_count == 1 ~ "《血色浪漫》",
  doc_count == 2 ~ "《亮劍》")) %>%
  group_by(name) %>%
  select(term,term_count,name) %>%
  arrange(-term_count);freq
## # A tibble: 36,452 x 3
## # Groups:   name [2]
##    term   term_count name        
##    <chr>       <int> <chr>       
##  1 鐘躍民       3117 《血色浪漫》
##  2 說           2853 《亮劍》    
##  3 李云龍       1936 《血色浪漫》
##  4 里            972 《亮劍》    
##  5 想            955 《亮劍》    
##  6 袁軍          903 《血色浪漫》
##  7 鄭桐          776 《血色浪漫》
##  8 周曉白        716 《血色浪漫》
##  9 寧偉          703 《血色浪漫》
## 10 道            652 《亮劍》    
## # ... with 36,442 more rows
freq %>% filter(term_count>10 & term_count<1000) %>%
  ggplot(aes(term_count,term,col=name)) +
  geom_jitter(alpha = 0.1, size = 2.5, width = 0.25, height = 0.25,na.rm = T) +
  geom_text(aes(label = term),check_overlap = TRUE, vjust = 1.5,na.rm = T) +
  geom_abline(color = "red") +
  labs(x=NULL,y=NULL) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        legend.title = element_blank(),
        legend.position = "top")
詞頻對(duì)比

6、分別使用頻率最高的詞

freq %>% filter(name=="《血色浪漫》") %>%
  top_n(15)
## Selecting by name
## # A tibble: 26,114 x 3
## # Groups:   name [1]
##    term   term_count name        
##    <chr>       <int> <chr>       
##  1 鐘躍民       3117 《血色浪漫》
##  2 李云龍       1936 《血色浪漫》
##  3 袁軍          903 《血色浪漫》
##  4 鄭桐          776 《血色浪漫》
##  5 周曉白        716 《血色浪漫》
##  6 寧偉          703 《血色浪漫》
##  7 張海洋        519 《血色浪漫》
##  8 田雨          420 《血色浪漫》
##  9 躍民          412 《血色浪漫》
## 10 趙剛          388 《血色浪漫》
## # ... with 26,104 more rows
freq %>% filter(name=="《亮劍》") %>%
  top_n(15)
## Selecting by name
## # A tibble: 10,338 x 3
## # Groups:   name [1]
##    term  term_count name    
##    <chr>      <int> <chr>   
##  1 說          2853 《亮劍》
##  2 里           972 《亮劍》
##  3 想           955 《亮劍》
##  4 道           652 《亮劍》
##  5 中           643 《亮劍》
##  6 走           642 《亮劍》
##  7 事           608 《亮劍》
##  8 時(shí)           571 《亮劍》
##  9 部隊(duì)         541 《亮劍》
## 10 兩個(gè)         468 《亮劍》
## # ... with 10,328 more rows
freq %>% group_by(name) %>%
  mutate(tf = term_count / sum(term_count)) %>%
  top_n(10) %>%
  ggplot(aes(reorder(term,tf), tf, fill = name)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(x=NULL,y=NULL)
## Selecting by tf
高頻詞
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 常用概念: 自然語言處理(NLP) 數(shù)據(jù)挖掘 推薦算法 用戶畫像 知識(shí)圖譜 信息檢索 文本分類 常用技術(shù): 詞級(jí)別...
    御風(fēng)之星閱讀 9,962評(píng)論 1 25
  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,695評(píng)論 0 5
  • 我家有只可愛的泰迪小狗,它的名字叫人民幣,別人聽到它的名字都哈哈大笑。 它很可愛,下咬人,但院...
    雨涵的快樂生活閱讀 309評(píng)論 3 2
  • 五點(diǎn)半起來,臉和頭都沒洗就興奮得出了門,同行的小伙伴沒有一個(gè)能起來的! 一個(gè)人走在早上六點(diǎn)Catania的街上,時(shí)...
    少許微楓閱讀 401評(píng)論 0 2
  • 經(jīng)過9天的騎行,終于在今天即將完成騎行的使命。對(duì)于第一次騎行遠(yuǎn)門的我來說,心里一直期盼著能夠早點(diǎn)回到臺(tái)北,原本計(jì)劃...
    私享者2021閱讀 346評(píng)論 1 3

友情鏈接更多精彩內(nèi)容