# Cosine angle calculation for the documents - Dissimilarity function not working in tm package in R

I want to find the document similarity.

I am using the below R code

# some preprocessing

corpus <- tm_map(corpus, removeWords, stopwords("english")) corpus <- tm_map(corpus, stripWhitespace) corpus <- tm_map(corpus, stemDocument, language="english")

# creating term matrix with TF-IDF weighting

terms <-DocumentTermMatrix(corpus,control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE)))

# or compute cosine distance among documents

dissimilarity(tdm, method = "cosine")

It seems dissimilarity function is not supported by the tm package any more. Is there a way to find the cosine angle between the documents. (Rows are documents, columns are texts. So i want to find the similarity between the rows using cosine angle).

You can try using an alternative package, quanteda, that has a function called similarity(). It's still a bit rough around the edges but works as advertised.

require(quanteda)
# clean the texts and create a document-feature matrix
myDfm <- dfm(inaugCorpus, verbose = FALSE)
# similarity matrix for cosine
similarity(myDfm, docnames(myDfm), margin = "documents", method = "cosine")


The last command produces a list of numeric vectors of cosine similarity where each list element corresponds to a document (supplied by docnames(myDfm)). If you wanted to focus on just a single document, you can name just one.

myCorpus <- corpus(textfile("data/txt/*txt"))

• It does not by default, but you can use weight(myDfm, "tfidf") to change the dfm object to tfodf weights, and then compute the cosine distances on that. – Ken Benoit Nov 5 '15 at 15:44
• @KenBenoit what is inaugCorpus data type in your code, I tried to put my Corpus object but gives me error saying  Error in UseMethod("dfm") : no applicable method for 'dfm' applied to an object of class "c('matrix', 'list')" – alily Oct 28 '16 at 10:41