6 # options(mc.cores = 20)
7 # Sys.setenv(GOTO_NUM_THREADS=05)
9 generate.spells <- function (page, d) {
11 x <- d[page,mult="all"]
13 x <- x[sort.list(x$timestamp),]
15 # transform the target because there are some differences that don't matter
16 x$target <- gsub('_', ' ', x$target)
17 x$target <- gsub("(^[[:alpha:]])", "\\U\\1", x$target, perl=TRUE)
18 x$target <- gsub('\\#.*$', '', x$target)
21 x$redirect.prev <- c(FALSE, x$redirect[1:(length(x$redirect)-1)])
22 x$target.prev <- c(NA, x$target[1:(length(x$redirect)-1)])
24 x$redirect.prev <- FALSE
28 # get a list of transitions
29 x <- x[x$redirect != x$redirect.prev |
30 ((!is.na(x$target) & !is.na(x$target.prev)) &
31 x$target != x$target.prev),]
33 # if there is only one transition it stays that way
35 x$end <- c(x$timestamp[2:dim(x)[1]], NA)
40 x <- x[x$redirect == TRUE,]
43 x <- x[,c("page.id", "timestamp", "end", "page.title", "target")]
44 colnames(x) <- c("page.id", "start", "end", "page.title", "target")
49 filename.to.spells <- function (filename) {
50 con <- pipe(paste("bzcat", filename))
52 d <- read.delim(con, stringsAsFactors=FALSE, header=FALSE, skip=1,
53 encoding="UTF-8", quote="")
55 colnames(d) <- c("page.id", "revision.id", "page.title", "timestamp",
56 "deleted", "redirect", "target")
58 d$timestamp <- as.POSIXct(d$timestamp, tz="UTC", origin="1970-01-01 00:00:00")
62 redirected.pages <- unique(d$page.title[d$redirect])
64 # convert to data.table
66 setkey(d, "page.title")
68 redirect.spells <- do.call("rbind", lapply(redirected.pages, generate.spells, d))
70 return(redirect.spells)
75 run <- as.numeric(commandArgs(TRUE)) + 1
76 run.string <- sprintf("%03d", run)
78 setwd("/nfs/home/B/bhill/data/wp-enwiki-redir")
79 redirect.spells <- filename.to.spells(list.files()[run])
81 setwd("/nfs/home/B/bhill/data/wp-enwiki-redir-spells")
82 save(redirect.spells, file=paste("redirect_spells-", run.string, ".RData", sep=""))
85 # filename <- "wp_edits_redir_070.tsv.bz2"