Make split by gender faster
This commit is contained in:
parent
e4c44dcae1
commit
cb9a24f4aa
@ -42,27 +42,6 @@ class Handler
|
|||||||
html.gsub("gnuplot_canvas", canvas_name)
|
html.gsub("gnuplot_canvas", canvas_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def query(sql)
|
|
||||||
# Runs a SQL query against the database.
|
|
||||||
#
|
|
||||||
# Returns an array of values [[Year,Count]...]
|
|
||||||
# Or nil if there are no results
|
|
||||||
|
|
||||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
|
||||||
cursor.query sql do |result_set|
|
|
||||||
result = [] of Tuple(Int32, Int32)
|
|
||||||
result_set.each do
|
|
||||||
year = result_set.read(Int32)
|
|
||||||
contador = result_set.read(Int32)
|
|
||||||
result.push({year, contador})
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
# No result, return nil
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_name(s)
|
def normalize_name(s)
|
||||||
# Remove diacritics, turn lowercase
|
# Remove diacritics, turn lowercase
|
||||||
normalized = s.unicode_normalize(:nfkd).chars
|
normalized = s.unicode_normalize(:nfkd).chars
|
||||||
@ -71,13 +50,12 @@ class Handler
|
|||||||
}.join("").downcase
|
}.join("").downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
def feminidad(nombre)
|
def feminidad(cursor, nombre)
|
||||||
# Yes this database is upper case
|
# Yes this database is upper case
|
||||||
nombre = nombre.to_s.upcase
|
nombre = nombre.to_s.upcase
|
||||||
sql1 = %(
|
sql1 = %(
|
||||||
SELECT COALESCE(frecuencia,0)
|
SELECT COALESCE(frecuencia,0)
|
||||||
FROM mujeres WHERE nombre='#{nombre}'
|
FROM mujeres WHERE nombre='#{nombre}'
|
||||||
|
|
||||||
)
|
)
|
||||||
sql2 = %(
|
sql2 = %(
|
||||||
SELECT COALESCE(frecuencia,0)
|
SELECT COALESCE(frecuencia,0)
|
||||||
@ -85,21 +63,19 @@ class Handler
|
|||||||
)
|
)
|
||||||
|
|
||||||
hombres = mujeres = 0
|
hombres = mujeres = 0
|
||||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
|
||||||
cursor.query sql1 do |result|
|
cursor.query sql1 do |result|
|
||||||
mujeres = result.read(Int32)
|
mujeres = result.read(Int32)
|
||||||
end
|
end
|
||||||
cursor.query sql2 do |result|
|
cursor.query sql2 do |result|
|
||||||
hombres = result.read(Int32)
|
hombres = result.read(Int32)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if hombres == mujeres == 0
|
if hombres == mujeres == 0
|
||||||
return 0.5
|
return 0.5
|
||||||
end
|
end
|
||||||
mujeres / (hombres + mujeres)
|
mujeres / (hombres + mujeres)
|
||||||
end
|
end
|
||||||
|
|
||||||
def split_por_genero(nombres)
|
def split_por_genero(cursor, nombres)
|
||||||
femeninos = Array(Tuple(Int32, String)).new
|
femeninos = Array(Tuple(Int32, String)).new
|
||||||
masculinos = Array(Tuple(Int32, String)).new
|
masculinos = Array(Tuple(Int32, String)).new
|
||||||
nombres.map { |nombre|
|
nombres.map { |nombre|
|
||||||
@ -200,8 +176,8 @@ class Handler
|
|||||||
datos = [] of Tuple(Int32, String)
|
datos = [] of Tuple(Int32, String)
|
||||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
||||||
cursor.query sql do |result_set|
|
cursor.query sql do |result_set|
|
||||||
puts "loop"
|
|
||||||
result_set.each do
|
result_set.each do
|
||||||
|
puts "loop"
|
||||||
valor = result_set.read(Int32)
|
valor = result_set.read(Int32)
|
||||||
nombre = result_set.read(String)
|
nombre = result_set.read(String)
|
||||||
datos.push({valor, nombre})
|
datos.push({valor, nombre})
|
||||||
@ -225,11 +201,12 @@ class Handler
|
|||||||
row[1].to_s.includes? " "
|
row[1].to_s.includes? " "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if genero
|
if genero
|
||||||
datos = split_por_genero(datos)[genero]
|
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
||||||
|
datos = split_por_genero(cursor, datos)[genero]
|
||||||
puts "Data split by gender"
|
puts "Data split by gender"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
datos = datos[..10]
|
datos = datos[..10]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user