首页 > 解决方案 > 如何使用 R 在法语中准确应用停用词

问题描述

我正在尝试使用古腾堡图书馆拉一本书,然后删除法语停用词。通过这样做,我已经能够用英语准确地做到这一点:

twistEN <- gutenberg_download(730)
twistEN <- twistEN[118:nrow(twistEN),]
twistEN <- twistEN %>%
  unnest_tokens(word, text)
data(stop_words)
twistEN <- twistEN %>%
  anti_join(stop_words)
countsEN <- twistEN %>%
  count(word, sort=TRUE)
top.en <- countsEN[1:20,]

我可以在这里看到,英文版 Oliver Twist 中排名前 20 的单词(按频率)是:

word          n
   <chr>     <int>
 1 oliver      746
 2 replied     464
 3 bumble      364
 4 sikes       344
 5 time        329
 6 gentleman   309
 7 jew         294
 8 boy         291
 9 fagin       291
10 dear        277
11 door        238
12 head        226
13 girl        223
14 night       218
15 sir         210
16 lady        209
17 hand        205
18 eyes        204
19 rose        201
20 cried       182

我正在尝试用同一部小说的法语版完成同样的事情:

twistFR <- gutenberg_download(16023)
twistFR <- twistFR[123:nrow(twistFR),]
twistFR <- twistFR %>%
  unnest_tokens(word, text)
stop_french <- data.frame(word = stopwords::stopwords("fr"), stringsAsFactors = FALSE)
stop_french <- get_stopwords("fr","snowball")
as.data.frame(stop_french)
twistFR <- twistFR %>%
  anti_join(stop_words, by = c('word')) %>%
  anti_join(stop_french, by = c("word"))
countsFR <- twistFR %>%
  count(word, sort=TRUE)
top.fr <- countsFR[1:20,]

我确实根据我在网上找到的信息更改了法语停用词的代码,它正在删除一些停用词。但这是我得到的清单:

word         n
   <chr>    <int>
 1 dit       1375
 2 r         1311
 3 tait      1069
 4 re         898
 5 e          860
 6 qu'il      810
 7 plus       780
 8 a          735
 9 olivier    689
10 si         673
11 bien       656
12 tout       635
13 tre        544
14 d'un       533
15 comme      519
16 c'est      494
17 pr         481
18 pondit     472
19 juif       450
20 monsieur   424

这些词中至少有一半应该被停用词列表捕获,但事实并非如此。我的代码有什么地方做错了吗?我是整理文本的新手,所以我确信有更好的方法来解决这个问题。

标签: rstop-wordstidytextproject-gutenberg

解决方案


事实证明,我的主要问题实际上不是停用词。这是重音字符作为代码而不是重音出现。我应用了这个:

twistFR$text <- iconv(twistFR$text, "latin1", "UTF-8")

情况几乎自行解决了。我也应用了 stopwords-iso 更大的列表。感谢您的两位评论!


推荐阅读