Skip to content

Instantly share code, notes, and snippets.

@hikilaka
Created July 22, 2018 20:26
Show Gist options
  • Save hikilaka/964cca8b1b9675e00f5ac81cc83c4d10 to your computer and use it in GitHub Desktop.
Save hikilaka/964cca8b1b9675e00f5ac81cc83c4d10 to your computer and use it in GitHub Desktop.
library('tidyverse')
library('lubridate')
read_weechatlog <- function(file_name, parse_dates=TRUE, messages_only=TRUE,
sanitize_names=TRUE) {
pattern <- "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})\t(.+)\t(.+)"
lines <- readLines(file_name) %>%
str_match_all(pattern)
# using do.call(...) is WAY faster than calling
# rbind() for some reason...?
data <- do.call("rbind", lines) %>%
as.data.frame(stringsAsFactors=FALSE) %>%
transmute(date = V2, name = V3, message = V4)
if (parse_dates) {
date_format = "%Y-%m-%d %H:%M:%S"
data$date = parse_datetime(data$date, date_format)
}
if (messages_only) {
weechat_operators = c("--", "<--", "-->", " *")
data <- data %>%
filter(!name %in% weechat_operators)
}
if (sanitize_names) {
irc_usermodes = "[~&@%]"
data <- data %>%
mutate(name = str_replace_all(name, irc_usermodes, ""))
}
return(data)
}
log <- read_weechatlog("#trollhour.weechatlog")
selected_day = as.POSIXct("2018-7-20")
data <- log %>%
filter(year(date) == 2018 & month(date) == 7 & day(date) == 20) %>%
mutate(date = hour(date))
ggplot(data, aes(x=as.factor(date), group=name, color=name)) +
labs(x="Hour", y="Message count",
title=paste("Activity in #trollhour on", selected_day)) +
geom_smooth(stat="count", show.legend=FALSE) +
geom_point(stat="count")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment