У меня есть некоторые текстовые данные в формате, аналогичном приведенному ниже.
Я пытаюсь сгруппировать по ID
и years
, а затем вычислить сходное сходство между текстом и посмотреть, насколькодокумент изменился с t-1
на t
.
Так что для ID1
возьмите год 2002
и сравните его с документом в 2001
.Затем возьмите 2001
и сравните его с документом в 2000
.
Text ID years
Text 1 ID1 2000
Text 2 ID1 2001
Text 3 ID1 2002
Text 4 ID2 2000
Text 5 ID2 2001
...
В данный момент я просто сравниваю документы за определенный год, но я также хотел бы сравнить их спредыдущий год.
library(tidytext)
library(textTinyR)
library(tm)
library(dplyr)
df <- data %>%
filter(years == "2000") %>%
select(ID, text) %>%
setNames(c("doc_id", "text"))
docs = VCorpus(DataframeSource(df))
dtm <- DocumentTermMatrix(docs)
dtm <- DocumentTermMatrix(docs, control = list(weighting = function(x)
weightTfIdf(x, normalize = FALSE)))
inspect(dtm)
findFreqTerms(dtm, 2)
m <- as.matrix(dtm)
library(Matrix)
m <- Matrix(m, sparse = TRUE)
d <- dist(m)
plot(d)
############
my.df <- as.data.frame(inspect(dtm))
my.df.scale <- scale(my.df)
d <- dist(my.df.scale,method="euclidean")
plot(d)
Данные:
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 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.",
"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 team sport in which two teams, most commonly of five players each, 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.",
"Hockey is a sport in which two teams play against each other by trying to manoeuvre a ball or a puck into the opponent's goal using a hockey stick. There are many types of hockey such as bandy, field hockey, and ice hockey. In most of the world, hockey refers to field hockey, while in Canada, the United States, Finland, Sweden, Latvia, the Czech Republic and Slovakia, hockey usually refers to ice hockey.[1]")
ID <- c("ID1", "ID1", "ID1", "ID2", "ID2", "ID2", "ID3", "ID3", "ID3")
years <- c("2000", "2001", "2002", "2000", "2001", "2002", "2000", "2001", "2002")
data <- data.frame(text, ID, years)
РЕДАКТИРОВАТЬ:
Это то, что у меня сейчас (слегка обновленные данные)…
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 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.",
"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 team sport in which two teams, most commonly of five players each, 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.")
ID <- c("ID1", "ID1", "ID1", "ID1", "ID1", "ID3", "ID3", "ID3", "ID3")
years <- c("2000", "2001", "2002", "2003", "2004", "2000", "2001", "2002", "2003")
data <- data.frame(text, ID, years)
data2 <- data %>%
mutate(doc_id=paste(ID, years, sep="_")) %>% #combines id and year
group_split(ID) #splits df
data2 <- lapply(data2, function(x) x[c(-2,-3)])#removes extra cols
data2 <- lapply(data2, as.data.frame)#changes dfs in list to dataframes
data3 <- plyr::ldply(data2)
docs = VCorpus(DataframeSource(data3))
dtm <- DocumentTermMatrix(docs)
dtm <- DocumentTermMatrix(docs, control = list(weighting = function(x)
weightTfIdf(x, normalize = FALSE)))
inspect(dtm)
dist(dtm)
Вывод:
ID1_2000 ID1_2001 ID1_2002 ID1_2003 ID1_2004 ID3_2000 ID3_2001 ID3_2002
ID1_2001 5.247895
ID1_2002 24.106356 22.578237
ID1_2003 21.983718 20.296410 9.234505
ID1_2004 39.816574 39.430571 44.036195 42.911001
ID3_2000 28.303690 27.093798 28.283461 26.546848 46.153218
ID3_2001 34.775122 33.755114 34.161337 32.969805 50.375486 37.634369
ID3_2002 34.972794 33.937542 34.864501 33.546066 50.386962 37.526433 42.609499
ID3_2003 0.000000 5.247895 24.106356 21.983718 39.816574 28.303690 34.775122 34.972794
Я думаю, что это приближается к тому, что я хочу, здесь, хотя мне не очень интересно вычислять расстояние между ID3
, 2003
и ID1
, 2000
.Поэтому я думаю, что я просто хочу сохранить диагональные элементы ...
РЕДАКТИРОВАТЬ 2:
Я также думаю, что теперь tf-idf
неправильно и смещено ... Я должен вычислить tf-idf
для 2000
с использованием двух текстов…
Football is a family of team sports that involve,...
Badminton is a racquet sport played using racquets...
Принимая во внимание, что для расчета используется весь текст за все годы.