【发布时间】:2018-08-09 01:02:33
【问题描述】:
我有一个数据框如下:
df<-data.frame(revtext=c('the dog that chased the cat', 'the dog which chased the cat', 'World Cup Hair 2014 very funny.i can change', 'BowBow', 'this is'), rid=c('r01','r02','r03','r04','r05'), stringsAsFactors = FALSE)
revtext rid
the dog that chased the cat r01
the dog which chased the cat r02
World Cup Hair 2014 very funny.i can change r03
Bow Bow r04
this is r05
我正在使用包textreuse 将df 转换为corpus 做:
#install.packages(textreuse)
library(textreuse)
d<-df$revtext
names(d)<-df$rid
corpus <- TextReuseCorpus(text = d,
tokenizer = tokenize_character, k=3,
progress = FALSE,
keep_tokens = TRUE)
tokenize_character 是我编写的函数:
tokenize_character <- function(document, k) {
shingles<-c()
for( i in 1:( nchar(document) - k + 1 ) ) {
shingles[i] <- substr(document,start=i,stop= (i+k-1))
}
return( unique(shingles) )
}
但是,我收到了一些警告提示:Skipping document with ID 'r04' because it has too few words to create at least two n-grams with n = 3.。但请注意,我的标记器在字符级别上工作。 r04 的文字够长。事实上,如果我们运行tokenize_character('BowBow',3),我们会得到:"Bow" "owB" "wBo"。
另请注意,对于r01,TextReuseCorpus 正在按预期工作,返回:tokens(corpus)$`r01= "the" "he " "e d" " do" "dog" "og " "g t" " th" "tha" "hat" "at " "t c" " ch" "cha" "has" "ase" "sed" "ed " "d t" "e c" " ca" "cat"
有什么建议吗?我不知道我在这里错过了什么。
【问题讨论】:
-
这里已经放了很多信息了。但我不太明白你工作的目的。如果你在做文本挖掘,你为什么期望有一些片段,例如'ca','ed'?
-
我想计算文本之间的相似度。即使这没有意义,该函数也不应该那样表现。我的意思是,问题不在于文本挖掘是否合适。
-
好的,对我有意义。
标签: r nlp text-mining corpus