删除语料库中具有特定单词的行

Removing rows with a specific word in Corpus

我有一个语料库,其中包含从互联网上抓取的多篇文本(新闻文章)。

一些文本包含文章中使用的照片的描述。我想删除它。

我找到了关于此主题的现有字符串,但它无法帮助我。参见 link:Removing rows from Corpus with multiple documents

我想删除包含单词 "PHOTO FILE"(大写)的每一行。此解决方案已发布:

require(tm)
corp <- VCorpus(VectorSource(txt))
textVector <- sapply(corp, as.character)
for(j in seq(textVector)) {
newCorp<-textVector
newCorp[[j]] <- textVector[[j]][-grep("PHOTO",    textVector[[j]], ignore.case = FALSE)]
}

虽然这对我来说似乎不起作用。代码运行但没有删除任何内容。

这是什么工作:

require(tm)
corp <- VCorpus(VectorSource(txt))
textVector <- sapply(corp, as.character)
newCorp <- VCorpus(VectorSource(textVector[-grep("PHOTO", textVector, 
                                              ignore.case = FALSE)]))

但这会删除所有包含该词的文件,我不希望这样。

如果有人能帮助我,我将不胜感激。

加法:

以下是其中一篇文章的示例:

[1] "Top News | Wed Apr 19, 2017 | 3:53pm BST\nFILE PHOTO: People walk accross a plaza in the Canary Wharf financial district, London, Britain, January 9, 2017. REUTERS/Dylan Martinez/File Photo\nLONDON Britain's current account deficit, one of the weak points of its economy, was bigger than previously thought in the years up to 2012, according to new estimates from the Office for National Statistics on Wednesday.\nThe figures showed British companies had paid out more interest to foreign holders of corporate bonds than initially estimated, resulting in a larger current account deficit.\nThe deficit, one of the biggest among advanced economies, has been in the spotlight since June's Brexit vote.\nBank of England Governor Mark Carney said in the run-up to the referendum that Britain was reliant on the \"陌生人的善意\",强调该国每年需要数百亿英镑的外国资金来平衡其收支。\n英国国家统计局表示,目前 2012 年的经常账户赤字占国内生产总值的 4.4%,而之前的估计为 3.7%。\n英国国家统计局将自 1998 年以来每年的赤字平均上调 0.6 个百分点。最大的修正发生在 2005 年之后。\n上次英国国家统计局上月表示,2016 年最后三个月英国的经常账户赤字下降至 GDP 的 2.4%,不到第三季度 5.3% 读数的一半。\n2012 年以后的修订数据将于 9 月 29 日公布,它由于英国公司债券收益率自 2012 年以来显着下降并在 2016 年年中触及新低,因此不清楚周三的变化是否指向进一步大幅上修。.MERUR00\nThe 英国国家统计局还上调了早先对英国人储蓄额的估计. 家庭储蓄率2012 年从之前的 8.3% 升至 9.8%,2011 年也有类似的向上修正。\n2016 年第四季度的比率尚未修正,为 3.3%,为 1963 年以来的最低水平。\n英国国家统计局表示,这些变化反映了变化个体经营者从自己的公司为自己支付股息的待遇,以及将以前包括在家庭中的慈善账户分开。\n最近几年可能会对储蓄率产生类似的大幅修正。自 2008 年初以来创造的大约 220 万个新工作岗位中,大约 40% 属于个体经营者类别。\n

所以我想删除FILE PHOTO的句子(行)

假设文本最初包含在文件 input.txt 中。 原始文件如下:

THis is a text that contains a lot
of information
and PHOTO FILE.
Great!


my_text<-readLines("input.txt")

[1] "THis is a text that contains a lot" "of information"                     "and PHOTO FILE."                    "Great!"                            

如果去掉伪元素

blah[-grep("PHOTO FILE",blah,value = F,perl=T)]  

你最终得到

[1] "THis is a text that contains a lot" "of information"                     "Great!"