【问题标题】:trying to create a value of strings using the | or operator尝试使用 | 创建字符串的值或运算符
【发布时间】:2017-10-05 19:01:45
【问题描述】:

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

keywords <- c(credit | model)

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

credit 和 model 是我要搜索网站的两个值,即返回带有单词 credit 或 model in 的行。

我收到以下错误

filter_impl(.data, dots) 中的错误:找不到对象“credit”

代码只返回带有单词“model”的结果,忽略单词“credit”。

我怎样才能返回所有带有单词“credit”或“model”的结果。

我的计划是keywords &lt;- 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 行。

【问题讨论】:

  • 现在无法检查,但filter_() 应该可以工作
  • 寻求帮助时,您应该提供reproducible example,其中包含示例输入和所需的输出。通常,您需要在 data.frames 中搜索特定列的值,而不是整个行,因此最好在这里更具体。
  • 如果有帮助,我已经创建了一个简化的示例。

标签: r text-mining stringr


【解决方案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

【讨论】:

  • 感谢您的回复!我在我的文本文件上运行了你的代码,它可以工作,但是我拥有的文本文件比我放在这里的文件要混乱得多,所以我得到了正确的结果,但输出中也有一些额外的噪音。 (对不起,这是我的错!)但它仍然有效。
  • @user113156,我不确定你所说的输出中的额外噪音是什么意思。您可以对搜索更加严格。例如any_word &lt;- paste0("\\b(?:", paste(keywords, collapse = "|"), ")\\b") 我认为只有在关键字是独立词时才匹配。
  • 它给了我包含关键字的正确行,但我所说的额外噪音是它还给了我额外的 HTML 输出(在不同的行中),其中没有请求的关键字,其中我不明白为什么......
【解决方案2】:

好的,我已经检查过了,我认为它对你不起作用,因为你必须使用 or | 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] 等,您必须从该变量中指定每个“关键字”

【讨论】:

  • 我认为iris %&gt;% filter(str_detect(Species, paste(keywords, collapse = "|"))) 会达到同样的效果。
  • 感谢您的回复,我运行了这个版本,将名称替换为与我的数据集名称相对应的名称,结果非常好,它需要更多的工作,指定关键字[3]、关键字[ 4],关键字[x]等,但它有效。再次感谢!
【解决方案3】:

我建议您在处理单词时远离正则表达式。您可以使用为您的特定任务量身定制的软件包。例如,尝试以下操作

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)

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2013-05-14
    • 2014-02-17
    • 1970-01-01
    • 2021-12-15
    • 2023-03-30
    • 1970-01-01
    • 2017-04-06
    • 2020-04-17
    相关资源
    最近更新 更多