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")]
}))
我们没有文字可以帮助您。这不是一个最小的,工作的,可重复的例子,并且可能会被关闭。 – hrbrmstr
添加到@hrbrmstr评论 - 请阅读[如何创建最小,完整和可验证示例](https://stackoverflow.com/help/mcve)并更新您的帖子。 –