R代码在段落中搜索单词并将句子复制到变量中 [英] R code to search a word in a paragraph and copy the sentence in a variable

查看:114
本文介绍了R代码在段落中搜索单词并将句子复制到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入

尤文图斯前锋卡洛斯·特维斯(Carlos Tevez)已被清除,可以在前曼城球星之后加盟意大利俱乐部.当特维斯在休赛期早些时候同意加盟尤文时,阿根廷人仍然需要完成他250小时的大部分工作Macclesfield治安法院判处的无偿工作是今年早些时候对一系列驾驶违法行为的惩罚,但是现在条款已经改变,这意味着Tevez将支付罚款而不是履行命令,从而允许他继续在Juve的职业生涯.布里奇特·奈特法官(Bridget Knight)接受了特维斯无法控制命令的情况,他说:这只是技术性违规.特维斯法律团队认为,由于前曼彻斯特联队球星被曼城出售,他对无法遵守该命令不承担法律责任.我要感谢法院它的理解.

问题

我们需要在上面给定的段落中搜索一个单词,例如"Carlos",然后复制整个句子,如下所示:

输出 尤文图斯前锋卡洛斯·特维斯(Carlos Tevez)已被清除,可以在前曼城球星之后加入意大利俱乐部.

在上面的输出中,我们可以找到单词"Carlos". 在这种情况下,需要您的帮助来构建r代码

解决方案

以下是使用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."

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

解决方案

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."

这篇关于R代码在段落中搜索单词并将句子复制到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆