Skip to content

R Scripts for Automation of Tarjani

Ananda Mahto edited this page Mar 9, 2015 · 1 revision
## THE WORKHORSES
tarjani <- function(..., outfile = NULL) {
  dots <- substitute(list(...))[-1]
  words <- sprintf("%s.mpg", sapply(dots, deparse))
  if (is.null(outfile)) outfile <- paste(dots, collapse = "_")
  command <- sprintf('ffmpeg -i "concat:%s" -c copy -target pal-dvd %s.mpg', 
                     paste(words, collapse = "|"), outfile)
  system(command, show.output.on.console = TRUE)
}

# example use
# Will create a file named "act_all_am_an.mpg"
tarjani(act, all, am, an)

## More likely to be used...
tarjani_ <- function(..., outfile = NULL) {
  dots <- list(...)
  words <- sprintf("%s.mpg", unlist(dots))
  if (is.null(outfile)) outfile <- paste(dots, collapse = "_")
  command <- sprintf('ffmpeg -i "concat:%s" -c copy -target pal-dvd %s.mpg', 
                     paste(words, collapse = "|"), outfile)
  system(command, show.output.on.console = TRUE)
}

# example use
# Will create a file named "Whhheeee.mpg"
tarjani_("act", "all", "am", "an", outfile = "Whhheeee")

# EXAMPLE INPUT DATASET
# The first column should be named "Word"
# For each level there will be two columns:
#     L***_s << indicates the session
#     L***_o << indicates the order within the session
df <- data.frame(Word = c("act", "all", "am", "an", "and", "ant", "apple", "apt", 
                          "are", "as", "at", "bad", "ban", "band", "bang", "bank", 
                          "bat", "be", "bed", "beg"), 
                 L1_s = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, rep(NA, 10)),
                 L1_o = c(3, 1, 2, 2, 1, 3, 1, 4, 2, 3, rep(NA, 10)),
                 L2_s = c(rep(NA, 5), 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
                 L2_o = c(rep(NA, 5), 5, 1, 4, 2, 3, 1, 4, 3, 2, 5, 1, 5, 3, 2, 4),
                 stringsAsFactors = FALSE)

library(data.table)
DT <- data.table(df)
DT
#      Word L1_s L1_o L2_s L2_o
#  1:   act    1    3   NA   NA
#  2:   all    1    1   NA   NA
#  3:    am    1    2   NA   NA
#  4:    an    2    2   NA   NA
#  5:   and    2    1   NA   NA
#  6:   ant    2    3    1    5
#  7: apple    3    1    1    1
#  8:   apt    3    4    1    4
#  9:   are    3    2    1    2
# 10:    as    3    3    1    3
# 11:    at   NA   NA    2    1
# 12:   bad   NA   NA    2    4
# 13:   ban   NA   NA    2    3
# 14:  band   NA   NA    2    2
# 15:  bang   NA   NA    2    5
# 16:  bank   NA   NA    3    1
# 17:   bat   NA   NA    3    5
# 18:    be   NA   NA    3    3
# 19:   bed   NA   NA    3    2
# 20:   beg   NA   NA    3    4

## THE AGGREGATION FUNCTION/API
## Specify:
##  1. The data source
##  2. The level (quoted, as in "L1", "L2", "L2M")
##  3. The output file naming format, either "sessions" or "combine"
##      a. "sessions" results in file names like "L1_Session_01.mpg"
##      b. "combine" results in file names like "an_am_at.mpg"
##  4. Whether you just want to preview the output or not
##  5. Whether you want to create a csv file of the relevant details
makeSessions <- function(indt, level, naming = c("sessions", "combine"), 
                         preview = TRUE, write = TRUE) {
  S <- paste0(level, "_s")
  O <- paste0(level, "_o")
  temp <- setorderv(indt[complete.cases(get(S)), c("Word", S, O), with = FALSE], c(S, O))
  P <- temp[, list(Words = paste(Word, collapse = ", ")), by = eval(S)]
  
  if (isTRUE(write)) write.csv(P, sprintf("Tarjani_%s.csv", level), row.names = FALSE)
  
  if (isTRUE(preview)) {
    P
  } else {
    temp[, tarjani_(Word, outfile = switch(
      naming,
      sessions = sprintf("%s_Session_%02d", level, get(S)),
      combine = paste(Word, collapse = "_"))), by = eval(S)]
  }
}

# Usage -- to get a session list (not combine)
# This grabs the words for L2 and displays the ordered list of words
# It also creates a csv file with the same information in the working directory
makeSessions(DT, "L2", naming = "sessions", TRUE, FALSE)
#    L2_s                    Words
# 1:    1 apple, are, as, apt, ant
# 2:    2 at, band, ban, bad, bang
# 3:    3  bank, bed, be, beg, bat

# Usage -- to combine, as per the order shown above
# Leave "naming" as sessions for a file name like
# "L2_Session_01.mpg"
# Change to "combine" if you want a file name like
# "apple_are_as_apt_ant.mpg"
makeSessions(DT, "L2", naming = "sessions", FALSE)