我如何使用一些关键字来查找哪些文章包含这些关键字?
How can I use some keywords to find which articles contain these keywords?
我是 R 的新程序员。我有一些文章 (.txt) 保存在一个文件夹中。
现在我可以在R中导入文章了。我有两种方法,我不知道哪种更好。
这是我的代码:
# 1
library(tm)
cname <- file.path("D:/magazine_pass")
docs <- Corpus(DirSource(cname), readerControl=list(reader=readPlain))
# 2
dir.list <- list.files("D:/magazine_pass" , full.name = TRUE)
for(i in 1:length(dir.list)){
file0 <- dir.list[i]
s <- readLines(file0,encoding="ASCII")
s <- sapply(s,function(row) iconv(row, "ASCII", "ASCII", sub=""))
}
而且我也在尝试使用一些 biokeywords(ex.clean energy,wearable device)
来查找哪些文章包含这些关键字。
我该怎么做?
请给我看代码并简单描述一下。非常感谢。
label1 = subset(docs, grepl(paste(c("clean energy","wearable device"), collapse = "|"), docs))
这应该查看您的语料库并提取包含 grepl 函数内单词的所有条目。基本的 grep 函数在文件中搜索与提供的模式匹配的字符串模式。 grepl returns TRUE/FALSE 的逻辑向量,表示模式是否在函数内匹配。
我是 R 的新程序员。我有一些文章 (.txt) 保存在一个文件夹中。 现在我可以在R中导入文章了。我有两种方法,我不知道哪种更好。
这是我的代码:
# 1
library(tm)
cname <- file.path("D:/magazine_pass")
docs <- Corpus(DirSource(cname), readerControl=list(reader=readPlain))
# 2
dir.list <- list.files("D:/magazine_pass" , full.name = TRUE)
for(i in 1:length(dir.list)){
file0 <- dir.list[i]
s <- readLines(file0,encoding="ASCII")
s <- sapply(s,function(row) iconv(row, "ASCII", "ASCII", sub=""))
}
而且我也在尝试使用一些 biokeywords(ex.clean energy,wearable device)
来查找哪些文章包含这些关键字。
我该怎么做?
请给我看代码并简单描述一下。非常感谢。
label1 = subset(docs, grepl(paste(c("clean energy","wearable device"), collapse = "|"), docs))
这应该查看您的语料库并提取包含 grepl 函数内单词的所有条目。基本的 grep 函数在文件中搜索与提供的模式匹配的字符串模式。 grepl returns TRUE/FALSE 的逻辑向量,表示模式是否在函数内匹配。