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

问题描述 投票:0回答:1

我正在使用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和regex表达式成功定位我想要删除的标签:

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

我已经浏览了gfm文档并尝试了一些带有remove和value类型参数的东西,但也许我对文档的理解不太好。

同样如this question的答案所示,我尝试了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)
r regex tags corpus quanteda
1个回答
2
投票

虽然你的问题没有提供可重复的例子,但我想我可以提供帮助。在进入dfm构建阶段之前,您希望清理进入语料库的文本。用这个替换#create corpus线:

# read texts, remove tags, and create the corpus
tmp <- readtext(data_dir)
tmp$text <- gsub("<.*?>", "", tmp$text)
micusp_corpus <- corpus(tmp)
© www.soinside.com 2019 - 2024. All rights reserved.