R:quanteda 从语料库中删除标签

R: quanteda removing tags from corpus

我正在使用 quanteda 包处理一些数字文本。我的文本中包含标签,其中一些具有唯一值,例如 URL。我不仅要删除标签,还要删除标签内的所有内容。

示例:

<oa>
</oa>
<URL: http://in.answers.yahoo.com/question/index;_ylt=Ap2wvXm2aeRQKHO.HeDgTfneQHRG;_ylv=3?qid=1006042400700>
<q>
<ad>
</ad>

我不确定在使用 quanteda 包时如何删除它们。在我看来 dfm 函数将是使用它的地方,我不认为 stopwords 会起作用,因为 URL 是唯一的。我可以使用以下带有正则表达式的 gsub 来成功定位我要删除的标签:

x <- gsub("<.*?>", "", y)

我已经阅读了 gfm 文档并尝试了一些使用 remove 和 value 类型参数的方法,但也许我不太理解文档。

也如 中的答案所示,我尝试了 dfm_select 函数,但也没有骰子。

这是我的代码:

library(readtext)
library(quanteda)

#create directory
data_dir <- list.files(pattern="*.txt", recursive = TRUE, full.names = TRUE)

#create corpus    
micusp_corpus <- corpus(readtext(data_dir))

#add field 'region'
docvars(micusp_corpus, "Region") <- gsub("(\w{6})\..*?$", "", rownames(micusp_corpus$documents))

#create document feature matrix
micusp_dfm <- dfm(micusp_corpus, groups = "Region", remove_punct = TRUE, remove_numbers = TRUE, remove_symbols = TRUE)
 #try to remove tags       
micusp_dfm <- dfm_select(micusp_dfm, "<.*?>", selection = "remove", valuetype = "regex")

#show top tokens (note the appearence of the tag content "oa")
textstat_frequency(micusp_dfm, n=10)

虽然您的问题没有提供可重现的示例,但我想我可以提供帮助。在到达 dfm 构建阶段之前,您想清理进入语料库的文本。将 #create corpus 行替换为:

# read texts, remove tags, and create the corpus
tmp <- readtext(data_dir)
tmp$text <- gsub("<.*?>", "", tmp$text)
micusp_corpus <- corpus(tmp)