2014-12-07 21:06:59 +01:00
|
|
|
require(lubridate)
|
2014-12-07 22:51:17 +01:00
|
|
|
require(XML)
|
|
|
|
|
require(ggplot2)
|
|
|
|
|
require(reshape2)
|
2015-01-12 14:12:25 +01:00
|
|
|
require(stringr)
|
2015-02-22 03:05:56 +01:00
|
|
|
library(foreach)
|
|
|
|
|
library(doParallel)
|
2014-12-07 21:06:59 +01:00
|
|
|
|
2015-01-12 23:52:24 +01:00
|
|
|
source("issuecomp-functions.R")
|
|
|
|
|
|
2015-01-15 20:27:19 +01:00
|
|
|
|
2015-01-15 20:24:40 +01:00
|
|
|
load(file = "tweets_untagged.RData")
|
|
|
|
|
|
2014-12-07 21:06:59 +01:00
|
|
|
# Create date range
|
|
|
|
|
date_start <- as.Date("2014-01-01")
|
2015-01-12 12:48:10 +01:00
|
|
|
date_end <- as.Date("2014-12-31")
|
2014-12-07 21:06:59 +01:00
|
|
|
drange <- as.integer(date_end - date_start)
|
2014-12-07 22:51:17 +01:00
|
|
|
drange <- date_start + days(0:drange)
|
2014-12-07 21:06:59 +01:00
|
|
|
|
2015-01-12 15:36:14 +01:00
|
|
|
|
|
|
|
|
# MATCH TWEETS ------------------------------------------------------------
|
|
|
|
|
|
2015-01-12 23:52:24 +01:00
|
|
|
id_folder <- "matched-ids"
|
|
|
|
|
unlink(id_folder, recursive = TRUE)
|
|
|
|
|
dir.create(id_folder)
|
|
|
|
|
|
2015-01-12 15:36:14 +01:00
|
|
|
issues <- data.frame(date = drange)
|
2015-02-22 01:43:06 +01:00
|
|
|
issuelist <- readLines("issues.xml")
|
|
|
|
|
issuelist <- str_replace_all(string = issuelist, pattern = ".*<!-- .+ -->", "")
|
|
|
|
|
issuelist <- xmlToList(issuelist)
|
2014-12-07 21:06:59 +01:00
|
|
|
issueheads <- names(issuelist)
|
|
|
|
|
issues[issueheads] <- 0
|
2015-01-15 20:24:40 +01:00
|
|
|
tweets$issue <- ""
|
|
|
|
|
tweets$tags <- ""
|
2014-12-07 21:06:59 +01:00
|
|
|
|
2015-02-22 15:10:54 +01:00
|
|
|
tagexpand <- c("", "s", "n", "en", "er", "e")
|
2015-01-21 12:27:09 +01:00
|
|
|
|
2015-02-22 02:50:01 +01:00
|
|
|
# Parallelisation
|
2015-02-22 03:05:56 +01:00
|
|
|
writeLines(c(""), "issuecomp-analysis.log")
|
2015-02-22 15:10:54 +01:00
|
|
|
cl<-makeCluster(4)
|
2015-02-22 02:50:01 +01:00
|
|
|
registerDoParallel(cl)
|
|
|
|
|
|
2015-02-22 15:10:54 +01:00
|
|
|
df<-foreach(d = 1:nrow(issues), .packages = c("stringr"), .combine=rbind) %dopar% {
|
2015-02-22 02:50:01 +01:00
|
|
|
#for(d in 1:nrow(issues)) {
|
2015-01-12 14:12:25 +01:00
|
|
|
# Go through every day
|
2014-12-07 21:06:59 +01:00
|
|
|
curdate <- issues$date[d]
|
2015-02-22 03:05:56 +01:00
|
|
|
cat(paste(as.character(curdate),"\n"), file="issuecomp-analysis.log", append=TRUE)
|
2015-01-12 14:12:25 +01:00
|
|
|
|
2014-12-07 21:06:59 +01:00
|
|
|
# Put all tweets from specific day in a temporary DF
|
|
|
|
|
tweets_curday <- tweets[tweets[, "created_at"] == curdate, ]
|
|
|
|
|
|
2015-02-22 15:10:54 +01:00
|
|
|
for(t in 1:nrow(tweets_curday)){
|
2015-02-22 03:05:56 +01:00
|
|
|
cat(paste("Starting tweet", t, "of",as.character(curdate),"\n"), file="issuecomp-analysis.log", append=TRUE)
|
2014-12-07 21:06:59 +01:00
|
|
|
# Select tweet's text, make it lowercase and remove hashtag indicators (#)
|
2015-01-12 14:59:51 +01:00
|
|
|
curtext <- as.character(tweets_curday$text[t])
|
2014-12-07 21:06:59 +01:00
|
|
|
curtext <- str_replace_all(curtext, "#", "")
|
2015-01-15 20:24:40 +01:00
|
|
|
|
2015-01-12 23:52:24 +01:00
|
|
|
curid <- as.character(tweets_curday$id_str[t])
|
2014-12-07 21:06:59 +01:00
|
|
|
|
2015-01-12 15:36:14 +01:00
|
|
|
# Now test each single issue (not tag!)
|
2015-01-18 22:59:59 +01:00
|
|
|
for(i in 1:length(issueheads)) {
|
|
|
|
|
curissue <- issueheads[i]
|
|
|
|
|
curtags <- as.character(issuelist[[curissue]])
|
2015-01-12 23:52:24 +01:00
|
|
|
curfile <- str_c(id_folder,"/",curissue,".csv")
|
2015-01-12 15:36:14 +01:00
|
|
|
|
|
|
|
|
# Now test all tags of a single issue
|
2015-01-12 23:52:24 +01:00
|
|
|
for(s in 1:length(curtags)) {
|
|
|
|
|
curtag <- curtags[s]
|
|
|
|
|
curchars <- nchar(curtag, type = "chars")
|
2015-01-12 14:59:51 +01:00
|
|
|
|
2015-01-12 23:52:24 +01:00
|
|
|
# Check if tag is an acronym. If so, ignore.case will be deactivated in smartPatternMatch
|
|
|
|
|
if(curchars <= 4) {
|
|
|
|
|
curacro <- checkAcronym(string = curtag, chars = curchars)
|
|
|
|
|
} else {
|
|
|
|
|
curacro <- FALSE
|
|
|
|
|
}
|
2015-01-21 12:27:09 +01:00
|
|
|
|
|
|
|
|
# Now expand the current tag by possible suffixes that may be plural forms
|
|
|
|
|
if(!curacro) {
|
|
|
|
|
for(e in 1:length(tagexpand)) {
|
|
|
|
|
curtag[e] <- str_c(curtag[1], tagexpand[e])
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-21 13:17:24 +01:00
|
|
|
|
|
|
|
|
# Set Levenshtein distance depending on char length
|
|
|
|
|
if(curchars <= 4) {
|
|
|
|
|
curdistance <- 0
|
|
|
|
|
} else {
|
|
|
|
|
curdistance <- 1
|
|
|
|
|
}
|
2015-01-12 23:52:24 +01:00
|
|
|
|
2015-01-21 12:27:09 +01:00
|
|
|
# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow also 1 (Levenshtein distance)
|
|
|
|
|
tags_found <- NULL
|
2015-01-21 13:17:24 +01:00
|
|
|
# Match the tweet with each variation of tagexpand
|
2015-01-21 12:27:09 +01:00
|
|
|
for(e in 1:length(curtag)) {
|
2015-01-21 13:17:24 +01:00
|
|
|
tags_found[e] <- smartPatternMatch(curtext, curtag[e], curdistance, curacro)
|
2015-01-21 12:27:09 +01:00
|
|
|
}
|
|
|
|
|
tags_found <- any(tags_found)
|
|
|
|
|
curtag <- curtag[1]
|
|
|
|
|
|
|
|
|
|
if(tags_found == TRUE) {
|
2015-02-22 15:10:54 +01:00
|
|
|
# # Raise number of findings on this day for this issue by 1
|
|
|
|
|
# issues[d,curissue] <- issues[d,curissue] + 1
|
|
|
|
|
#
|
|
|
|
|
# # Add issue and first matched tag of tweet to tweets-DF
|
|
|
|
|
# oldissue <- tweets[tweets[, "id_str"] == curid, "issue"]
|
|
|
|
|
# tweets[tweets[, "id_str"] == curid, "issue"] <- str_c(oldissue, curissue, ";")
|
|
|
|
|
# oldtag <- tweets[tweets[, "id_str"] == curid, "tags"]
|
|
|
|
|
# tweets[tweets[, "id_str"] == curid, "tags"] <- str_c(oldtag, curtag, ";")
|
2015-01-15 20:24:40 +01:00
|
|
|
|
|
|
|
|
# Add information to file for function viewPatternMatching
|
2015-02-22 15:10:54 +01:00
|
|
|
write(str_c(curdate,";\"",curid,"\";",curissue,";",curtag), curfile, append = TRUE)
|
2015-02-22 03:18:51 +01:00
|
|
|
cat(paste("Match!\n"), file="issuecomp-analysis.log", append=TRUE)
|
2015-02-22 15:10:54 +01:00
|
|
|
# data.frame(date=curdate, issue=curissue)
|
|
|
|
|
break # next issue, no more tags from same issue
|
2015-01-12 14:59:51 +01:00
|
|
|
}
|
2015-01-12 15:36:14 +01:00
|
|
|
else {
|
|
|
|
|
#cat("Nothing found\n")
|
|
|
|
|
}
|
|
|
|
|
} # /for curtags
|
2014-12-07 21:06:59 +01:00
|
|
|
} # /for issuelist
|
|
|
|
|
} # /for tweets_curday
|
|
|
|
|
} # /for drange
|
|
|
|
|
|
2015-01-18 19:38:14 +01:00
|
|
|
#rm(tweets_curday,curacro, curchars, curdate,curfile,curid,curissue,curtag,curtags,curtext,d,date_end,date_start,i,id_folder,oldissue,oldtag,s,t,tags_found)
|
2015-02-22 03:05:56 +01:00
|
|
|
stopCluster(cl)
|
2014-12-07 21:06:59 +01:00
|
|
|
|
2015-01-15 20:24:40 +01:00
|
|
|
# SAVING ------------------------------------------------------------------
|
2015-01-12 15:36:14 +01:00
|
|
|
|
2014-12-07 22:51:17 +01:00
|
|
|
|
2015-01-15 20:24:40 +01:00
|
|
|
row.names(tweets) <- NULL
|
|
|
|
|
write.csv(tweets, "tweets.csv")
|
|
|
|
|
save(tweets, file="tweets.RData")
|
|
|
|
|
|
2015-01-18 19:38:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# SOME TESTS --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
stats <- data.frame(date=drange)
|
|
|
|
|
stats$tpd <- 0
|
|
|
|
|
|
|
|
|
|
# Total number of tweets per day over time
|
|
|
|
|
for(r in 1:length(drange)) {
|
|
|
|
|
stats$tpd[r] <- length(tweets[tweets[, "created_at"] == drange[r], "id_str"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats_melt <- melt(stats, id="date")
|
|
|
|
|
g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour=variable, group=variable)) +
|
|
|
|
|
geom_line() +
|
|
|
|
|
geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color=1)
|
|
|
|
|
g1
|
|
|
|
|
|
|
|
|
|
rm(g1, r)
|
|
|
|
|
|
2015-01-21 12:27:09 +01:00
|
|
|
|
|
|
|
|
# Show party percentage of twitter users
|
|
|
|
|
acc_parties <- data.frame(party = c("cducsu", "spd", "linke", "gruene"))
|
|
|
|
|
acc_parties$btw13 <- c(49.3, 30.6, 10.1, 10.0) # seats of party / 631 seats
|
|
|
|
|
acc_parties$twitter <- 0
|
|
|
|
|
for(p in 1:nrow(acc_parties)) {
|
|
|
|
|
acc_parties$twitter[p] <- round(nrow(acc_df[acc_df$party == as.character(acc_parties$party[p]), ]) / 280 * 100)
|
|
|
|
|
}
|
|
|
|
|
pie(acc_parties$btw13, col=c("black", "red", "purple", "green"), labels = c("CDU/CSU", "SPD", "Die LINKE", "Bündnis 90/Grüne"), clockwise = T,
|
|
|
|
|
main = "Seats of parties in the parliament")
|
|
|
|
|
pie(acc_parties$twitter, col=c("black", "red", "purple", "green"), labels = c("CDU/CSU", "SPD", "Die LINKE", "Bündnis 90/Grüne"), clockwise = T,
|
|
|
|
|
main = "Percentage of parties' MdBs of all Twitter accounts")
|
|
|
|
|
|
|
|
|
|
rm(acc_parties, p)
|
|
|
|
|
|
|
|
|
|
|
2014-12-07 21:44:03 +01:00
|
|
|
# VISUALS -----------------------------------------------------------------
|
|
|
|
|
|
2015-01-15 20:24:40 +01:00
|
|
|
|
2014-12-07 22:51:17 +01:00
|
|
|
# Level: days
|
|
|
|
|
issues_melt <- melt(issues,id="date")
|
2015-01-10 01:50:01 +01:00
|
|
|
ggplot(issues_melt,aes(x=date,y=value,colour=variable,group=variable)) + geom_line(size=1)
|
|
|
|
|
ggplot(issues_melt,aes(x=date,y=value,colour=variable,group=variable)) + geom_smooth(size=1,method="loess",formula = y ~ x, se=FALSE)
|
2014-12-07 22:51:17 +01:00
|
|
|
|
|
|
|
|
|
2014-12-07 21:06:59 +01:00
|
|
|
|
|
|
|
|
# POSSIBLY USEFUL CODE ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
# Limits of list
|
|
|
|
|
length(issuelist)
|
|
|
|
|
length(issuelist[[2]])
|
|
|
|
|
|
|
|
|
|
# Select all tweets from current day in drange
|
|
|
|
|
tweets_curday <- tweets[tweets[, "created_at"] == drange[5], ]
|
|
|
|
|
# Is column a issue counting column?
|
|
|
|
|
str_detect(names(issues[2]), "^issue")
|