【问题标题】:Remove 2 stopwords lists with Quanteda package R使用 Quanteda 包 R 删除 2 个停用词列表
【发布时间】:2021-08-26 07:57:18
【问题描述】:

我正在使用语料库数据帧上的 quanteda 包,这是我使用的基本代码:

library(quanteda)

fmsi_des <- dfm(corpus_des, remove=stopwords("spanish"), verbose=TRUE,
                remove_punct=TRUE, remove_numbers=TRUE)

但是,我有另一个停用词列表作为数据框,称为 stpw,我想考虑一下。

我试过了:

fmsi_des <- dfm(corpus_des, remove=stopwords("spanish","stpw"), verbose=TRUE,
                remove_punct=TRUE, remove_numbers=TRUE)

停用词错误(“西班牙语”,“stpw”):未使用的参数(“stpw”)

然后我创建了一个列表,其中包含“西班牙语”的停用词 + stpw 的停用词:

all_stops <- c("bogota","vias","medellin","valle","departamento",stopwords("spanish"))

fmsi_des <- dfm(corpus_des, remove=stopwords("all_stops"), verbose=TRUE,
                remove_punct=TRUE, remove_numbers=TRUE)

停用词错误(“all_stops”):没有可用于“all_stops”的停用词

我还用我的停用词创建了一个 txt 文件,以便尝试:

library(tm)

stopwords = readLines('stpw.txt') 
x  = fd$contract_description        
x  =  removeWords(x,stopwords)

des <- subset(x, !is.na(x))
corpus_des <- corpus(des$fd.contract_description)
fmsi_des <- dfm(corpus_des, remove=stopwords("spanish"), verbose=TRUE,
                remove_punct=TRUE, remove_numbers=TRUE)
     

警告信息: 在 readLines("stp.txt") 中:在 'stpw.txt' 中找到不完整的最后一行

gsub(sprintf("(*UCP)\b(%s)\b", paste(sort(words, reduction = TRUE)) 中的错误: 不正确的正则表达式 '(*UCP)\b(bogota|vias|medellin|valle|departamento|+)\b' 另外:警告信息: 在 gsub(sprintf("(*UCP)\b(%s)\b", paste(sort(words, reduction = TRUE), : PCRE 模式编译错误 “没有什么可重复的” 在'+)\b'

【问题讨论】:

标签: r text-mining corpus stop-words quanteda


【解决方案1】:

在这种情况下,知道 R 中返回对象的值是获得所需结果的关键。具体来说,您需要知道 stopwords() 返回什么,以及它的第一个参数应该是什么。

stopwords(language = "sp") 使用默认的source = "snowball" 列表返回西班牙语停用词的字符向量。 (详情请参阅?stopwords。)

因此,如果您想删除默认的西班牙语列表加上您自己的单词,您可以将返回的字符向量与其他元素连接起来。这就是您在创建all_stops 时所做的。

所以要删除all_stops -- 在这里,使用 quanteda v3 建议的用法 -- 您只需执行以下操作:

fmsi_des <- corpus_des %>%
    tokens(remove_punct = TRUE, remove_numbers = TRUE) %>%
    tokens_remove(pattern = all_stops) %>%
    dfm()

【讨论】:

  • 不幸的是我的输出中仍然有停用词:(
  • 不,我的错误,我为我的主题命令尝试了错误的数据。它有效,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 2018-09-28
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
相关资源
最近更新 更多