首页 > 解决方案 > 过滤R列表

问题描述

我正在尝试制作一个滚动大型 get_sentences 列表的函数,找到包含特定单词的句子并将它们附加到新列表中。这是我尝试过的:

new_list <- function(words, oldlist) {
filtered_list=list()
j=1
for (i in 1:length(oldlist)) {
    for (x in 1:length(i)){
      if (grepl(paste(words,collapse="|"), oldlist[[i]][x])){
        filtered_list[[j]]<-oldlist[i]
        j=j+1
      }
    }
 }
return(filtered_list)

可悲的是,这会返回一个包含不应该出现的句子的列表。

你有什么想法吗?

更新:这就是句子的样子

> sentences[1]
[[1]]
 [1] "Auricolari davvero ottimi, piccoli, senza troppe 'lucette', con dimensione e forma giusta per far in modo che non cadano mentre si cammina o si corre."                                                                                                                                                                                                                                                                                              
 [2] "La qualità audio è decisamente tra le migliori, specie per questo prezzo e soprattutto rispetto ad altri prodotti concorrenti (suonano meglio delle Samsung Gear IconX 2018 e danno molto meno fastidio mentre vengono utilizzate)."  

> sentences[[1]]
 [1] "Auricolari davvero ottimi, piccoli, senza troppe 'lucette', con dimensione e forma giusta per far in modo che non cadano mentre si cammina o si corre."                                                                                                                                                                                                                                                                                              
 [2] "La qualità audio è decisamente tra le migliori, specie per questo prezzo e soprattutto rispetto ad altri prodotti concorrenti (suonano meglio delle Samsung Gear IconX 2018 e danno molto meno fastidio mentre vengono utilizzate)."

该结构是从 get_sentences 生成的。它对评论和句子进行编号。

课程是:

> class(sentences)
[1] "get_sentences"           "get_sentences_character" "list"     

目标是调用这个例子中的函数:

fildered<-new_list(c("quality", "headphones"), list)

并获得相同的列表,但没有不包含“质量”和“耳机”的条目

标签: r

解决方案


让我们从https://en.wikipedia.org/wiki/Harvard_sentences获取一些经典句子

harvardSentences <- read_table("Sentences
    Oak is strong and also gives shade.
    Cats and dogs each hate the other.
    The pipe began to rust while new.
    Open the crate but don't break the glass.
    Add the sum to the product of these three.
    Thieves who rob friends deserve jail.
    The ripe taste of cheese improves with age.
    Act on these orders with great speed.
    The hog crawled under the high fence.
    Move the vat over the hot fire.")

制作我们想要在句子中的单词向量

wordsList = "hog pipe jail fire" %>% 
    str_split(" ") %>% 
    first()
3 [1] "hog"  "pipe" "jail" "fire"

将它们构建成一个regex. 强制单词边界,\\b所以我们不接,例如sHOGun。这(?i)使它不区分大小写。

regex <- paste0("(?i)\\b(" ,
                paste(wordsList, collapse = "|"),
                ")\\b")
# [1] "(?i)\\b(hog|pipe|jail|fire)\\b"

现在它很filter简单str_detect

harvardSentences %>% 
    tolower() %>%
    filter(str_detect(Sentences, regex))

# A tibble: 4 x 1
  Sentences                            
  <chr>                                
1 The pipe began to rust while new.    
2 Thieves who rob friends deserve jail.
3 The hog crawled under the high fence.
4 Move the vat over the hot fire. 

如果您想计算诸如jail / jailor / jailed / jails之类的变形形式,请查看and包。tmSnowballC


推荐阅读