【问题标题】:find a string in sentence in R在R中的句子中找到一个字符串
【发布时间】:2018-09-04 05:01:31
【问题描述】:

嗨,我正在尝试在句子中找到一个简短的文本,然后进行一些操作。在 java 中很容易,但在 R 中我遇到了一些问题。我没有达到 if 条件。 这是我的代码

rm(list=ls())
library(tidytext)
library(dplyr)

shortText= c('grt','gr8','bcz','ur')


tweet=c('stats is gr8','this car is good','your movie is grt','i hate your book of hatred','food is awsome'
        )
tweet=data.frame(tweet, stringsAsFactors = FALSE)

for(row in 1:nrow(tweet)) {

tweetWords=strsplit(tweet[row,]," ")
print(tweetWords)
  for (word in 1:length(tweetWords)) {
    if(tweetWords[word] %in% shortText){
      print('we have a match')
    }

  }

【问题讨论】:

  • 您的预期输出是什么?也许有更好的方法来做到这一点。
  • 当我找到短文本时。 gr8 或我句子中的其他单词,它应该打印'we have a match'
  • 您缺少右括号。您确定您发布了正确的代码吗?
  • 类似ifelse(grepl(paste0(shortText, collapse ="\\b|\\b "), tweet$tweet), "We have a match", "We don't have a match")

标签: r string for-loop if-statement


【解决方案1】:

有很多方法可以改善这一点。但是一个快速的解决方案,只需对您的代码进行最少的更改:

shortText= c('grt','gr8','bcz','ur')


tweet=c('stats is gr8','this car is good','your movie is grt','i hate your book of hatred','food is awsome'
)
tweet=data.frame(tweet, stringsAsFactors = FALSE)

for(row in 1:nrow(tweet)) {

  tweetWords=strsplit(tweet[row,]," ")
  print(tweetWords)
  for (word in 1:length(tweetWords)) {
    if(any(tweetWords[word][[1]] %in% shortText)){
      print('we have a match')
    }

  }
}

返回:

[[1]]
[1] "stats" "is"    "gr8"  

[1] "we have a match"
[[1]]
[1] "this" "car"  "is"   "good"

[[1]]
[1] "your"  "movie" "is"    "grt"  

[1] "we have a match"
[[1]]
[1] "i"      "hate"   "your"   "book"   "of"     "hatred"

[[1]]
[1] "food"   "is"     "awsome"

如果任何布尔运算符为 T,则添加 any 将执行 if 语句,否则它将仅使用第一个元素

【讨论】:

    【解决方案2】:

    这是一个使用 grepl 的简单基本 R 选项:

    shortText <- c('grt','gr8','bcz','ur')
    tweet <- c('stats is gr8','this car is good','your movie is grt','i hate your book of hatred','food is awsome')
    
    res <- sapply(shortText, function(x) grepl(paste0("\\b", x, "\\b"), tweet))
    tweet[rowSums(res)]
    
    [1] "stats is gr8" "stats is gr8"
    

    Demo

    基本思想是生成一个矩阵,其行是推文,其列是关键字。如果我们在给定的行中找到一个或多个 1(真)值,这意味着推文触发了一个或多个关键字。

    请注意,我用单词边界 \b 围绕每个搜索词。这是必要的,因为搜索词不会错误地匹配为较大词的子字符串。

    【讨论】:

    • tweet[rowSums(res)==1]
    【解决方案3】:

    会不会是这样的:

    cbind(tweet, ifelse(sapply(shortText, grepl, x = tweet), "Match is found", "No match"))
    
                 tweet                        grt              gr8              bcz       
        [1,] "stats is gr8"               "No match"       "Match is found" "No match"
        [2,] "this car is good"           "No match"       "No match"       "No match"
        [3,] "your movie is grt"          "Match is found" "No match"       "No match"
        [4,] "i hate your book of hatred" "No match"       "No match"       "No match"
        [5,] "food is awsome"             "No match"       "No match"       "No match"
         ur              
        [1,] "No match"      
        [2,] "No match"      
        [3,] "Match is found"
        [4,] "Match is found"
        [5,] "No match"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-29
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      相关资源
      最近更新 更多