R code to search a word in a paragraph and copy the sentence in a variable

↘锁芯ラ 提交于 2020-01-25 17:35:46

问题


Input

Juventus striker Carlos Tevez has been cleared to start the season with the Italian club after the former Manchester City star.When Tevez agreed to join Juve earlier in the close season, the Argentine still had to complete the majority of his 250 hours of unpaid work, imposed by Macclesfield Magistrates Court as punishment for a string of driving offences earlier this year.However, the terms have now changed, meaning Tevez will pay a fine instead of fulfilling the order, allowing him to continue his career with Juve.District Judge Bridget Knight accepted there were circumstances beyond Tevez control in his inability to fulfil the terms of his order, saying:"This is only a technical breach. It is not, I repeat not, a case of a footballer thumbing his nose at a court order.It is thought Tevez legal team argued that as the former Manchester United star was sold by City, he was not responsible in law for his inability to comply with the order.I would like to thank the court for its understanding.

Question

We Need to search a word in the above given paragraph like "Carlos" and copy the entire sentence as show below

Output Juventus striker Carlos Tevez has been cleared to start the season with the Italian club after the former Manchester City star.

I the above output we can find the word "Carlos". Need your help to build a r code in this case


回答1:


Here's one approach using the qdap package:

x <- 'Juventus striker Carlos Tevez has been cleared to start the season with the Italian club after 
    the former Manchester City star.When Tevez agreed to join Juve earlier in the close season, the 
    Argentine still had to complete the majority of his 250 hours of unpaid work, imposed by 
    Macclesfield Magistrates Court as punishment for a string of driving offences earlier this year.
    However, the terms have now changed, meaning Tevez will pay a fine instead of fulfilling the order, 
    allowing him to continue his career with Juve.District Judge Bridget Knight accepted there were 
    circumstances beyond Tevez control in his inability to fulfil the terms of his order, saying: 
    "This is only a technical breach. It is not, I repeat not, a case of a footballer thumbing his nose 
    at a court order.It is thought Tevez legal team argued that as the former Manchester United star was 
    sold by City, he was not responsible in law for his inability to comply with the order.I would like 
    to thank the court for its understanding.'

library(qdap)

dat <- data.frame(text=sent_detect(x), stringsAsFactors = FALSE)
Search(dat, "Carlos")

## [1] "Juventus striker Carlos Tevez has been cleared to start the season with the Italian club after the former Manchester City star."



回答2:


First I would separate the text into sentences (assuming you saved the text in a text file called input.txt):

text <- readLines("input.txt")
sentences <- strsplit(text,".",fixed=TRUE)[[1]]

Then I would select the sentences containing the word you are searching for, using grep:

sentences <- sentences[grep("Carlos",sentences)]

#[1] "Juventus striker Carlos Tevez has been cleared to start the season with the Italian club after the former Manchester City star"


来源:https://stackoverflow.com/questions/26438570/r-code-to-search-a-word-in-a-paragraph-and-copy-the-sentence-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!