输入
尤文图斯的前锋卡洛斯·特维斯已经获准与意大利俱乐部在前曼城球星之后开始本赛季的比赛。当特维斯同意在赛季结束前加盟尤文时,阿根廷人仍然必须完成他250小时的大部分时间。今年早些时候麦克尔斯菲尔德裁判法院对一系列违法驾驶行为判处无偿工作。但是,条款现在已经改变,这意味着特维斯将支付罚款而不是履行命令,这使他能够继续他在Juve.District的职业生涯。布里奇特·奈特法官接受了Tevez无法控制他无法履行其命令条款的情况,并说:“这只是一次技术性违规。我不再重复,一个足球运动员在法庭上嗤之以鼻的案例据认为,特维斯的法律团队认为,由于这位前曼联球星被曼城出售,因此无法遵守该命令,他对法律不负责任。我要感谢法院。它的理解。问题
我们需要在上面给出的段落中搜索一个单词,如“Carlos”,并复制整个句子,如下所示
输出 尤文图斯队的前锋卡洛斯·特维斯已经获准在意大利俱乐部开始本赛季,这位前曼彻斯特城球队的球星。
我在上面的输出中我们可以找到“卡洛斯”这个词。 在这种情况下需要您的帮助来构建代码
答案 0 :(得分:2)
以下是使用qdap
包的一种方法:
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."
答案 1 :(得分:1)
首先,我将文本分成句子(假设您将文本保存在名为input.txt
的文本文件中):
text <- readLines("input.txt")
sentences <- strsplit(text,".",fixed=TRUE)[[1]]
然后我会使用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"