# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

# Any results you write to the current directory are saved as output.

#  B2B 0.63523 R vers for Outbrain - based on python vers. by clustifier
library(data.table)
library(dplyr)
#---------------------------------------------------------------------------
reg <- 10   # 10 # trying anokas idea of regularization
#---------------------------------------------------------------------------
insert_hash <- function(x,ht) {
  key = as.character( x[1])
  value = x[2]
  ht[[key]] <- value
  return(invisible())  # return invisibly returns NULL
}

insert_hashclick <- function(x,ht=htadcntsclick, htcnts=htadcnts) {
  key = as.character( x[1])
  value = x[2] / (htcnts[[key]] + reg )
  ht[[key]] <- value
  return(invisible())  # return invisibly returns NULL
}

get_prob <- function(k) {
  # note k as.char ad_id, no entry in hash table will be NULL
  if (is.null(htadcntsclick[[k]])){
    return (0) 
  } else {
    return (htadcntsclick[[k]] )
  }
}


#------------------------------------------------------------------------

train <- fread("../input/clicks_train.csv", header = TRUE)  
#  dplyr for train ad cnts, ad click cnts
adcnts <-  train %>% group_by(ad_id) %>% summarise(count=n())
adcntsclick <- train[which(train$clicked==1)] %>% group_by(ad_id) %>% summarise(count=n())
rm(train)
#  hash ad cnts using R environment
htadcnts <- new.env() 
apply(adcnts,1,insert_hash,ht=htadcnts)
# hash calc ad clicks prob using ad click cnts, ad cnts 
htadcntsclick <- new.env()  
apply(adcntsclick,1,insert_hashclick,ht=htadcntsclick,htcnts=htadcnts) 

# test ads from sample_submission already grouped by display_id
subm <- fread("../input/sample_submission.csv", header = TRUE) # , nrows = N to test subset
submcols <- colnames(subm)
testsplit <- subm[, unlist(strsplit(ad_id, " " ))  ,by=display_id]
rm(subm)
testpreds <- testsplit[,prob :=  sapply(testsplit$V1,get_prob)] [order(display_id,-prob)][, paste0(V1, collapse=" "), by = display_id]
colnames(testpreds) <- submcols
rm(testsplit)
cat("saving the submission file\n")
runat <- format(Sys.time(), "%Y%b%d%H%M%S") # add timestamp to filename 
fname <- paste(runat, "submission.csv", sep="_")
write.csv(testpreds, fname, row.names = F)
#------------------------------------------------------
