我有一些文本数据,其格式与以下数据类似。
我试图按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
的{{1}}……
2000
Football is a family of team sports that involve,...
目前,它将所有年份的所有文本都用于计算。
答案 0 :(得分:1)
不确定有帮助或理解正确,但可以按ID拆分数据并按年份过滤,将正文包装在函数中并将列表传递给函数。大概是这样的:
data2<-data %>% filter(years != "2000") %>% #or change to 2002
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
gofun<-function(df){
docs = VCorpus(DataframeSource(df))
dtm <- DocumentTermMatrix(docs)
......
}
编辑:
我*想一下,如果您想对角线,必须先将文档分开,然后制作Vcorpus。我找到了此子序列函数here。
data <- data.frame(text, ID, years)
#creates subsequence --(still learning R so I think there is a different way to do this)
get_seq=function(df,n){
res=c()
for(i in 1:(nrow(df)-n+1)){
res=rbind(res,cbind(df[i:(i+n-1),],"seq_label"=i))
}
res
}
data<-data %>%
group_by(ID) %>%
do(data.frame(mod=get_seq(.,2)))%>% ungroup() %>%
select(ID, text=mod.text, years=mod.years, seq=mod.seq_label)
data2 <- data %>%
mutate(doc_id=paste(ID, years, sep="_")) %>% #combines id and year
group_split(ID,seq) #splits
data2 <- lapply(data2, function(x) x[c(-1,-3,-4)])#removes extra cols
data2 <- lapply(data2, as.data.frame)#changes dfs in list to dataframes
gofun<-function(df){
docs = VCorpus(DataframeSource(df))
dtm <- DocumentTermMatrix(docs)
dtm <- DocumentTermMatrix(docs, control = list(weighting = function(x)
weightTfIdf(x, normalize = FALSE)))
inspect(dtm)
dist(dtm)
}
lapply(data2, gofun)
[[1]]
ID1_2000
ID1_2001 2.44949
[[2]]
ID1_2001
ID1_2002 14.24781
[[3]]
ID1_2002
ID1_2003 3
[[4]]
ID1_2003
ID1_2004 22.69361
[[5]]
ID3_2000
ID3_2001 13.37909
[[6]]
ID3_2001
ID3_2002 15.16575
[[7]]
ID3_2002
ID3_2003 17.08801