问题
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