Skip to content
Snippets Groups Projects

LearnBraille

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Alfonso Siciliano

    A simple Lua chunk to review Braille.

    Edited
    learnbraille.lua 1.94 KiB
    -- Learn Braille 0.0.1
    
    -- Configure
    local CONVERSION_TABLE = "--forward unicode.dis,it-it-comp6.utb"
    local BLACK_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    --local BLACK_CHARS = ',.;:@$?![]'
    local REPEAT_CHAR_QUIZ = 2
    
    -- Do not edit.
    
    local function braille(str)
    	local h = io.popen("echo -n '" .. str .. "' | lou_translate " .. CONVERSION_TABLE)
    	local b = h:read("*l") -- * for old lua versions
    	h:close()
    	return b
    end
    
    local function random()
    	local question = {}
    	local chars = BLACK_CHARS
    	local len = #chars
    	local repeatchar = {}
    
    	if REPEAT_CHAR_QUIZ < 1 then
    		print("Error: REPEAT_CHAR_QUIZ < 1")
    		os.exit(1)
    	end
    	math.randomseed(os.time())
    	while len > 0 do
    		local r = math.random(1, len)
    		local c = string.sub(chars, r, r)
    		table.insert(question, c)
    		repeatchar[c] = 1 + (repeatchar[c] or 0)
    		if repeatchar[c] == REPEAT_CHAR_QUIZ then
    			chars = string.sub(chars, 1, r-1) .. string.sub(chars, r + 1)
    		end
    		len = #chars
    	end
    
    	return question
    end
    
    print("LearnBraille 0.0.1\n")
    
    io.write(string.format("Conversion table: %s\n", CONVERSION_TABLE))
    io.write(string.format("Black chars:      %s\n", BLACK_CHARS))
    io.write(string.format("Repeat a char:    %s\n\n", REPEAT_CHAR_QUIZ))
    
    local quiz = random()
    local nquiz = #quiz
    local curr = 0
    local nerrors = 0
    local c = table.remove(quiz)
    
    while c do
    	curr = curr + 1
    	io.write(string.format("Question [%d/%d]\n", curr, nquiz))
    	io.write(string.format(" |%s|? ", braille(c)))
    	local input = io.read()
    	if c == input then
    		print "\tCorrect!\n"
    	else
    		io.write(string.format("\tError! The char is:  %s\n\n", c))
    		nerrors = nerrors + 1
    		nquiz = nquiz + 1
    		table.insert(quiz, 1, c)
    	end
    	c = table.remove(quiz)
    end
    
    print("\n")
    local correct = nquiz - nerrors
    io.write(string.format("Questions: %i, ", nquiz))
    io.write(string.format("Correct: %i (%.0f%%), ", correct, (correct * 100) / nquiz))
    io.write(string.format("Errors: %i (%.0f%%)", nerrors, (nerrors * 100) / nquiz))
    print("\n\n")
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment