I need calculate Jaccard similarity between each words in 2 vectors. Each word by each word. And extract most similar word.
Here is my bad bad slow code:
txt1 <- c('The quick brown fox jumps over the lazy dog')
txt2 <- c('Te quick foks jump ovar lazey dogg')
words <- strsplit(as.character(txt1), " ")
words.p <- strsplit(as.character(txt2), " ")
r <- length(words[[1]])
c <- length(words.p[[1]])
m <- matrix(nrow=r, ncol=c)
for (i in 1:r){
for (j in 1:c){
m[i,j] = stringdist(tolower(words.p[[1]][j]), tolower(words[[1]][i]), method='jaccard', q=2)
}
}
ind <- which(m == min(m))-nrow(m)
words[[1]][ind]
Please help me to improve and beautify this code for large data frame.