Quanteda 随时间改变术语的相对频率
Quanteda changing rel freq of a term over time
我有一个新闻文章语料库,发布日期和时间为 'docvars'。
readtext object consisting of 6 documents and 8 docvars.
# Description: df[,10] [6 × 10]
doc_id text year month day hour minute second title source
* <chr> <chr> <int> <int> <int> <int> <int> <int> <chr> <chr>
1 2014_01_01_10_51_00… "\"新华网伦敦1… 2014 1 1 10 51 0 docid报告称若不减… RMWenv
2 2014_01_01_11_06_00… "\"新华网北京1… 2014 1 1 11 6 0 docid盘点2013… RMWenv
3 2014_01_02_08_08_00… "\"原标题:报告… 2014 1 2 8 8 0 docid报告称若不减… RMWenv
4 2014_01_03_08_42_00… "\"地球可能毁灭… 2014 1 3 8 42 0 docid地球可能毁灭… RMWenv
5 2014_01_03_08_44_00… "\"北美鼠兔看起… 2014 1 3 8 44 0 docid北美鼠兔为应… RMWenv
6 2014_01_06_10_30_00… "\"欣克力C点核… 2014 1 6 10 30 0 docid英国欲建50… RMWenv
我想测量特定术语(例如 'development')在这些文章中出现的 changing 相对频率(作为总术语的比例)文章/或作为在特定日期/月份发表的所有文章中总词数的比例)。我知道我可以计算该术语在一个月内出现在所有文章中的次数,使用:
dfm(corp, select = "term", groups = "month")
并且我可以使用以下方法获得文档中单词与总单词的相对频率:
dfm_weight(dfm, scheme = "prop")
但是我如何将这些组合在一起以获得特定术语相对于特定日期或特定月份的单词总数的频率?
我想做的是衡量一个术语的使用次数随时间的变化,但要考虑到使用的单词总数也在变化这一事实。感谢您的帮助!
我怀疑有人会在 quanteda
内提出更好的解决方案,但如果他们不这样做,您总是可以从 dfm
中提取单词并将其放入数据集中连同日期,然后制作图表。在下面的代码中,我使用了从卫报网站上抓取的一些音乐评论。我已经注释掉了从 Dropbox 的 .rda
文件中读取数据的函数。如果您愿意,欢迎您使用它 - 它很干净,但我不想无意中让某人从他们不知道的网络上下载文件。
# f <- file("https://www.dropbox.com/s/kl2cnd63s32wsxs/music.rda?raw=1")
# load(f)
## create corpus and dfm
corp <- corpus(as.character(m$body_text))
docvars(corp, "date") <- m$first_publication_date
D <- dfm(corp, remove=stopwords("english"))
## take word frequencies "wonderfully" in the dfm
## along with the date
tmp <- tibble(
word = as.matrix(D)[,"wonderfully"],
date = docvars(corp)$date,
## calculate the total number of words in each document
total = rowSums(D)
)
tmp <- tmp %>%
## turn date into year-month
mutate(yearmon =zoo::as.yearmon(date)) %>%
## group by year-month
group_by(yearmon) %>%
## calculate the sum of the instances of "wonderfully"
## divided by the sum of the total words across all
## documents in the month
summarise(prop = sum(word)/sum(total))
## make a plot.
ggplot(tmp, aes(x=yearmon, y=prop)) +
geom_line() +
labs(x= "Date", y="Wonderfully/Total # Words")
@DaveArmstrong 在这里给出了一个很好的答案,我赞成它,但可以使用一些最新的 quanteda 语法来提高效率,这有点简单。
此处的关键是保留 zoo::yearmon()
创建的日期格式,因为 dfm 分组将其强制转换为一个字符。所以我们把它打包成一个docvar,由分组保存下来,然后在ggplot()
调用中获取。
load(file("https://www.dropbox.com/s/kl2cnd63s32wsxs/music.rda?raw=1"))
library("quanteda")
## Package version: 2.1.1
## create corpus and dfm
corp <- corpus(m, text_field = "body_text")
corp$date <- m$first_publication_date %>%
zoo::as.yearmon()
D <- dfm(corp, remove = stopwords("english")) %>%
dfm_group(groups = "date") %>%
dfm_weight(scheme = "prop")
library("ggplot2")
convert(D[, "wonderfully"], to = "data.frame") %>%
ggplot(aes(x = D$date, y = wonderfully, group = 1)) +
geom_line() +
labs(x = "Date", y = "Wonderfully/Total # Words")
我有一个新闻文章语料库,发布日期和时间为 'docvars'。
readtext object consisting of 6 documents and 8 docvars.
# Description: df[,10] [6 × 10]
doc_id text year month day hour minute second title source
* <chr> <chr> <int> <int> <int> <int> <int> <int> <chr> <chr>
1 2014_01_01_10_51_00… "\"新华网伦敦1… 2014 1 1 10 51 0 docid报告称若不减… RMWenv
2 2014_01_01_11_06_00… "\"新华网北京1… 2014 1 1 11 6 0 docid盘点2013… RMWenv
3 2014_01_02_08_08_00… "\"原标题:报告… 2014 1 2 8 8 0 docid报告称若不减… RMWenv
4 2014_01_03_08_42_00… "\"地球可能毁灭… 2014 1 3 8 42 0 docid地球可能毁灭… RMWenv
5 2014_01_03_08_44_00… "\"北美鼠兔看起… 2014 1 3 8 44 0 docid北美鼠兔为应… RMWenv
6 2014_01_06_10_30_00… "\"欣克力C点核… 2014 1 6 10 30 0 docid英国欲建50… RMWenv
我想测量特定术语(例如 'development')在这些文章中出现的 changing 相对频率(作为总术语的比例)文章/或作为在特定日期/月份发表的所有文章中总词数的比例)。我知道我可以计算该术语在一个月内出现在所有文章中的次数,使用:
dfm(corp, select = "term", groups = "month")
并且我可以使用以下方法获得文档中单词与总单词的相对频率:
dfm_weight(dfm, scheme = "prop")
但是我如何将这些组合在一起以获得特定术语相对于特定日期或特定月份的单词总数的频率?
我想做的是衡量一个术语的使用次数随时间的变化,但要考虑到使用的单词总数也在变化这一事实。感谢您的帮助!
我怀疑有人会在 quanteda
内提出更好的解决方案,但如果他们不这样做,您总是可以从 dfm
中提取单词并将其放入数据集中连同日期,然后制作图表。在下面的代码中,我使用了从卫报网站上抓取的一些音乐评论。我已经注释掉了从 Dropbox 的 .rda
文件中读取数据的函数。如果您愿意,欢迎您使用它 - 它很干净,但我不想无意中让某人从他们不知道的网络上下载文件。
# f <- file("https://www.dropbox.com/s/kl2cnd63s32wsxs/music.rda?raw=1")
# load(f)
## create corpus and dfm
corp <- corpus(as.character(m$body_text))
docvars(corp, "date") <- m$first_publication_date
D <- dfm(corp, remove=stopwords("english"))
## take word frequencies "wonderfully" in the dfm
## along with the date
tmp <- tibble(
word = as.matrix(D)[,"wonderfully"],
date = docvars(corp)$date,
## calculate the total number of words in each document
total = rowSums(D)
)
tmp <- tmp %>%
## turn date into year-month
mutate(yearmon =zoo::as.yearmon(date)) %>%
## group by year-month
group_by(yearmon) %>%
## calculate the sum of the instances of "wonderfully"
## divided by the sum of the total words across all
## documents in the month
summarise(prop = sum(word)/sum(total))
## make a plot.
ggplot(tmp, aes(x=yearmon, y=prop)) +
geom_line() +
labs(x= "Date", y="Wonderfully/Total # Words")
@DaveArmstrong 在这里给出了一个很好的答案,我赞成它,但可以使用一些最新的 quanteda 语法来提高效率,这有点简单。
此处的关键是保留 zoo::yearmon()
创建的日期格式,因为 dfm 分组将其强制转换为一个字符。所以我们把它打包成一个docvar,由分组保存下来,然后在ggplot()
调用中获取。
load(file("https://www.dropbox.com/s/kl2cnd63s32wsxs/music.rda?raw=1"))
library("quanteda")
## Package version: 2.1.1
## create corpus and dfm
corp <- corpus(m, text_field = "body_text")
corp$date <- m$first_publication_date %>%
zoo::as.yearmon()
D <- dfm(corp, remove = stopwords("english")) %>%
dfm_group(groups = "date") %>%
dfm_weight(scheme = "prop")
library("ggplot2")
convert(D[, "wonderfully"], to = "data.frame") %>%
ggplot(aes(x = D$date, y = wonderfully, group = 1)) +
geom_line() +
labs(x = "Date", y = "Wonderfully/Total # Words")