我从pdf文件中提取文本并创建了一个语料库对象。
在文本中,我的行以“,”或“ - ”结尾,我想向他们追加以下行,因为它属于同一个句子。
例如我有
[1566] "this and other southeastern states (Eukerria saltensis,"
[1567] "Sparganophilus helenae, Sp. tennesseensis). In the"
我想改为
[1566] "this and other southeastern states (Eukerria saltensis, Sparganophilus helenae, Sp. tennesseensis). In the"
我尝试过更换换行符,但没有成功:
tm_map(myCorpus, content_transformer(gsub), pattern =",$\n",replacement = "")
关于如何在R中做到这一点的任何想法?
答案 0 :(得分:0)
这是一种方法,基于您通过换行符拆分的想法......
txt <- c("aaa","bbc,","df","fgh-","jkh-","dfsf","gghf")
txt2 <- paste0(txt,collapse="\n")
txt2 <- gsub(",\\n",", ",txt2)
txt2 <- gsub("\\-\\n","-",txt2)
txt2 <- unlist(strsplit(txt2,"\\n"))
txt2
[1] "aaa" "bbc, df" "fgh-jkh-dfsf" "gghf"
答案 1 :(得分:0)
谢谢,它确实有效!
我必须把它放在一个函数中才能使它与tm_map一起使用,但是:
clean.X <- function(X){
X2 <- paste0(X,collapse="\n")
X2 <- gsub(",\\n",", ",X2)
X2 <- gsub("\\-\\n","-",X2)
X2 <- unlist(strsplit(X2,"\\n"))
return(X2)
}
txt2 <- tm_map(txt, content_transformer(clean.X))