2017-12-27 124 views
-1

我是R的初学者,学习基础知识 尝试检索包含某些特定词语的句子 我使用readLines()读取文件数据并使用grep尝试检索某些特定语句 但检索的数据是包含特定单词完整的段落如何将段落拆分为R语言中的行

x<- readLines(filepath) 
grep("processor",x,value=TRUE,ignore.case=TRUE) 

如果我的话是包含处理器“处理器”,然后完整段落检索

输出: 第5代Corei3过程或者8GB内存,2GB图形处理器,1TB硬盘,15.6英寸720p高清防反射显示屏,这款笔记本电脑是该领域的高端产品。来自惠普这样的品牌,可以为您提供在开展业务时可能需要的状态价值和企业服务。

但我只想要单句 即第5代Corei3处理器,8GB内存,2GB图形处理器,1TB硬盘,15.6英寸720p高清抗反射显示屏,这款笔记本电脑是该领域的高端产品。

如何将段落拆分成行。这样我就只能得到包含特定单词的句子,并且grep很好用或不可用

+1

我们没有文字可以帮助您。这不是一个最小的,工作的,可重复的例子,并且可能会被关闭。 – hrbrmstr

+0

添加到@hrbrmstr评论 - 请阅读[如何创建最小,完整和可验证示例](https://stackoverflow.com/help/mcve)并更新您的帖子。 –

回答

0

quanteda包可用于将文本输入标记为句子。一旦文档被分成句子,grep()可用于将包含单词处理器的句子提取到矢量中。我们将使用原始文本文件,将其解释为quanteda中的2个文档,并提取包含单词处理器的句子。

rawText <- "A 5th Gen Core i3 processor, 8GB RAM, 2GB graphics processor, 1TB HDD, 15.6-inch 720p HD antireflective display, this laptop is a premium offering in this segment. Coming from a brand like HP this offers you the status value and corporate services that you might need while conducting business. 
Intel® Celeron® processor N3160. Entry-level quad-core processor for general e-mail, Internet and productivity tasks. 4GB system memory for basic multitasking: Adequate high-bandwidth RAM to smoothly run multiple applications and browser tabs all at once." 

library(quanteda) 
sentences <- tokens(rawText,"sentence") 
unlist(lapply(sentences,function(x){ 
    grep("processor",x,value=TRUE) 
})) 

...和输出:

> unlist(lapply(sentences,function(x){ 
+  grep("processor",x,value=TRUE) 
+ })) 


text11 

"A 5th Gen Core i3 processor, 8GB RAM, 2GB graphics processor, 1TB HDD, 15.6-inch 720p HD antireflective display, this laptop is a premium offering in this segment." 


text12 


"Intel® Celeron® processor N3160." 


text13 


"Entry-level quad-core processor for general e-mail, Internet and productivity tasks." 
> 

另一种方法是使用stringi::str_detect_fixed()找到字符串。

# stringi::stri_detect_fixed() approach 
library(stringi) 
unlist(lapply(sentences,function(x){ 
     x[stri_detect_fixed(x,"processor")] 
})) 
+0

是的,谢谢你的帮助 – sampurna