Skip to content

Instantly share code, notes, and snippets.

@ricardotealdi
Last active November 8, 2015 17:18
Show Gist options
  • Save ricardotealdi/128572e07289b2701333 to your computer and use it in GitHub Desktop.
Save ricardotealdi/128572e07289b2701333 to your computer and use it in GitHub Desktop.
Improve vocabulary script by repetition
data = {
"amid" => ["em meio a", "no meio de", "entre"],
"bliss" => ["êxtase", "felicidade"],
"at first glance" => ["à primeira vista"],
"slumber" => ["dormir"],
"thrash out" => ["debater", "discutir"],
"plea" => ["apelo"],
"attire" => ["vestuário", "traje"],
"blunt" => ["brusco"],
"newlyweds" => ["recém-casados"],
"unfolding" => ["desdobrando"],
"gnawing away" => ["corroendo"],
"idyll" => ["idílio"],
"rustling" => ["sussurro"],
"wealthy" => ["rico"],
"bystanders" => ["transeuntes", "passantes"],
"folly" => ["insensatez"],
"doomed" => ["condenado"],
"welfare" => ["bem-estar"],
"assure" => ["assegurar"],
"low-lying areas" => ["áreas de baixa altitude"],
"amongst" => ["entre"],
"gloomy" => ["sombrio"],
"grimly" => ["tristemente", "sombriamente"],
"fodder" => ["dar forragem aos animais"],
"digest" => ["digerir"],
"first-hand" => ["primeira mão", "em primeira mão"],
"head off" => ["desviar"],
"wallowing" => ["chafurdando"],
"cosset" => ["mimado"],
"drowned" => ["afogado"],
"tide" => ["maré"],
"concede" => ["admitir"],
"feature (something)" => ["destacar algo"],
"swamped" => ["inundado"],
"mull" => ["ponderar"],
"steely" => ["feito de aço", "férreo"],
"ebullient" => ["efervescente"]
}.freeze
class WordFetcher
def initialize(repeat_times = 5, data = {})
@repeat_times = repeat_times
@data = data
@words = map_words
end
def give_me(quantity = 5)
take(quantity)
end
def update(words_to_update)
increment_tries = lambda do |row|
words.detect { |it| it.fetch(:word) == row.fetch(:word) }[:times] += 1
end
words_to_update.each(&increment_tries)
end
private
attr_reader :repeat_times, :data, :words
def take(quantity)
words.dup.
delete_if(&only_less_than_max_tries).
shuffle.
sort(&sort_by_tries).
take(quantity).
shuffle
end
def only_less_than_max_tries
->(row) { row.fetch(:times) >= repeat_times }
end
def sort_by_tries
->(a,b) { b.fetch(:times) <=> a.fetch(:times) }
end
def map_words
data.inject([]) do |acc, (k, v)|
acc << { word: k, translate: v , times: 0 }
acc
end
end
end
class Lesson
def initialize(
repeat_quantity = 5, data = {}
)
@repeat_quantity = repeat_quantity
@fetcher = WordFetcher.new(repeat_quantity, data)
end
def learn
while !(words = fetcher.give_me).empty?
system("clear") or system("cls")
words.each(&execute)
puts 'Press RETURN to continue...'
STDIN.gets.chomp.downcase
end
puts "Finished lesson"
end
private
attr_reader :repeat_quantity, :fetcher
def execute
lambda do |word|
puts "Translation of \"#{word.fetch(:word)}\""
answer = read_answer
correct_answer = word.fetch(:translate)
puts "The correct answers are \"#{correct_answer}\""
check_answer.call(answer, word)
puts "-" * 40
end
end
def check_answer
lambda do |answer, word|
answer_without_accent = remove_accent(answer)
correct_answers = Array(
word.fetch(:translate)
).each(&:downcase).map(&method(:remove_accent))
if correct_answers.any? { |correct_answer| answer_without_accent == correct_answer }
puts "Your answer is \e[32mCORRECT!\e[0m"
update_fetcher.call(word)
if word.fetch(:times) >= repeat_quantity
puts "\e[34mYou've just learned \"#{word.fetch(:word)}\"\e[0m"
end
else
puts "Your answer is \e[31mINCORRECT!\e[0m"
end
end
end
def update_fetcher
lambda do |word|
fetcher.update([word])
end
end
def read_answer
STDIN.gets.chomp.downcase
end
def remove_accent(word)
word.tr(
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
)
end
end
Lesson.new(3, data).learn
Lesson.new(3, data.invert).learn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment