R-段落中的单词共现频率

R-段落中的单词共现频率,r,text-mining,quanteda,R,Text Mining,Quanteda,该数据集包含26篇新闻文章的文本数据。 我想计算每个段落中的单词共现频率,但我下面的代码似乎是在一个文档(一整篇文章)中进行的。 您是否可以指定使用fcm()计算共现频率的级别(句子、段落…)? 或者是否有其他方案可以这样做 library(quanteda) library(readtext) library(tm) ##corpus tf_pb <- readtext("PB_articles.csv",text_field = "text") tf2_pb <- gsub(

该数据集包含26篇新闻文章的文本数据。 我想计算每个段落中的单词共现频率,但我下面的代码似乎是在一个文档(一整篇文章)中进行的。 您是否可以指定使用fcm()计算共现频率的级别(句子、段落…)? 或者是否有其他方案可以这样做

library(quanteda)
library(readtext)
library(tm)

##corpus
tf_pb <- readtext("PB_articles.csv",text_field = "text")
tf2_pb  <- gsub(pattern = "\\b(rifle|rifles|weapon|weapons)\\b", replace = "gun", x = tf_pb)
corpus_pb <- corpus(tf2_pb)

summary(corpus_pb)

##Tokenization&Cleaning
tkn_pb <- tokens(corpus_pb,
                 remove_url = TRUE,
                 remove_numbers = TRUE,
                 remove_punct = TRUE,
                 remove_symbols = TRUE,
                 remove_separators = TRUE)

##removeing stopwords & stemming
stm_pb <- tokens_wordstem(tkn_pb)
stw_pb <- tokens_remove(stm_pb, pattern = stopwords('en'))


##multi-word expression
multiword <- c("social media", "house of worship")
comp_toks <- tokens_compound(stw_pb, pattern = phrase(multiword))
comp_toks

##keyword_list
kw_pb <- lapply(comp_toks, function(x){ x[which(grepl("\\b(synagogu|jewish|rabbi|jew|anti-semit|pittsburgh|congre|communiti|member|hous|worship|weapon|rifle|gun|shooting|assault|attack|hate|hatr|twitter|social_media|morn|gab|white|post|gun|funer|victim|prayer|rabinowitz|suspect|religi|account|nation|door|friend|charge|shiva|wax|speech|home|leader)\\b", x))]})
head(kw_pb)

##tokenにする
tkn2_pb <- as.tokens(kw_pb)

##remove words
tkn3_pb <- tokens_select(tkn2_pb,c("next-door","consumer-friend","ar-15-gun-mass-shootings.html",
                                 "hate-fuel","hate-fil","prayer.ImageOutsid","door.Th",
                                 "we-need-to-stop-the-hate-rabbi-tells-elected-leaders.html","speech.CreditCredit",
                                 "hate-rel","shooting.Credit","shooting.Polic","pittsburgh-shooting-synagogue.html",
                                 "media.Ar","shooting.Speedi","speech.Gab","shooting.Thi","shooting.On","gun-control",
                                 "gun.ImageAR-15","shooting.In","gun-safeti","pic.twitter.com","post-World","home.But","worship.Th"),
                         selection = "remove", padding = FALSE)

##co-occurrence frequency
fcm_pb <- fcm(tkn3_pb,
             count = "frequency")

库(quanteda)
图书馆(readtext)
图书馆(tm)
##语料库

答案是首先将语料库重塑为段落,这样新的“文档”就可以是原始文档中的段落,然后用“文档”共现上下文计算fcm

下面是一个您可以修改的示例,使用内置就职演说语料库中的前三个文档

库(“quanteda”)
##软件包版本:2.0.1
数据_语料库_就职典礼平均百分比
删除标记(停止字(“en”))
#这将在段落中创建fcm

fcmat答案是首先将语料库重塑为段落,这样新的“文档”就变成了原始文档中的段落,然后用“文档”共现上下文计算fcm

下面是一个您可以修改的示例,使用内置就职演说语料库中的前三个文档

库(“quanteda”)
##软件包版本:2.0.1
数据_语料库_就职典礼平均百分比
删除标记(停止字(“en”))
#这将在段落中创建fcm

fcmat由于您没有提供数据,我无法给出明确的答案(这就是为什么我不将此作为答案发布的原因)。但是你应该尝试的第一件事是重塑语料库
corpus_pb_param由于您没有提供数据,我无法给出明确的答案(这就是为什么我不将此作为答案发布的原因)。但是你应该尝试的第一件事是重塑语料库<代码>语料库参数