Showing posts with label Fun. Show all posts
Showing posts with label Fun. Show all posts

Sunday, September 20, 2015

Dear Silvio!..

R-Code to produce this nice gif-animated greeting card can be viewed HERE.

Photobucket
Read more »

Tweaking Movie Subtitles with R

I use R to fix subtitles that are not in sync with my movies. For the example below the subs were showing too early - so I added some time to each sequence in the srt file. For simplicity I used exactly 1 second in the below example.
You'll see that I use my function dl_from_dropbox(), on which I wrote a post previously, to get the example file!





setwd(tempdir()")
options(digits = 12)
options(digits.secs = 3)

### get subtitle example file:
dl_from_dropbox <- function(x, key) {
require(RCurl)
bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x),
ssl.verifypeer = FALSE)
con <- file(x, open = "wb")
writeBin(bin, con)
close(con)
message(noquote(paste(x, "read into", getwd())))
}

dl_from_dropbox("Game_of_Thrones_S3_E1_engl.srt", "wojo9k8v8cezs9g")
shell.exec("Game_of_Thrones_S3_E1_engl.srt") #I use the MS text-editor to view srt files

# https://www.dropbox.com/s/wojo9k8v8cezs9g/Game_of_Thrones_S3_E1_engl.srt
###

### tweak the file by changing the time - i.e., I add 1 sec to all sequences here:
t <- readLines("Game_of_Thrones_S3_E1_engl.srt")
tt <- unlist(strsplit(t, " --> ")) #split time start/end

x <- grep("\\d{2}:\\d{2}:\\d{2},\\d{3}", t) #ids of time data in t
y <- sort(c(x, x+1)) #ids of time data in tt

ttt <- gsub(",",".", tt[y]) #replace decimal comma

(a <- strptime(ttt, format="%H:%M:%OS", tz="GMT")) #convert to date/time
(b <- as.numeric(a)) #convert to number

c <- 1 #add 1 sec

(d <- as.POSIXct(as.numeric(b+c+1e-6), origin="1970-01-01", tz="GMT")) #convert back
(e <- format(d, "%H:%M:%OS")) #re-format
(f <- gsub("\\.", ",", e)) #replace decimal point

id_t1 <- seq(1, length(y), 2)
id_t2 <- seq(0, length(y), 2)

(g <- paste0(f[id_t1], " --> ", f[id_t2])) #bring into original form

t_new <- t
t_new[x] <- g #insert new sequences into original data
print(t_new)

### save to new file:
write(t_new, "Game_of_Thrones_S3_E1_engl_new.srt")
shell.exec("Game_of_Thrones_S3_E1_engl_new.srt") #I use the MS text-editor to view srt files
Read more »

Some Fun with googleVis - Mapping Blog Visits on Google Map

See stand-alone code to produce this map below.


require(RJSONIO)
require(RCurl)
require(plyr)
library(googleVis)

# function to retrieve coords from ip-addresses
# (https://github.com/rtelmore/RDSTK/blob/master/src/RDSTK/R/functions.R)
ip2coordinates <- function(ip, session=getCurlHandle()) {
api <- "http://www.datasciencetoolkit.org/ip2coordinates/"
get.ips <- getURL(paste(api, URLencode(ip), sep=""), curl=session)
result <- ldply(fromJSON(get.ips), data.frame)
names(result)[1] <- "ip.address"
return(result)
}

# read log-file:
setwd(tempdir())
download.file("http://docs.google.com/uc?export=download&id=0B2wAunwURQNsZjY4Njc1OTYtYWE3My00ZjNjLTg1YzEtNDNlNmYyYmNiYmI0",
destfile = "google_docs.csv", mode = "wb")
log <- read.csv(paste(tempdir(),"/", "google_docs.csv", sep = ""),
header = T, stringsAsFactors = F)

# create dataframe to collect coords:
nr = nrow(log)

Lon <- as.numeric(rep(NA, nr))
Lat <- Lon
Coords <- data.frame(Lon, Lat)

# some will not be found (I will dismiss these rows later):
for (i in 1:nr){
try(
Coords[i, 1:2] <- ip2coordinates(log$IP.Address[i])[c("longitude", "latitude")]
)
}

# append to log-file:
log <- data.frame(log, Lat = Coords$Lat, Long = Coords$Lon,
LatLong = paste(round(Coords$Lat, 1),
round(Coords$Lon, 1),
sep = ":"))

log_gmap <- log[!is.na(log$Lat), ]
gmap <- gvisMap(log_gmap, "LatLong",
options = list(showTip = TRUE, enableScrollWheel = TRUE,
mapType = 'hybrid', useMapTypeControl = TRUE,
width = 550, height = 300))
plot(gmap)
Read more »