【问题标题】:quanteda - stopwords not working in Frenchquanteda - 停用词在法语中不起作用
【发布时间】:2021-03-17 12:35:59
【问题描述】:

由于某种原因,停用词不适用于我的语料库,完全是法语。这几天我一直在反复尝试,但是很多本来应该过滤的单词却没有。我不确定其他人是否有类似的问题?我在某处读到可能是因为口音。我试过stringi::stri_trans_general(x, "Latin-ASCII"),但我不确定我做对了。另外,我注意到法语停用词有时被称为“french”或“fr”。

这是我尝试过的一个代码示例,如果有任何建议,我将不胜感激。 我也手动安装了 quanteda,因为我很难下载它,所以它可以链接到那个。

text_corp <- quanteda::corpus(data,
   text_field="text")

head(stopwords("french"))

summary(text_corp)

my_dfm <- dfm(text_corp)
myStemMat <- dfm(text_corp, remove = stopwords("french"), stem = TRUE, remove_punct = TRUE, remove_numbers = TRUE, remove_separators = TRUE)

myStemMat[, 1:5]

topfeatures(myStemMat 20)

在这最后一步中,还有“etre”(成为)、“plus”(更多)、comme(“喜欢”)、avant(“之前”)、avoir(“拥有”)等词

我还尝试通过创建令牌以不同的方式过滤停用词:

tokens <-
tokens(
text_corp,
remove_numbers = TRUE,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_url = TRUE,
split_hyphens = TRUE,
include_docvars = TRUE,
)

mydfm <- dfm(tokens,
    tolower = TRUE,
   stem = TRUE,
   remove = stopwords("french")
   )

topfeatures(mydfm, 20)

【问题讨论】:

  • 当文本不是 UTF-8 时会发生这种情况。请检查您的原始数据的编码。

标签: r nlp stop-words quanteda french


【解决方案1】:

停用词可以正常工作,但是法语停用词的默认 Snowball 列表根本不包括您要删除的单词。

您可以通过检查 stopwords("fr") 返回的停用词向量来看到这一点:

library("quanteda")
## Package version: 2.1.2
c("comme", "avoir", "plus", "avant", "être") %in%
  stopwords("fr")
## [1] FALSE FALSE FALSE FALSE FALSE

这是完整的单词列表:

sort(stopwords("fr"))
##   [1] "à"        "ai"       "aie"      "aient"    "aies"     "ait"     
##   [7] "as"       "au"       "aura"     "aurai"    "auraient" "aurais"  
##  [13] "aurait"   "auras"    "aurez"    "auriez"   "aurions"  "aurons"  
##  [19] "auront"   "aux"      "avaient"  "avais"    "avait"    "avec"    
##  [25] "avez"     "aviez"    "avions"   "avons"    "ayant"    "ayez"    
##  [31] "ayons"    "c"        "ce"       "ceci"     "cela"     "celà"    
##  [37] "ces"      "cet"      "cette"    "d"        "dans"     "de"      
##  [43] "des"      "du"       "elle"     "en"       "es"       "est"     
##  [49] "et"       "étaient"  "étais"    "était"    "étant"    "été"     
##  [55] "étée"     "étées"    "étés"     "êtes"     "étiez"    "étions"  
##  [61] "eu"       "eue"      "eues"     "eûmes"    "eurent"   "eus"     
##  [67] "eusse"    "eussent"  "eusses"   "eussiez"  "eussions" "eut"     
##  [73] "eût"      "eûtes"    "eux"      "fûmes"    "furent"   "fus"     
##  [79] "fusse"    "fussent"  "fusses"   "fussiez"  "fussions" "fut"     
##  [85] "fût"      "fûtes"    "ici"      "il"       "ils"      "j"       
##  [91] "je"       "l"        "la"       "le"       "les"      "leur"    
##  [97] "leurs"    "lui"      "m"        "ma"       "mais"     "me"      
## [103] "même"     "mes"      "moi"      "mon"      "n"        "ne"      
## [109] "nos"      "notre"    "nous"     "on"       "ont"      "ou"      
## [115] "par"      "pas"      "pour"     "qu"       "que"      "quel"    
## [121] "quelle"   "quelles"  "quels"    "qui"      "s"        "sa"      
## [127] "sans"     "se"       "sera"     "serai"    "seraient" "serais"  
## [133] "serait"   "seras"    "serez"    "seriez"   "serions"  "serons"  
## [139] "seront"   "ses"      "soi"      "soient"   "sois"     "soit"    
## [145] "sommes"   "son"      "sont"     "soyez"    "soyons"   "suis"    
## [151] "sur"      "t"        "ta"       "te"       "tes"      "toi"     
## [157] "ton"      "tu"       "un"       "une"      "vos"      "votre"   
## [163] "vous"     "y"

这就是它们没有被删除的原因。我们可以从我创建的一个例子中看到这一点,使用了你的很多词:

toks <- tokens("Je veux avoir une glace et être heureux, comme un enfant avant le dîner.",
  remove_punct = TRUE
)

tokens_remove(toks, stopwords("fr"))
## Tokens consisting of 1 document.
## text1 :
## [1] "veux"    "avoir"   "glace"   "être"    "heureux" "comme"   "enfant" 
## [8] "avant"   "dîner"

如何删除它们?使用更完整的停用词列表,或通过将您想要的停用词附加到现有停用词来自定义 Snowball 列表。

mystopwords <- c(stopwords("fr"), "comme", "avoir", "plus", "avant", "être")

tokens_remove(toks, mystopwords)
## Tokens consisting of 1 document.
## text1 :
## [1] "veux"    "glace"   "heureux" "enfant"  "dîner"

您还可以使用其他停用词来源之一,例如“stopwords-iso”,其中包含您希望删除的所有单词:

c("comme", "avoir", "plus", "avant", "être") %in%
  stopwords("fr", source = "stopwords-iso")
## [1] TRUE TRUE TRUE TRUE TRUE

关于语言问题,请参阅?stopwords::stopwords 的帮助,其中指出:

每个停用词列表的语言代码使用来自https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 的两个字母的 ISO 代码。为了向后兼容,也可以使用 quanteda 包中停用词的全英文名称,尽管这些已被弃用。

关于您对stringi::stri_trans_general(x, "Latin-ASCII") 的尝试,这只会在您想删除“etre”并且您的停用词列表仅包含“être”时对您有所帮助。在下面的示例中,包含重音字符的停用词向量与已删除重音字符的自身版本连接。

sw <- "être"
tokens("etre être heureux") %>%
  tokens_remove(sw)
## Tokens consisting of 1 document.
## text1 :
## [1] "etre"    "heureux"

tokens("etre être heureux") %>%
  tokens_remove(c(sw, stringi::stri_trans_general(sw, "Latin-ASCII")))
## Tokens consisting of 1 document.
## text1 :
## [1] "heureux"

c(sw, stringi::stri_trans_general(sw, "Latin-ASCII"))
## [1] "être" "etre"

【讨论】:

  • 亲爱的 Ken,非常感谢您为此提供的所有帮助!对此,我真的非常感激。我今天出于某种原因收到以下错误消息:“在 R_decompress1Error 中重新启动中断的承诺评估内部错误 -3:延迟加载数据库 '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/stopwords/data/Rdata .rdb' 已损坏 有关支持的语言的更多信息,请参阅 ?stopwords_getlanguages。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 2012-06-29
  • 2017-01-16
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多