#!/usr/bin/env ruby
# jochem, http://vorm.net/captchas 

def shuffle(a)
    (a.size-1).downto(1) { |i|
        j = rand(i+1)
        a[i], a[j] = a[j], a[i] if i != j
    }
end

all=Dir["google/[0-9]*.wav"];
shuffle(all)
train=all[0..80]
test=all-train

ignored = 0
correct = 0
tmp = String.new
fo = File.open("google.txt", "w") 
train.each do |fi|
	i=0 
	o=$1 if fi =~ /([0-9]{5,15})/
	p = IO.popen("./devoicecaptcha #{fi} --train", "r")
	tmp = "#{o[i,1]}:\n"
	while l=p.gets
		if l=~ /^$/
			i+=1
			tmp += "#{o[i,1]}:\n" if o[i,1] =~ /[0-9]/
		else 
			tmp += l 
		end
	end
	if i==o.length
		fo.puts tmp
	else
		ignored += 1 # incorrect nr of words
	end
end
fo.close

test.each do |fi|
	o=$1 if fi =~ /([0-9]{5,15})/
	p = IO.popen("./devoicecaptcha #{fi}", "r")
	while l=p.gets
		g=$1 if l=~ /^Guess is: ([0-9]*)$/
	end
	correct += 1 if g==o 
end

puts "Ignored: #{ignored} out of #{train.length} (#{ignored*100/train.length}%)"
puts "Correct: #{correct} out of #{test.length} (#{correct*100/test.length}%)"

