尝试使用|创建字符串值或运营商

时间:2017-10-05 19:01:46

标签: r text-mining stringr

我正在尝试抓一个网站链接。到目前为止,我下载了文本并将其设置为数据帧。我有以下几点;

keywords <- c(credit | model)

text_df <- as.data.frame.table(text_df)
text_df %>%
  filter(str_detect(text, keywords))

其中,信用和模型是我想要搜索网站的两个值,即返回带有信用或模型的行。

我收到以下错误

  

filter_impl(.data,dots)中的错误:object&#39; credit&#39;找不到

代码仅返回带有&#34; model&#34;的结果。 in并忽略了&#34; credit&#34;。

如何使用&#34; credit&#34;这个词返回所有结果?或&#34;模型&#34;英寸

我的计划是keywords <- c(credit | model | more_key_words | something_else | many values)

提前致谢。

编辑:

text_df:
    Var 1    text
    1        Here is some credit information
    2        Some text which does not expalin any keywords but messy <li> text9182edj </i>
    3        This line may contain the keyword model
    4        another line which contains nothing of use

所以我试图只提取第1行和第3行。

3 个答案:

答案 0 :(得分:1)

我认为问题是您需要将字符串作为参数传递给str_detect。要检查“信用”或“模型”,您可以将它们粘贴到由|分隔的单个字符串中。

library(tidyverse)
library(stringr)
text_df <- read_table("Var 1    text
1        Here is some credit information
2        Some text which does not expalin any keywords but messy <li> text9182edj </i>
3        This line may contain the keyword model
4        another line which contains nothing of use")


keywords <- c("credit", "model")
any_word <- paste(keywords, collapse = "|") 
text_df %>% filter(str_detect(text, any_word))
#> # A tibble: 2 x 3
#>     Var   `1`                                    text
#>   <int> <chr>                                   <chr>
#> 1     1               Here is some credit information
#> 2     3       This line may contain the keyword model

答案 1 :(得分:0)

好的我已经检查了它,我认为它不会起作用,因为你必须使用或| filter()内的运算符不在str_detect()

所以它会这样工作:

keywords <- c("virg", "tos")

 library(dplyr)
 library(stringr)

 iris %>%
      filter(str_detect(Species, keywords[1]) | str_detect(Species, keywords[2]))

作为keywords[1]等,您必须从此变量中指定每个“关键字”

答案 2 :(得分:0)

我建议你在处理单词时远离正则表达式。有适合您特定任务的包,您可以使用。例如,尝试以下

library(corpus)
text <- readLines("http://norvig.com/big.txt") # sherlock holmes
terms <- c("watson", "sherlock holmes", "elementary")
text_locate(text, terms)
##    text           before               instance                after             
## 1  1    …Book of The Adventures of  Sherlock Holmes                             
## 2  27     Title: The Adventures of  Sherlock Holmes                             
## 3  40   … EBOOK, THE ADVENTURES OF  SHERLOCK HOLMES  ***                        
## 4  50                               SHERLOCK HOLMES                               
## 5  77                           To  Sherlock Holmes  she is always the woman. I…
## 6  85   …," he remarked. "I think,      Watson      , that you have put on seve…
## 7  89   …t a trifle more, I fancy,      Watson      . And in practice again, I …
## 8  145  …ere's money in this case,      Watson      , if there is nothing else.…
## 9  163  …friend and colleague, Dr.      Watson      , who is occasionally good …
## 10 315  … for you. And good-night,      Watson      ," he added, as the wheels …
## 11 352  …s quite too good to lose,      Watson      . I was just balancing whet…
## 12 422  …as I had pictured it from  Sherlock Holmes ' succinct description, but…
## 13 504         "Good-night, Mister  Sherlock Holmes ."                          
## 14 515  …t it!" he cried, grasping  Sherlock Holmes  by either shoulder and loo…
## 15 553                        "Mr.  Sherlock Holmes , I believe?" said she.     
## 16 559                     "What!"  Sherlock Holmes  staggered back, white with…
## 17 565  …tter was superscribed to " Sherlock Holmes , Esq. To be left till call…
## 18 567                "MY DEAR MR.  SHERLOCK HOLMES ,--You really did it very w…
## 19 569  …est to the celebrated Mr.  Sherlock Holmes . Then I, rather imprudentl…
## 20 571  …s; and I remain, dear Mr.  Sherlock Holmes ,                           
## ⋮  (189 rows total)

请注意,无论情况如何,这都符合该条款。

针对您的具体用例,请执行

ix <- text_detect(text, terms)

matches <- text_subset(text, terms)