diff --git a/.Rhistory b/.Rhistory index 1d3e03f..7d95a31 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,389 +1,3 @@ -source("issuecomp-codingsample-function2.R") -} -if(c_errcode == "1") { -#cat("Which issue is incorrect?\n") -repeat { -c_tag <- readYN("Which issue is incorrect?: ") -c_tag <- unlist(str_split(c_tag, ";")) -for(i in 1:length(c_tag)) { -if(checkIssue(c_tag[i], c_issueheads)) {status[i] <- TRUE} else {cat("Issue",c_tag[i],"does not exist. Please try again.\n")} -} -if(all(status)) { -break -} -} -if(c_errcode == "1") { -#cat("Which issue is incorrect?\n") -repeat { -c_tag <- readYN("Which issue is incorrect?: ") -c_tag <- unlist(str_split(c_tag, ";")) -for(i in 1:length(c_tag)) { -if(checkIssue(c_tag[i], c_issueheads)) {status[i] <- TRUE} else {cat("Issue",c_tag[i],"does not exist. Please try again.\n")} -} -if(all(status)) { -break -} -} -} -wdq -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -source("issuecomp-codingsample-function2.R") -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -checkAllIssues <- function(string, issuelist) { -string <- unlist(str_split(string, ";")) -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -} -test -checkAllIssues <- function(string, issuelist) { -string <- unlist(str_split(string, ";")) -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -return(status) -} -test <- "issue.edathy" -checkAllIssues(test, c_issueheads) -test <- "issue.edathy" -checkAllIssues(test, c_issueheads) -rm(status) -checkAllIssues(test, c_issueheads) -checkAllIssues <- function(string, issuelist) { -status <- NULL -string <- unlist(str_split(string, ";")) -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -return(status) -} -checkAllIssues(test, c_issueheads) -checkAllIssues("wdjqaowd", c_issueheads) -test <- checkAllIssues("wdjqaowd", c_issueheads) -test -test <- checkAllIssues("wdjqaow;wiqud", c_issueheads) -test -test <- checkAllIssues("wdjqaow;issue.edathy", c_issueheads) -test -checkAllIssues <- function(string, issuelist) { -status <- NULL -string <- unlist(str_split(string, ";")) -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -return(status) -} -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -checkAllIssues <- function(string, issuelist) { -status <- NULL -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -return(status) -} -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -for(r in 1:nrow(c_errors)) { -c_errcode <- as.character(c_errors$code[r]) -c_errtags <- as.character(c_errors$tags[r]) -c_errtext <- as.character(c_errors$text[r]) -cat("===============\n\n[TWEET]: ",c_errtext,"\n[ISSUES]: ", c_errtags, "\n", sep="") -source("issuecomp-codingsample-function2.R") -} -View(c_issues) -View(tweets) -tweets$tagged <- NULL -View(c_tweets) -View(tweets) -# MATCH TWEETS ------------------------------------------------------------ -id_folder <- "matched-ids" -unlink(id_folder, recursive = TRUE) -dir.create(id_folder) -issues <- data.frame(date = drange) -issuelist <- xmlToList("issues.xml") -issueheads <- names(issuelist) -issues[issueheads] <- 0 -tweets$issue <- "" -tweets$tags <- "" -for(d in 1:nrow(issues)) { -# Go through every day -curdate <- issues$date[d] -cat(as.character(curdate),"\n") -# Put all tweets from specific day in a temporary DF -tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] -for(t in 1:nrow(tweets_curday)){ -# Select tweet's text, make it lowercase and remove hashtag indicators (#) -curtext <- as.character(tweets_curday$text[t]) -curtext <- str_replace_all(curtext, "#", "") -curtext <- str_replace_all(curtext, "$", " ") -curtext <- str_replace_all(curtext, "http://.+?\\s", "URL ") -curid <- as.character(tweets_curday$id_str[t]) -# Now test each single issue (not tag!) -for(i in 1:length(issuelist)) { -curtags <- as.character(issuelist[[i]]) -curissue <- names(issuelist)[i] -curfile <- str_c(id_folder,"/",curissue,".csv") -# Now test all tags of a single issue -for(s in 1:length(curtags)) { -curtag <- curtags[s] -curchars <- nchar(curtag, type = "chars") -# 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 -} -# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance) -tags_found <- smartPatternMatch(curtext, curtag, curchars, curacro) -if(tags_found == 1) { -# 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, ";") -# Add information to file for function viewPatternMatching -write(str_c(curdate,";\"",curid,"\";",curtag), curfile, append = TRUE) -break -} -else { -#cat("Nothing found\n") -} -} # /for curtags -} # /for issuelist -} # /for tweets_curday -} # /for drange -date_start <- as.Date("2014-01-01") -date_end <- as.Date("2014-12-31") -drange <- as.integer(date_end - date_start) -drange <- date_start + days(0:drange) -# MATCH TWEETS ------------------------------------------------------------ -id_folder <- "matched-ids" -unlink(id_folder, recursive = TRUE) -dir.create(id_folder) -issues <- data.frame(date = drange) -issuelist <- xmlToList("issues.xml") -issueheads <- names(issuelist) -issues[issueheads] <- 0 -tweets$issue <- "" -tweets$tags <- "" -for(d in 1:nrow(issues)) { -# Go through every day -curdate <- issues$date[d] -cat(as.character(curdate),"\n") -# Put all tweets from specific day in a temporary DF -tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] -for(t in 1:nrow(tweets_curday)){ -# Select tweet's text, make it lowercase and remove hashtag indicators (#) -curtext <- as.character(tweets_curday$text[t]) -curtext <- str_replace_all(curtext, "#", "") -curtext <- str_replace_all(curtext, "$", " ") -curtext <- str_replace_all(curtext, "http://.+?\\s", "URL ") -curid <- as.character(tweets_curday$id_str[t]) -# Now test each single issue (not tag!) -for(i in 1:length(issuelist)) { -curtags <- as.character(issuelist[[i]]) -curissue <- names(issuelist)[i] -curfile <- str_c(id_folder,"/",curissue,".csv") -# Now test all tags of a single issue -for(s in 1:length(curtags)) { -curtag <- curtags[s] -curchars <- nchar(curtag, type = "chars") -# 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 -} -# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance) -tags_found <- smartPatternMatch(curtext, curtag, curchars, curacro) -if(tags_found == 1) { -# 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, ";") -# Add information to file for function viewPatternMatching -write(str_c(curdate,";\"",curid,"\";",curtag), curfile, append = TRUE) -break -} -else { -#cat("Nothing found\n") -} -} # /for curtags -} # /for issuelist -} # /for tweets_curday -} # /for drange -View(tweets) -View(tweets) -# MATCH TWEETS ------------------------------------------------------------ -id_folder <- "matched-ids" -unlink(id_folder, recursive = TRUE) -dir.create(id_folder) -issues <- data.frame(date = drange) -issuelist <- xmlToList("issues.xml") -issueheads <- names(issuelist) -issues[issueheads] <- 0 -tweets$issue <- "" -tweets$tags <- "" -for(d in 1:nrow(issues)) { -# Go through every day -curdate <- issues$date[d] -cat(as.character(curdate),"\n") -# Put all tweets from specific day in a temporary DF -tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] -for(t in 1:nrow(tweets_curday)){ -# Select tweet's text, make it lowercase and remove hashtag indicators (#) -curtext <- as.character(tweets_curday$text[t]) -curtext <- str_replace_all(curtext, "#", "") -curtext <- str_replace_all(curtext, "$", " ") -curtext <- str_replace_all(curtext, "http://.+?\\s", "URL ") -curid <- as.character(tweets_curday$id_str[t]) -# Now test each single issue (not tag!) -for(i in 1:length(issuelist)) { -curtags <- as.character(issuelist[[i]]) -curissue <- names(issuelist)[i] -curfile <- str_c(id_folder,"/",curissue,".csv") -# Now test all tags of a single issue -for(s in 1:length(curtags)) { -curtag <- curtags[s] -curchars <- nchar(curtag, type = "chars") -# 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 -} -# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance) -tags_found <- smartPatternMatch(curtext, curtag, curchars, curacro) -if(tags_found == 1) { -# 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, ";") -# Add information to file for function viewPatternMatching -write(str_c(curdate,";\"",curid,"\";",curtag), curfile, append = TRUE) -break -} -else { -#cat("Nothing found\n") -} -} # /for curtags -} # /for issuelist -} # /for tweets_curday -} # /for drange -View(tweets) -View(c_errors) -View(tweets) -readYN <- function(question) { -n <- readline(prompt=question) -n <- as.character(n) -return(n) -} -checkIssue <- function(string, issuelist) { -status <- any(str_detect(string, issuelist)) -return(status) -} -checkAllIssues <- function(string, issuelist) { -status <- NULL -for(i in 1:length(string)) { -if(checkIssue(string[i], issuelist)) { -status[i] <- TRUE -} -else { -cat("Issue",string[i],"does not exist. Please try again.\n") -status[i] <- FALSE -} -} -return(status) -} -View(tweets) -View(tweets) -write.csv(tweets, "tweets.csv") -save(tweets, file="tweets.RData") -write.csv(tweets, "tweets.csv") -save(tweets, file="tweets.RData") -c_tweets <- read.csv("tweets.csv") -View(c_tweets) -c_tweets$X <- NULL -# Read all issues from XML file -c_issues <- data.frame(date = drange) -c_issuelist <- xmlToList("issues.xml") -c_issueheads <- names(issuelist) -c_issues[issueheads] <- 0 -source("issuecomp-codingsample-function.R") -rm(c_err, c_result, c_samid, c_samno,c_samtags,c_samissue,c_samtext,c_yn) -rm(c_samtag) -rm(tweets_curday,curacro, curchars, curdate,curfile,curid,curissue,curtag,curtags,curtext,d,date_end,date_start,drange,i,id_folder,oldissue,oldtag,s,t,tags_found) -c_errors <- read.csv("issuecomp-codingsample-error.csv", header = F, sep=",", colClasses="character") -View(c_errors) -names(c_errors) <- c("str_id", "code", "issue", "tags", "text") View(c_errors) for(r in 1:nrow(c_errors)) { c_errcode <- as.character(c_errors$code[r]) @@ -510,3 +124,389 @@ acc_df <- acc_df[-delrow, ] rm(delrow, r, acc) acc_df$row.names <- NULL row.names(acc_df) <- NULL +require(lubridate) +require(XML) +require(ggplot2) +require(reshape2) +require(stringr) +source("issuecomp-functions.R") +load("tweets.RData") +View(tweets) +View(tweets) +View(tweets) +date_start <- as.Date("2014-01-01") +date_start + days +date_start + days(1) +date_start + days(0) +date_start + days(0:2) +date_start <- as.Date("2014-01-01") +date_end <- as.Date("2014-12-31") +drange <- as.integer(date_end - date_start) +drange <- date_start + days(0:drange) +curdate <- date_start + days(2) +curdate +tweets[tweets[, "created_at"] == curdate, "msg_id"] +View(tweets) +curdate +tweets[tweets[, "created_at"] == "2014-01-01", "msg_id"] +tweets[tweets[, "created_at"] == curdate, "id_str"] +drange +length(tweets[tweets[, "created_at"] == curdate, "id_str"]) +length(tweets[tweets[, "created_at"] == curdate+1, "id_str"]) +length(tweets[tweets[, "created_at"] == curdate+15, "id_str"]) +stats <- data.frame(x=NULL) +View(stats) +stats <- data.frame(date=drange) +View(stats) +stats$tpd <- NULL +stats$tpd <- "" +stats$tpd <- NULL +stats$tpd[1] <- 2 +View(stats) +stats$tpd[2] <- 3 +View(stats) +stats$tpd <- "" +stats$tpd <- NULL +stats$tpd <- "" +stats$tpd[1] <- 2 +View(tweets) +View(stats) +stats <- data.frame(date=drange) +stats$tpd <- "" +# Total number of tweets per day over time +for(r in 1:length(drange)) { +stats$tpd[r] <- length(tweets[tweets[, "created_at"] == curdate, "id_str"]) +} +View(stats) +drange[2] +stats <- data.frame(date=drange) +stats$tpd <- "" +# 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"]) +} +View(stats) +plot.ts(x = stats$tpd, y=stats$date) +plot.ts(x = stats$date, y=stats$tpd) +g1 <- ggplot(stats, aes(date,tpd)) +g1 <- g1 + geom_histogram(fill="steelblue", stat="identity") +g1 <- g1 + stat_smooth(size=1,colour="red",method="loess", se=FALSE) +g1 <- gg1 + ggtitle("Zeitliche Entwicklung von plötzlichen Medienfokussen") + xlab("Einzelne Monate") + ylab("Plötzliche Medienfokusse") +g1 +g1 <- ggplot(stats, aes(date,tpd)) +g1 <- g1 + geom_histogram(fill="steelblue", stat="identity") +g1 +g1 <- g1 + stat_smooth(size=1,colour="red",method="loess", se=FALSE) +g1 +g1 <- ggplot(stats, aes(date,tpd)) +g1 <- g1 + geom_line() +g1 +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd, color=black)) +g1 +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd, color="black")) +g1 +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd)) +g1 +ggplot(stats, aes(date, tpd)) + geom_line() + +scale_x_date(format = "%b-%Y") + xlab("") + ylab("Daily Views") +ggplot(stats, aes(date, tpd)) + geom_line() + +scale_x_date() + xlab("") + ylab("Daily Views") +lapply(stats, class) +stats$tpd <- 0 +lapply(stats, class) +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"]) +} +View(stats) +plot.ts(x = stats$date, y=stats$tpd) +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd)) +g1 +ggplot(stats, aes(date, tpd)) + geom_line() + +scale_x_date() + xlab("") + ylab("Daily Views") +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +stats_smooth(size=1,colour="red",method="loess", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +stat_smooth(size=1,colour="red",method="loess", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +stat_smooth(colour="red",method="loess", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +stat_smooth(colour="red",method="lm", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +geom_smooth(colour="red",method="lm", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +geom_smooth(colour="red",method="loess", se=FALSE) +g1 +g1 <- ggplot() + +geom_line(data = stats, aes(x=date,y=tpd)) + +geom_smooth(size=1,colour="red",method="loess", se=FALSE) +g1 +g1 <- g1 + geom_smooth(size=1,colour="red", method="loess", se=FALSE) +g1 +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd)) +g1 <- g1 + geom_smooth(size=1,colour="red", method="loess", se=FALSE) +g1 +g1 <- ggplot() + geom_line(data = stats, aes(x=date,y=tpd)) +g1 +g1 + geom_smooth() +g1 + geom_smqwd +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) +g1 +g1 <- g1 + geom_smooth(size=1,colour="red", method="loess", se=FALSE) +g1 +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) +g1 <- g1 + geom_smooth(size=1,method="loess", se=FALSE) +g1 +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) +g1 <- g1 + geom_smooth(size=1,method="loess", se=FALSE, aes(group=1)) +g1 +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) + +geom_smooth(size=1,method="loess", se=FALSE, aes(group=1)) +g1 +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) + +geom_smooth(size=1,method="loess", se=FALSE, aes(x = date, y=tpd)) +g1 +geom_smooth(size=1,method="loess", se=FALSE) +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd)) + +geom_smooth(size=1,method="loess", se=FALSE) +g1 +plot.ts(x = stats$date, y=stats$tpd) +g1 <- ggplot() + geom_point(data = stats, aes(x=date,y=tpd, color=group)) + +geom_smooth(size=1,method="loess", se=FALSE) +g1 +install.packages(c("BH", "bibtex", "devtools", "dplyr", "httr", "jsonlite", "lazyeval", "manipulate", "RCurl", "ROAuth", "rstudioapi", "sp", "stringi")) +g1 <- ggplot(data = stats, aes(x=date,y=tpd, color=variable, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE) +g1 +stats_melt <- melt(stats, id="date") +View(stats_melt) +View(stats_melt) +stats_melt <- melt(stats, id="date") +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color=variable, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE) +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color=1, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE) +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color=1, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, colors="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color=1, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color="black", group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,color="yellow", group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour="black", group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour="#FFFFFF", group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour="vqwdqw", group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +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="red") +g1 +g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour=1, group=variable)) + +geom_line() + +geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color="red") +g1 +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 +require(lubridate) +require(XML) +require(ggplot2) +require(reshape2) +require(stringr) +source("issuecomp-functions.R") +rm(curdate) +rm(date_end, date_start) +rm(g1, r, ) +rm(g1, r) +id_folder <- "matched-ids" +unlink(id_folder, recursive = TRUE) +dir.create(id_folder) +issues <- data.frame(date = drange) +View(issues) +issuelist <- xmlToList("issues.xml") +issuelist +issueheads <- names(issuelist) +issueheads +issues[issueheads] <- 0 +tweets$issue <- "" +tweets$tags <- "" +View(issues) +# MATCH TWEETS ------------------------------------------------------------ +id_folder <- "matched-ids" +unlink(id_folder, recursive = TRUE) +dir.create(id_folder) +issues <- data.frame(date = drange) +issuelist <- xmlToList("issues.xml") +issueheads <- names(issuelist) +issues[issueheads] <- 0 +tweets$issue <- "" +tweets$tags <- "" +for(d in 1:nrow(issues)) { +# Go through every day +curdate <- issues$date[d] +cat(as.character(curdate),"\n") +# Put all tweets from specific day in a temporary DF +tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] +for(t in 1:nrow(tweets_curday)){ +# Select tweet's text, make it lowercase and remove hashtag indicators (#) +curtext <- as.character(tweets_curday$text[t]) +curtext <- str_replace_all(curtext, "#", "") +curid <- as.character(tweets_curday$id_str[t]) +# Now test each single issue (not tag!) +for(i in 1:length(issuelist)) { +curtags <- as.character(issuelist[[i]]) +curissue <- names(issuelist)[i] +curfile <- str_c(id_folder,"/",curissue,".csv") +# Now test all tags of a single issue +for(s in 1:length(curtags)) { +curtag <- curtags[s] +curchars <- nchar(curtag, type = "chars") +# 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 +} +# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance) +tags_found <- smartPatternMatch(curtext, curtag, curchars, curacro) +if(tags_found == 1) { +# 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, ";") +# Add information to file for function viewPatternMatching +write(str_c(curdate,";\"",curid,"\";",curtag), curfile, append = TRUE) +break +} +else { +#cat("Nothing found\n") +} +} # /for curtags +} # /for issuelist +} # /for tweets_curday +} # /for drange +View(issues) +# MATCH TWEETS ------------------------------------------------------------ +id_folder <- "matched-ids" +unlink(id_folder, recursive = TRUE) +dir.create(id_folder) +issues <- data.frame(date = drange) +issuelist <- xmlToList("issues.xml") +issueheads <- names(issuelist) +issues[issueheads] <- 0 +tweets$issue <- "" +tweets$tags <- "" +for(d in 1:nrow(issues)) { +# Go through every day +curdate <- issues$date[d] +cat(as.character(curdate),"\n") +# Put all tweets from specific day in a temporary DF +tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] +for(t in 1:nrow(tweets_curday)){ +# Select tweet's text, make it lowercase and remove hashtag indicators (#) +curtext <- as.character(tweets_curday$text[t]) +curtext <- str_replace_all(curtext, "#", "") +curid <- as.character(tweets_curday$id_str[t]) +# Now test each single issue (not tag!) +for(i in 1:length(issuelist)) { +curtags <- as.character(issuelist[[i]]) +curissue <- names(issuelist)[i] +curfile <- str_c(id_folder,"/",curissue,".csv") +# Now test all tags of a single issue +for(s in 1:length(curtags)) { +curtag <- curtags[s] +curchars <- nchar(curtag, type = "chars") +# 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 +} +# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance) +tags_found <- smartPatternMatch(curtext, curtag, curchars, curacro) +if(tags_found == 1) { +# 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, ";") +# Add information to file for function viewPatternMatching +write(str_c(curdate,";\"",curid,"\";",curtag), curfile, append = TRUE) +break +} +else { +#cat("Nothing found\n") +} +} # /for curtags +} # /for issuelist +} # /for tweets_curday +} # /for drange +#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) +View(issues) +save(issues, "issues.RData") +save(issues, file="issues.RData") +readYN <- function(question) { +n <- readline(prompt=question) +n <- as.character(n) +return(n) +} +checkIssue <- function(string, issuelist) { +status <- any(str_detect(string, issuelist)) +return(status) +} +checkAllIssues <- function(string, issuelist) { +status <- NULL +for(i in 1:length(string)) { +if(checkIssue(string[i], issuelist)) { +status[i] <- TRUE +} +else { +cat("Issue",string[i],"does not exist. Please try again.\n") +status[i] <- FALSE +} +} +return(status) +} +require(stringr) +require(XML) diff --git a/issuecomp-codingsample-correct.csv b/issuecomp-codingsample-correct.csv index b5f42e9..46ba6dd 100644 --- a/issuecomp-codingsample-correct.csv +++ b/issuecomp-codingsample-correct.csv @@ -1,2 +1,31 @@ "443032724505624576",0,"","","RT @Linksfraktion: Sevim Dagdelen: Optionspflicht abschaffen – ohne „Wenn“ und „Aber“ http://t.co/PXVz60RyDa" "464039981766684672",0,"","","Hange spricht von einer Kutsche ohne Dach in der wir fahren und hoffen, dass es nicht regne #btADA" +"528513118440525824",0,"","","Dora zu Erfolgen der #Hamburger Linken: kostenfreies Mittag + Initiative, dass Maklergebühr nicht von Vermieter*innen.bezahlt werden müssen " +"511144502590181376",0,"","","RT @2kdei: TY! RİP @SajadJiyad: post about David Haines? Use this photo. He would want us to remember him. URL #ISISmedi… " +"515791668969504768",0,"","","Anschauen: Beitrag Heute Show zu #TTIP URL " +"533717720689549312",0,"","","RT @JUKoMo: "Wie groß muss die Angst der SPD vor Julia Klöckner sein?" Starker @LSaktuellRP-Kommentar zum SPD-Parteitag: URL " +"472393996879527936",0,"","","RT @johannisbear: Bitte RT! Bitte helft @ulf_der_freak #Aurela darf nicht sterben! URL " +"499494814778662912",0,"","","Das wievielte Mal verspricht #Merkel die Angleichung der #Ostrenten an Westniveau? Es gibt gute Gründe zu misstrauen URL " +"530750832443400192",0,"","","ist - gezwungenermaßen - mit dem #Fernbus unterwegs #gdlstreik " +"532310093904089088",0,"","","SPD will Waldschluchtpfad als Dauerstandort - Nachrichten Gatow | SPANDAUER VOLKSBLATT URL " +"465407100408320000",0,"","","Die neue Modernität in den Kleingarten Kneipen URL " +"428481075732811776",0,"civil.208;","Datenschutz;",""Datenschutz soll nicht unverhältnismäßig geschwächt werden." Was heißt da unverhältnismäßig? #Regierungserklärung #Merkel #Bundestag " +"421711548189786114",0,"","","...und inzwischen gute Freunde. URL " +"532608281009979392",0,"","","RT @initiatived21: “@anipenny: ”Graswurzelbewegg medienpägogisch interessierter Lehrer vernetzt sich, braucht aber auch Unterstützg“ @Esken… " +"537931613464965121",0,"","","RT @StefanKaufmann: Es läuft Plenardebatte zum Haushalt des Bundesmin. für Bild. und Forschung. Trotz Schwarzer Null steigt Etat deutlich -… " +"499843193069129728",0,"","","#CETA - der komplette Text wurde heute geleakt: https://t.co/YeWUsSAHoB " +"477758769703944194",0,"","","Beim Tag der Offenen Tür des THW beeindruckt von der Vielfalt der Einsätze der Organisation. 10 Mill. im Haushalt sind hier gut eingesetzt " +"539467838520832000",0,"","","RT @spdbt: KoA-Vertrag muss gelten! @ThomasOppermann: „Bei #Maut darf es keine Mehrbelastung für deutsche Autofahrer geben." URL " +"449534756259381248",0,"","","Höflich, aber klares Statement zu Menschenrechten. Der Bundespräsident macht das gut! #China #XiJinping URL " +"514314498510168065",0,"","","RT @zeitonline_wir: Ban Ki Moon lädt zum #Klimagipfel. 120 Staats- und Regierungschefs kommen. Nur nicht Angela Merkel. (ae) #climate2014 h… " +"434413898054524928",0,"","","RT @KonstantinNotz: Zum Appell der SchriftstellerInnen URL und Videobericht zum Empfang der #grünen Bundestagsfraktion h… " +"509707990929534976",0,"","","RT @weltnetzTV: URL in Kooperation mit theREALnews! URL " +"542066419274637312",0,"","","RT @tagesthemen: "Ein bescheuerter Satz" - Sigmund Gottlieb kommentiert #YallaCSU. Jetzt in den Tagesthemen. URL " +"508921157131984896",0,"","","RT @drthomasfeist: #gain2014 @MartinRabanus @KaiGehring @PLengsfeld @KarambaDiaby @the_dfg @DAADnewyork @AvHStiftung @UniLeipzig URL " +"540745511024992257",0,"","","RT @maybritillner: .@MikeMohring Der Freistaat #Thueringen hat es nicht verdient von einer Regierung geführt zu werden, die sich als Experi… " +"507492334347763712",0,"","","Bitte RT: Wer kann helfen? Brauchen ganz dringend mobile Duschcontainer für die Flüchtlingszelte in #Nürnberg #followerpower " +"485509000268902400",0,"","","#WM2014 #Deutschland #GER - #Thalheim #Erzgebirge URL " +"542710860528234497",0,"","","Illegaler #Kunsthandel blüht! BReg muss nachbessern. Kein Umschlagplatz BRD f. Raubkunst. Dazu @GreenClaudia + ich: https://t.co/tr16CjQl42 " +"535525766919106560",0,"","","RT @EU_Salon: .@DJanecek: #TTIP Zivilgesellsxhaft hat Funktion zu treiben... ohne kritische Masse hätte es Diskussion so nicht gegeben. #ES3 " +"472481188641124352",0,"","","Vor dem Grand Serail in Beirut,- im Schatten @nouripour :-) URL " +"532818411374788608",0,"","","Laut einer Studie treibt unsere Debatte um Mietpreisbremse Mieten in die Höhe - tolle Wurst! " diff --git a/issuecomp-codingsample-error.csv b/issuecomp-codingsample-error.csv index be3513a..6007ae0 100644 --- a/issuecomp-codingsample-error.csv +++ b/issuecomp-codingsample-error.csv @@ -1,3 +1,6 @@ "532463690013233152",1,"","","RT @cordhos: ! “@cducsubt: Paul Breitner vom @FCBayern - #Bundestag jetzt mit eigenem Fanclub @hahnflo @DoroBaer @dieAlbsteigerin http://t.…" "516584367448403968",1,"","","Debate und critics in the parlamentarian assembly of the European Council about the elections in #Turkey @PACE_News @GeziParkii" "516624274522918912",1,"","","Nach Bürgergespräch bin nun noch im Ratshof zur Ausstellungseröffnung - Wanderausstellung zum Bundestag." +"530357749188923392",2,"","","Streiks müssen Auswirkungen haben - und die #bahn verletzt täglich Verbraucherinteressen: URL #gdlstreik #gdl #db " +"465846218858708992",2,"","","RT @bioland_de: Wieso sollen Biobauern dafür bestraft werden, dass sie KEINE Pestizide einsetzen? Genau das hat die EU vor: URL " +"543111899794407426",2,"","","RT @DanielLuecking: #NSAUA @Peter_Schaar Verhältnismäßigkeit muss hinterfragt werden - Grundrechtsverletzungen durch Überwachung gehören au… " diff --git a/issuecomp-codingsample-error2.csv b/issuecomp-codingsample-error2.csv new file mode 100644 index 0000000..745b9a1 --- /dev/null +++ b/issuecomp-codingsample-error2.csv @@ -0,0 +1 @@ +"530357749188923392","","labor.504","","Streiks müssen Auswirkungen haben - und die #bahn verletzt täglich Verbraucherinteressen: URL #gdlstreik #gdl #db " diff --git a/issuecomp-functions.R b/issuecomp-functions.R index c7ff670..a2d60dc 100644 --- a/issuecomp-functions.R +++ b/issuecomp-functions.R @@ -27,16 +27,20 @@ convertLogical0 <- function(var) { } smartPatternMatch <- function(string, pattern, chars, acronym) { - pattern <- str_c("\\b", pattern, "\\b") + patternrex <- str_c("\\b", pattern, "\\b") - if(chars <= 4) { - found <- agrep(pattern, string, max.distance = list(all = 0), ignore.case = !acronym, fixed = FALSE) + if(chars <= 4) { # 4 or less + found <- agrep(patternrex, string, max.distance = list(all = 0), ignore.case = !acronym, fixed = FALSE) } - else if(chars >= 8) { - found <- agrep(pattern, string, max.distance = list(all = 2), ignore.case = !acronym, fixed = FALSE) + else if(chars >= 8) { # 8 or more + found <- agrep(patternrex, string, max.distance = list(all = 3), ignore.case = !acronym, fixed = FALSE) + # Give longer words a chance by ignoring word boundaries \\b + if(convertLogical0(found) == 0) { + found <- grep(pattern, string, ignore.case = !acronym, fixed = FALSE) + } } - else { - found <- agrep(pattern, string, max.distance = list(all = 1), ignore.case = !acronym, fixed = FALSE) + else { # 5,6,7 + found <- agrep(patternrex, string, max.distance = list(all = 2), ignore.case = !acronym, fixed = FALSE) } found <- convertLogical0(found) return(found) diff --git a/issues.xml b/issues.xml index 1136af0..aaaae8a 100644 --- a/issues.xml +++ b/issues.xml @@ -1,4 +1,5 @@ +# Makroökonomie Wirtschaft Wirtschaftswachstum @@ -68,7 +69,6 @@ KFZ-Steuer Rassismus rassistisch Rechtsextremismus - Nazi Nazis