使用 R 的累积字数统计
Cumulative Word Count using R
有没有办法获取一段文字的累计字数?我有一篇要分析的文本,我想找到文本中总字数的累积计数以及文本中某些字词的累积计数。
目前我有 3 个独立的数据框。第一个包含文本文档中的所有单词,"count" 列一直向下包含 1,"total" 列给出 "count" 列的累积和。其他两个数据框完全相同,除了它们只包含我在文本中查找的特定单词的所有出现。
目标是制作一个情节,显示整个文本中两个特定词的使用关系。
感谢任何帮助。以下是我目前所拥有的。
URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
romeo <- htmlParse(URL)
txPath <- "//blockquote//a"
txValue <- xpathApply(romeo, txPath, xmlValue)
txValue <- strsplit(gsub('\n','',txValue), split=" ")
words <- unlist(str_extract_all(txValue,'(\w+)\'*(\w+)'))
vWord <- tolower(words)
rCount <- unlist(str_extract_all(vWord,'(romeo)'))
lCount <- unlist(str_extract_all(vWord,'(love)'))
rDF <- as.data.frame(rCount) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
lDF <- as.data.frame(lCount) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
wordsDF <- as.data.frame(vWord) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
如果您包含数据和所需的输出,将会有所帮助。但根据我的理解,你能否使用你的 'first' data.frame 来做一些事情,例如 (via dplyr
):
我对你的看法 'first' data.frame:
df <- data.frame(word = c("a", "b", "c", "a", "a", "c", "d", "b", "a"),
count = rep(1,9))
library(dplyr)
df %>% group_by(word) %>% mutate(cumsum= cumsum(count))
输出:
word count cumsum
(fctr) (dbl) (dbl)
1 a 1 1
2 b 1 1
3 c 1 1
4 a 1 2
5 a 1 3
6 c 1 2
7 d 1 1
8 b 1 2
9 a 1 4
而且,因为我需要强迫自己学习 data.table
,这里有一个使用它的解决方案:
library(data.table)
setDT(df)[, cumsum:=cumsum(count), by=word]
这展示了如何使用 stringi
(比内置字符串操作更快更灵活)进行语料库切片和切块,以及一种绘制您正在寻找的比较的方法:
library(xml2)
library(rvest)
library(dplyr)
library(stringi)
library(ggplot2)
URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
wherefore <- read_html(URL)
txt <- stri_trim(html_text(html_nodes(wtxtherefore, "blockquote > a")))
corpus <- data_frame(word=stri_trans_tolower(unlist(stri_extract_all_words(txt))),
count=1)
corpus$word_number <- 1:nrow(corpus)
cumsum_corpus <- mutate(group_by(corpus, word), cumsum=cumsum(count))
gg <- ggplot(filter(cumsum_corpus, word %in% c("romeo", "juliet")),
aes(x=word_number, y=cumsum))
gg <- gg + geom_line(aes(color=word), size=0.75)
gg <- gg + geom_point(aes(fill=word), shape=21, color="white", size=1.5)
gg <- gg + scale_x_continuous(limits=c(1, nrow(corpus)))
gg <- gg + theme_bw()
gg
有没有办法获取一段文字的累计字数?我有一篇要分析的文本,我想找到文本中总字数的累积计数以及文本中某些字词的累积计数。
目前我有 3 个独立的数据框。第一个包含文本文档中的所有单词,"count" 列一直向下包含 1,"total" 列给出 "count" 列的累积和。其他两个数据框完全相同,除了它们只包含我在文本中查找的特定单词的所有出现。
目标是制作一个情节,显示整个文本中两个特定词的使用关系。
感谢任何帮助。以下是我目前所拥有的。
URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
romeo <- htmlParse(URL)
txPath <- "//blockquote//a"
txValue <- xpathApply(romeo, txPath, xmlValue)
txValue <- strsplit(gsub('\n','',txValue), split=" ")
words <- unlist(str_extract_all(txValue,'(\w+)\'*(\w+)'))
vWord <- tolower(words)
rCount <- unlist(str_extract_all(vWord,'(romeo)'))
lCount <- unlist(str_extract_all(vWord,'(love)'))
rDF <- as.data.frame(rCount) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
lDF <- as.data.frame(lCount) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
wordsDF <- as.data.frame(vWord) %>%
mutate(count=1) %>%
mutate(tot=cumsum(count))
如果您包含数据和所需的输出,将会有所帮助。但根据我的理解,你能否使用你的 'first' data.frame 来做一些事情,例如 (via dplyr
):
我对你的看法 'first' data.frame:
df <- data.frame(word = c("a", "b", "c", "a", "a", "c", "d", "b", "a"),
count = rep(1,9))
library(dplyr)
df %>% group_by(word) %>% mutate(cumsum= cumsum(count))
输出:
word count cumsum
(fctr) (dbl) (dbl)
1 a 1 1
2 b 1 1
3 c 1 1
4 a 1 2
5 a 1 3
6 c 1 2
7 d 1 1
8 b 1 2
9 a 1 4
而且,因为我需要强迫自己学习 data.table
,这里有一个使用它的解决方案:
library(data.table)
setDT(df)[, cumsum:=cumsum(count), by=word]
这展示了如何使用 stringi
(比内置字符串操作更快更灵活)进行语料库切片和切块,以及一种绘制您正在寻找的比较的方法:
library(xml2)
library(rvest)
library(dplyr)
library(stringi)
library(ggplot2)
URL <- 'http://shakespeare.mit.edu/romeo_juliet/full.html'
wherefore <- read_html(URL)
txt <- stri_trim(html_text(html_nodes(wtxtherefore, "blockquote > a")))
corpus <- data_frame(word=stri_trans_tolower(unlist(stri_extract_all_words(txt))),
count=1)
corpus$word_number <- 1:nrow(corpus)
cumsum_corpus <- mutate(group_by(corpus, word), cumsum=cumsum(count))
gg <- ggplot(filter(cumsum_corpus, word %in% c("romeo", "juliet")),
aes(x=word_number, y=cumsum))
gg <- gg + geom_line(aes(color=word), size=0.75)
gg <- gg + geom_point(aes(fill=word), shape=21, color="white", size=1.5)
gg <- gg + scale_x_continuous(limits=c(1, nrow(corpus)))
gg <- gg + theme_bw()
gg