I was wondering if it's possible to build a TermdocumentMatrix without using the package tm.
I was thinking about using two for loops in combination with a grep, but unfortunately i did not manage to create something useful.
matrix <- matrix(, nrow=length(lvector), ncol=length(lvector))
for(i in 1:length(lvector))
{
for(j in 1:length(l))
{
lijst <- grep(lvector[i],l[j])
if (length(lijst)==0)
{
matrix[i,j] == 0
}
else
{
matrix[i,j] == 1
}
}
}
thx in advance
FWIW, here's one way:
get.dtm <- function(txts) {
require(plyr)
dtm <- do.call(rbind.fill.matrix, lapply(txts, function(txt) t(table(scan(file = textConnection(txt), what = "character", quiet = TRUE)))))
dtm[is.na(dtm)] <- 0
return(dtm)
}
get.dtm(c("this is a text text", "this is just another text"))
# a is text this another just
# [1,] 1 1 2 1 0 0
# [2,] 0 1 1 1 1 1