【问题标题】:grepl for finding wordsgrepl 查找单词
【发布时间】:2019-11-20 19:54:57
【问题描述】:

我正在尝试在 R 中查找多个单词中的西班牙语单词。我有一个 excel 中的所有西班牙语单词,我不知道如何附加到帖子中(它有超过 80000 个单词),我正在尝试检查其中是否有一些单词。

例如:

words = c("Silla", "Sillas", "Perro", "asdfg")

我尝试使用这个solution

grepl(paste(spanish_words, collapse = "|"), words) 

但是西班牙语单词太多,并给我这个错误:

Error

那么...我可以做谁?我也试过这个:

toupper(words) %in% toupper(spanish_words)

Result

正如您所看到的,此选项仅在完全匹配时给出 TRUE,并且我需要“Sillas”也显示为 TRUE(它是 silla 的复数词)。这就是我首先尝试使用 grepl 的原因,也用于获取复数。

有什么想法吗?

【问题讨论】:

  • 我不认为 regex/grepl 是这里的解决方案,也许使用专用包,tm 包浮现在脑海中,可能还有更专业的字典样式包。
  • grepl 抛出的错误不是因为你的模式中有太多单词,而是因为你的正则表达式无效。
  • 您需要将正则表达式放在括号中。你有word|word2|word3...,但你应该有(word|word2|word3...)

标签: r regex grepl multiple-matches


【解决方案1】:

作为df:

df <- tibble(text = c("some words", 
                      "more words", 
                      "Perro", 
                      "And asdfg", 
                      "Comb perro and asdfg"))

词向量: 单词

然后使用mutatestr_detect

df %>% 
  mutate(
   text = tolower(text), 
   spanish_word = str_detect(text, words)
 )

返回:

text                 spanish_word
  <chr>                <lgl>       
1 some words           FALSE       
2 more words           FALSE       
3 perro                TRUE        
4 and asdfg            TRUE        
5 comb perro and asdfg TRUE    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2011-09-25
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多