Created
April 30, 2014 12:16
-
-
Save jonatas/171dfa4b37852399e892 to your computer and use it in GitHub Desktop.
programando um conversor de fala como meu filho lorenzo fala com 2,5 anos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APLICA_COM = {letras: "[a-z]", vogais: "[aeiou]", consoantes: "[bcdfghjklmnpqrstwxyz]"} | |
def troca string, opts={} | |
lambda do |na_string| | |
if entre=opts[:entre] | |
if entre.is_a? Symbol | |
start_with = ends_with = APLICA_COM[entre] | |
elsif entre.is_a? Range | |
p "range,", entre.first,entre.last, APLICA_COM | |
start_with, ends_with = APLICA_COM[entre.first], APLICA_COM[entre.last] | |
end | |
if not start_with or not ends_with | |
raise "no opts for #{entre.class} #{opts.inspect} in #{APLICA_COM.inspect}" | |
end | |
regex = /(#{start_with})#{string}(#{ends_with})/ | |
#puts "#{regex} para #{opts.inspect}" | |
return na_string.gsub(regex, "\\1#{opts[:por]}\\2") | |
elsif no=opts[:no] | |
if no == :inicio | |
regex = /^#{string}/ | |
elsif no == :fim | |
regex = /#{string}$/ | |
end | |
else | |
regex = /#{string}/ | |
end | |
#puts "#{regex} para #{opts.inspect}" | |
if regex and opts[:por] | |
na_string.gsub(regex, opts[:por]) | |
else | |
raise "no regex for #{opts}" | |
end | |
end | |
end | |
def perde string, opts={} | |
troca string, opts.merge(por: "") | |
end | |
LORENZO = [ | |
troca('r', por: 'l', no: :inicio), | |
troca('g', por: 'd', no: :inicio), | |
troca('c', no: :inicio, por: 't'), | |
perde('r', entre: :vogais..:consoantes), | |
perde('r', entre: :consoantes..:vogais), | |
perde('s', entre: :vogais..:consoantes) | |
] | |
ao_inves_de_ele_fala = { | |
"porco" => "poico", | |
"porta" => "poita", | |
"caminhão" => "taminhão", | |
"brilho" => "bilho", | |
"trabalho" => "tabalho", | |
"abacaxi" => "abataxi", | |
"amendoin" => "ameoin", | |
"pascoa" => "paoa", | |
"vassoura" => "vassola", | |
"tesoura" => "tesola", | |
"mascara" => "macala", | |
"gosto" => "dosto", | |
"quero" => "quelo" | |
} | |
def lorenzo_fala string | |
_string = string | |
LORENZO.each do |rex| | |
puts "#{_string} = #{rex.call _string}" | |
_string = rex.call _string | |
end | |
return _string | |
end | |
describe "Fala do Lorenzo" do | |
ao_inves_de_ele_fala.each do |certo, ele_fala| | |
it "o certo é: '#{certo}' mas ele fala: '#{ele_fala}'" do | |
lorenzo_fala(certo).should == ele_fala | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment