Skip to content

Instantly share code, notes, and snippets.

@daroczig
Created December 3, 2019 16:40
Show Gist options
  • Save daroczig/471b826cc17cec63e45ce595eff44420 to your computer and use it in GitHub Desktop.
Save daroczig/471b826cc17cec63e45ce595eff44420 to your computer and use it in GitHub Desktop.
Hungarian - German words tester
library(googlesheets)
library(logger)
library(crayon)
suppressPackageStartupMessages(library(data.table))
logpath <- '/tmp'
logpath <- getwd()
logfile <- file.path(logpath, paste0(as.integer(Sys.time()), '.log'))
log_appender(appender_file(logfile))
log_threshold(TRACE)
## TODO praise pkg
SUCCESS <- c(
'Ügyes vagy!',
"Szép munka!",
"Ez az, csak így tovább!",
"Juhúúú, ez is sikerült!",
"Nagyon megy ez!"
)
FAIL <- c(
"Ajaj, ez sajnos most nem sikerült.",
"Húha, sajnos nem jó választ adtál!",
"Hm, legközelebb jobban gondold át!"
)
ask <- function(text) {
## readline
cat(text)
readLines("stdin", n = 1)
}
## #############################################################################
## load dictionary
sheet <- '1qcaTJRC_Gd3NG1mJ48TYmz_odXaVG-fBsm3tSBiwO_A'
sheet <- suppressMessages(gs_key(sheet))
sheet <- suppressMessages(gs_read(sheet))
sheet <- data.table(sheet)
setnames(sheet, c('german', 'hungarian', 'active'))
sheet <- sheet[!is.na(active)]
## #############################################################################
## UI
n <- 10
cat('Hány feladatot szeretnél megoldani? ')
n <- suppressWarnings(as.numeric(readLines("stdin", n = 1)))
if (is.na(n)) {
stop('HIBA!! Számot adj meg.')
}
log_info('Starting {n} exercises')
scores <- 0
for (i in seq_len(n)) {
row <- sample(1:nrow(sheet), 1)
question <- paste('Hogy van németül?', shQuote(sheet$hungarian[row]))
answer <- sheet$german[row]
cat('[', i, '/', n, '] ', sep = '')
response <- ask(question)
log_debug('Exercise #{i}: {question}')
pass <- response == answer
log_debug('Response for #{i}: {response} ({pass})')
log_info('Exercise #{i}: {question}{response} ({pass})')
if (isTRUE(pass)) {
scores <- scores + 1
cat(green(sample(SUCCESS, 1), '\n'))
} else {
cat(red(sample(FAIL, 1), '\n'))
cat('A helyes megoldás: ', answer, '\n')
}
}
log_info(paste('Score: ', scores, ' / ', n, ' (', round(scores / n * 100, 2), '%)', sep = ''))
cat('Pontszám: ', scores, ' / ', n, ' (', round(scores / n * 100, 2), '%)\n', sep = '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment