user8959427
user8959427

Reputation: 2067

cosine similarity between 2 document term matrix

This is a follow up question to a previously asked one here. This post is a little more clear than my original.

I have created two document term matrices (one for 2000 and the other for 2001).

          Terms
Docs       activity badminton court football per played racquet rugby shuttlecock side
  ID1_2000        0         0     0       12   0      0       0     3           0    0
  ID2_2000        1         2     2        0   2      3       1     0           1    2
  ID3_2000        1         2     2        0   2      4       2     0           2    2

and

          Terms
Docs       ball football game player players rugby side teams the two
  ID1_2001    1       10    0      0       0     3    0     0   0   0
  ID2_2001    3        0    2      1       2     0    3     2   2   4
  ID3_2001    3        0    1      3       1     0    0     1   2   2

I have been following this post which seems to have the same problem as mine, however I cannot seem to get the solution working with my data. I get the following error:

cosine_sim <- tcrossprod_simple_triplet_matrix(dtm_t, dtm_Lt)/sqrt(row_sums(dtm_t^2) %*% t(row_sums(dtm_Lt^2))) Error in .tcrossprod_simple_triplet_matrix(x, y) : the number of columns of 'x' and 'y' do not conform

What I am trying to do is calculate the cosine similarity between the two document term matrices. This example Works but I cannot get it working with my data.

library(slam)
library(tm)
data("acq")
data("crude")

dtm <- DocumentTermMatrix(c(acq, crude))

index <- sample(1:70, size = 10)

dtm1 <- dtm[index, ]
dtm2 <- dtm[-index, ]

cosine_sim <- tcrossprod_simple_triplet_matrix(dtm1, dtm2)/sqrt(row_sums(dtm1^2) %*% t(row_sums(dtm2^2)))
cosine_sim

Data / Code

text <- c("Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. Unqualified, the word football is understood to refer to whichever form of football is the most popular in the regional context in which the word appears. Sports commonly called football in certain places include association football (known as soccer in some countries); gridiron football (specifically American football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); and Gaelic football.[1][2] These different variations of football are known as football codes.",
          "Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. Unqualified, the word football is understood to refer to whichever form of football is the most popular in the regional context in which the word appears. Sports commonly called football in certain places include association football (known as soccer in some countries); gridiron football (specifically American football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); and Gaelic football.[1][2]",
          "Tennis is a racket sport that can be played individually against a single opponent (singles) or between two teams of two players each (doubles). Each player uses a tennis racket that is strung with cord to strike a hollow rubber ball covered with felt over or around a net and into the opponent's court. The object of the game is to maneuver the ball in such a way that the opponent is not able to play a valid return. The player who is unable to return the ball will not gain a point, while the opposite player will.",
          "Tennis is a racket sport that can be played individually against a single opponent (singles) or between two teams of two players each (doubles). Each player uses a tennis racket that is strung with cord to strike a hollow rubber ball covered with felt over or around a net and into the opponent's court. The object of the game is to maneuver the ball in such a way that the opponent is not able to play a valid return.",
          "Rugby refers to the team sports rugby league and rugby union. Legend claims that rugby football was started about 1845 in Rugby School, Rugby, Warwickshire, England, although forms of football in which the ball was carried and tossed date to medieval times. Rugby eventually split into two sports in 1895 when twenty-one clubs split from the original Rugby Football Union, to form the Northern Union (later to be named rugby league in 1922) in the George Hotel, Huddersfield, Northern England over the issue of payment to players, thus making rugby league the first code to turn professional and pay its players, rugby union turned fully professional in 1995. Both sports are run by their respective world governing bodies World Rugby (rugby union) and the Rugby League International Federation (rugby league). Rugby football was one of many versions of football played at English public schools in the 19th century.[1][2] Although rugby league initially used rugby union rules, they are now wholly separate sports. In addition to these two codes, both American and Canadian football evolved from rugby football.",
          "Badminton is. Although it may be played with larger teams, the most common forms of the game are singles (with one player per side) and doubles (with two players per side). Badminton is often played as a casual outdoor activity in a yard or on a beach; formal games are played on a rectangular indoor court. Points are scored by striking the shuttlecock with the racquet and landing it within the opposing side's half of the court.",
          "Cricket is a bat-and-ball game played between two teams of eleven players on a field at the centre of which is a 20-metre (22-yard) pitch with a wicket at each end, each comprising two bails balanced on three stumps. The batting side scores runs by striking the ball bowled at the wicket with the bat, while the bowling and fielding side tries to prevent this and dismiss each player (so they are out). Means of dismissal include being bowled, when the ball hits the stumps and dislodges the bails, and by the fielding side catching the ball after it is hit by the bat, but before it hits the ground. When ten players have been dismissed, the innings ends and the teams swap roles. The game is adjudicated by two umpires, aided by a third umpire and match referee in international matches. They communicate with two off-field scorers who record the match's statistical information.",
          "Basketball is a opposing one another on a rectangular court, compete with the primary objective of shooting a basketball (approximately 9.4 inches (24 cm) in diameter) through the defender's hoop (a basket 18 inches (46 cm) in diameter mounted 10 feet (3.048 m) high to a backboard at each end of the court) while preventing the opposing team from shooting through their own hoop. A field goal is worth two points, unless made from behind the three-point line, when it is worth three. After a foul, timed play stops and the player fouled or designated to shoot a technical foul is given one or more one-point free throws. The team with the most points at the end of the game wins, but if regulation play expires with the score tied, an additional period of play (overtime) is mandated.",
          "Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. Unqualified, the word football is understood to refer to whichever form of football is the most popular in the regional context in which the word appears. Sports commonly called football in certain places include association football (known as soccer in some countries); gridiron football (specifically American football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); and Gaelic football.[1][2] These different variations of football are known as football codes.",
          "Badminton is a racquet sport played using racquets to hit a shuttlecock across a net. Although it may be played with larger teams, the most common forms of the game are singles (with one player per side) and doubles (with two players per side). Badminton is often played as a casual outdoor activity in a yard or on a beach; formal games are played on a rectangular indoor court. Points are scored by striking the shuttlecock with the racquet and landing it within the opposing side's half of the court.",
          "Tennis is a racket sport that can be played individually against a single opponent (singles) or between two teams of two players each (doubles). Each player uses a tennis racket that is strung with cord to strike a hollow rubber ball covered with felt over or around a net and into the opponent's court. The object of the game is to maneuver the ball in such a way that the opponent is not able to play a valid return. The player who is unable to return the ball will not gain a point, while the opposite player will.",
          "Rugby refers to the team sports rugby league and rugby union. Legend claims that rugby football was started about 1845 in Rugby School, Rugby, Warwickshire, England, although forms of football in which the ball was carried and tossed date to medieval times. Rugby eventually split into two sports in 1895 when twenty-one clubs split from the original Rugby Football Union, to form the Northern Union (later to be named rugby league in 1922) in the George Hotel, Huddersfield, Northern England over the issue of payment to players, thus making rugby league the first code to turn professional and pay its players, rugby union turned fully professional in 1995. Both sports are run by their respective world governing bodies World Rugby (rugby union) and the Rugby League International Federation (rugby league). Rugby football was one of many versions of football played at English public schools in the 19th century.[1][2] Although rugby league initially used rugby union rules, they are now wholly separate sports. In addition to these two codes, both American and Canadian football evolved from rugby football.",
          "Cricket is a bat-and-ball game played between two teams of eleven players on a field at the centre of which is a 20-metre (22-yard) pitch with a wicket at each end, each comprising two bails balanced on three stumps. The batting side scores runs by striking the ball bowled at the wicket with the bat, while the bowling and fielding side tries to prevent this and dismiss each player (so they are out). Means of dismissal include being bowled, when the ball hits the stumps and dislodges the bails, and by the fielding side catching the ball after it is hit by the bat, but before it hits the ground. When ten players have been dismissed, the innings ends and the teams swap roles. The game is adjudicated by two umpires, aided by a third umpire and match referee in international matches. They communicate with two off-field scorers who record the match's statistical information.")


ID <- c("ID1", "ID1", "ID1", "ID1", "ID1", "ID2", "ID2", "ID2", "ID2",  "ID3", "ID3", "ID3", "ID3")

years <- c("2000", "2001", "2002", "2003", "2004", "2000", "2001", "2002", "2003", "2000", "2001", "2002", "2003")

data <- data.frame(text, ID, years)




docs_df_Lt <- data %>%                              # Where Lt represents "lagged_t"
  filter(years %in% c("2000")) %>%
  mutate(doc_id = paste(ID, years, sep = "_")) %>%
  select(doc_id, text) %>%
  setNames(c("doc_id", "text")) %>%
  distinct(tolower(text), .keep_all = TRUE)

docs_df_t <- data %>%
  filter(years %in% c("2001")) %>%
  mutate(doc_id = paste(ID, years, sep = "_")) %>%
  select(doc_id, text) %>%
  setNames(c("doc_id", "text")) %>%
  distinct(tolower(text), .keep_all = TRUE)

docs_t = VCorpus(DataframeSource(docs_df_t))
docs_Lt = VCorpus(DataframeSource(docs_df_Lt))

# Soem document cleaning
docs_t <- tm_map(docs_t, removePunctuation)
docs_t <- tm_map(docs_t, removeWords, stopwords('english'))

docs_Lt <- tm_map(docs_Lt, removePunctuation)
docs_Lt <- tm_map(docs_Lt, removeWords, stopwords('english'))


dtm_t <- DocumentTermMatrix(docs_t)
dtm_Lt <- DocumentTermMatrix(docs_Lt)

dtm_t
dtm_Lt

inspect(dtm_t)
inspect(dtm_Lt)

Edit:

This gets me a little closer to what I am trying to achieve.

dtm <- DocumentTermMatrix(c(docs_t, docs_Lt))

m <- as.matrix(dtm)
dist.matrix = proxy::dist(m, method = "cosine") 
dist.matrix

Output:

           ID1_2001   ID2_2001   ID3_2001   ID1_2000   ID2_2000
ID2_2001 0.97192896                                            
ID3_2001 0.97288923 0.69527190                                 
ID1_2000 0.01505221 0.97565046 0.97648342                      
ID2_2000 1.00000000 0.75908178 0.77840308 1.00000000           
ID3_2000 1.00000000 0.77099402 0.76921180 1.00000000 0.05728332

The problem here is that I do not need/want all of the information… i.e. I am not interested in cosine of ID1_2001 and ID2_2001 (cell 1). I am only interested in the following;

I do plan on having many more years in the data and more IDs so seems quite computationally expensive also to calculate the cosine of all of the documents when I am not interested in 90% of them.

Upvotes: 0

Views: 685

Answers (1)

AkselA
AkselA

Reputation: 8837

Disregarding all the tm stuff, as it seems to be besides the point, proxy::dist() has the argument pairwise which lets you do what you want.

set.seed(1)
N <- 6*8
m <- matrix(sample(c(0, 1, 1), N, rep=TRUE)*rpois(N, 6), 6)
dimnames(m) <- list(c(paste0("ID", 1:3, "_2000"), paste0("ID", 1:3, "_2001")),
                     sample(LETTERS, ncol(m)))

library(proxy)
proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine")
# 0.6160563 0.2746764 0.2038266

# Which is the same as
diag(proxy::dist(m[1:3,], m[4:6,], method="cosine"))
# 0.6160563 0.2746764 0.2038266

Upvotes: 1

Related Questions