Make split by gender faster
This commit is contained in:
parent
b05327fd8d
commit
19f25017f0
@ -75,24 +75,24 @@ class Handler
|
||||
mujeres / (hombres + mujeres)
|
||||
end
|
||||
|
||||
def split_por_genero(cursor, nombres)
|
||||
femeninos = Array(Tuple(Int32, String)).new
|
||||
masculinos = Array(Tuple(Int32, String)).new
|
||||
nombres.map { |nombre|
|
||||
fem = feminidad(cursor, nombre[1])
|
||||
# El overlap en 0.5 es intencional!
|
||||
if fem >= 0.5
|
||||
femeninos << nombre
|
||||
end
|
||||
if fem <= 0.5
|
||||
masculinos << nombre
|
||||
end
|
||||
}
|
||||
{
|
||||
"f": femeninos,
|
||||
"m": masculinos,
|
||||
}
|
||||
end
|
||||
# def split_por_genero(cursor, nombres)
|
||||
# femeninos = Array(Tuple(Int32, String)).new
|
||||
# masculinos = Array(Tuple(Int32, String)).new
|
||||
# nombres.map { |nombre|
|
||||
# fem = feminidad(cursor, nombre[1])
|
||||
# # El overlap en 0.5 es intencional!
|
||||
# if fem >= 0.5
|
||||
# femeninos << nombre
|
||||
# end
|
||||
# if fem <= 0.5
|
||||
# masculinos << nombre
|
||||
# end
|
||||
# }
|
||||
# {
|
||||
# "f": femeninos,
|
||||
# "m": masculinos,
|
||||
# }
|
||||
# end
|
||||
|
||||
def run(request : HTTP::Request)
|
||||
# Try to find most popular names based on a prefix, year and gender.
|
||||
@ -174,7 +174,7 @@ class Handler
|
||||
puts "QUERY: #{sql}"
|
||||
|
||||
datos = [] of Tuple(Int32, String)
|
||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
||||
DB.open(DB_URL) do |cursor|
|
||||
cursor.query sql do |result_set|
|
||||
result_set.each do
|
||||
puts "loop"
|
||||
@ -202,14 +202,30 @@ class Handler
|
||||
}
|
||||
|
||||
if genero
|
||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
||||
DB.open(DB_URL) do |cursor|
|
||||
filtered = Array(Tuple(Int32, String)).new
|
||||
datos.map { |nombre|
|
||||
fem = feminidad(cursor, nombre[1])
|
||||
# How feminine is this name?
|
||||
# Yes this database is upper case
|
||||
nombre = nombre.to_s.upcase
|
||||
feminidad = 0
|
||||
sql = %(
|
||||
SELECT COALESCE((SELECT frecuencia FROM mujeres WHERE nombre='#{nombre}'), 0) AS mujeres,
|
||||
COALESCE((SELECT frecuencia FROM hombres WHERE nombre='#{nombre}'), 0) AS hombres
|
||||
)
|
||||
cursor.query sql do |result|
|
||||
mujeres = result.read(Int32)
|
||||
hombres = result.read(Int32)
|
||||
if hombres == mujeres == 0
|
||||
feminidad = 0.5
|
||||
else
|
||||
feminidad = mujeres / (hombres + mujeres)
|
||||
end
|
||||
end
|
||||
# El overlap en 0.5 es intencional!
|
||||
if fem >= 0.5 && genero == "f"
|
||||
if feminidad >= 0.5 && genero == "f"
|
||||
filtered << nombre
|
||||
elsif fem <= 0.5 && genero == "m"
|
||||
elsif feminidad <= 0.5 && genero == "m"
|
||||
filtered << nombre
|
||||
end
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user