【问题标题】:R: quanteda removing tags from corpusR:quanteda 从语料库中删除标签
【发布时间】:2019-03-29 23:28:52
【问题描述】:

我正在使用 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 类型参数的事情,但也许我不太了解文档。

也如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】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2012-04-10
      • 2014-12-31
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2021-03-01
      • 1970-01-01
      相关资源
      最近更新 更多