美文网首页R语言与统计分析R语言作业数据科学与R语言
37-基于R:《亮剑》和《血色浪漫》用词对比

37-基于R:《亮剑》和《血色浪漫》用词对比

作者: wonphen | 来源:发表于2020-02-16 17:46 被阅读0次

1、数据整理

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("血色浪漫-合并章节.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、各章节字数对比

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())
章节字数对比

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、词频对比

p_load(text2vec)

# 预处理函数,英文转小写,并清除非字符串(标点符号等)
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")
词频对比

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 时           571 《亮剑》
##  9 部队         541 《亮剑》
## 10 两个         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
高频词

相关文章

  • 37-基于R:《亮剑》和《血色浪漫》用词对比

    1、数据整理 2、读取小说文本 3、各章节字数对比 4、中文分词 5、词频对比 6、分别使用频率最高的词

  • 你一定知道《亮剑》,但你可能不知道《血色浪漫》

    知道电视《亮剑》的人很多,知道小说《亮剑》的就少了些,而知道都梁这一作者的人恐怕就少之又少了。《亮剑》、《血色浪漫...

  • 李云龙和他的亮剑精神

    我看了很多遍亮剑了,无论是书还是电视剧,都很经典。《亮剑》,《血色浪漫》,《狼烟北平》,这是都梁先生的三部曲,都很...

  • 此生棠棣开荼靡(血色浪漫)

    其实想来我读血色浪漫的时候并没有很用心,比起这个,我更喜欢都梁的亮剑,小说是,电视亦如是。不过没有拍出亮剑的结局我...

  • 电视剧单 - 草稿

    电视剧,国产,血色浪漫,走向共和,大明宫词,蜗居,乔家大院,亮剑,汉武大帝 国外电视剧,权力的游戏,绝命毒师,风骚...

  • 2019-01-06

    好累,忙中偷闲,又重温亮剑这本书,还是被李云龙的气节深深的震撼,我喜欢都梁的小说,血色浪漫的主人公的气节同样烙上了...

  • 一匹血色浪漫的白驹

    血色浪漫是都梁的一篇长篇小说,或许没有亮剑的名气大。但是人物刻画的性格鲜明,尤其是钟跃民不羁的生活态度更是...

  • 血色浪漫

    《血色浪漫》,是都梁三部曲的第二部,第一部最著名,家喻户晓,妇孺皆知,就是《亮剑》。都梁是盱眙的古称,记得大学刚看...

  • 《亮剑》和《新亮剑》

    2011年,《新亮剑》筹资4000w开拍,导演邀请黄志忠出演“李云龙”,导演表示: 我们是大投资,肯定能超越李幼斌...

  • 血色浪漫

    血色浪漫 ——写在清明节到来之际 1 荒冢和黄昏,先驱和忠魂,原野和诗篇 被 烈火燃烧的血色浪漫 被信念 被革命 ...

网友评论

    本文标题:37-基于R:《亮剑》和《血色浪漫》用词对比

    本文链接:https://www.haomeiwen.com/subject/mmkmfhtx.html