Compare commits
6 Commits
main
...
ea9e65a20b
Author | SHA1 | Date | |
---|---|---|---|
ea9e65a20b | |||
19f25017f0 | |||
b05327fd8d | |||
9a84e979ac | |||
539c0839db | |||
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,38 +63,36 @@ 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|
|
||||||
fem = feminidad(nombre[1])
|
# fem = feminidad(cursor, nombre[1])
|
||||||
# El overlap en 0.5 es intencional!
|
# # El overlap en 0.5 es intencional!
|
||||||
if fem >= 0.5
|
# if fem >= 0.5
|
||||||
femeninos << nombre
|
# femeninos << nombre
|
||||||
end
|
# end
|
||||||
if fem <= 0.5
|
# if fem <= 0.5
|
||||||
masculinos << nombre
|
# masculinos << nombre
|
||||||
end
|
# end
|
||||||
}
|
# }
|
||||||
{
|
# {
|
||||||
"f": femeninos,
|
# "f": femeninos,
|
||||||
"m": masculinos,
|
# "m": masculinos,
|
||||||
}
|
# }
|
||||||
end
|
# end
|
||||||
|
|
||||||
def run(request : HTTP::Request)
|
def run(request : HTTP::Request)
|
||||||
# Try to find most popular names based on a prefix, year and gender.
|
# Try to find most popular names based on a prefix, year and gender.
|
||||||
@@ -198,10 +174,10 @@ class Handler
|
|||||||
puts "QUERY: #{sql}"
|
puts "QUERY: #{sql}"
|
||||||
|
|
||||||
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(DB_URL) 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,12 +201,38 @@ class Handler
|
|||||||
row[1].to_s.includes? " "
|
row[1].to_s.includes? " "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if genero
|
if genero
|
||||||
datos = split_por_genero(datos)[genero]
|
DB.open(DB_URL) do |cursor|
|
||||||
|
filtered = Array(Tuple(Int32, String)).new
|
||||||
|
datos.map { |item|
|
||||||
|
# How feminine is this name?
|
||||||
|
# Yes this database is upper case
|
||||||
|
nombre = item[1].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 feminidad >= 0.5 && genero == "f"
|
||||||
|
filtered << item
|
||||||
|
elsif feminidad <= 0.5 && genero == "m"
|
||||||
|
filtered << item
|
||||||
|
end
|
||||||
|
}
|
||||||
|
datos = filtered
|
||||||
puts "Data split by gender"
|
puts "Data split by gender"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
datos = datos[..10]
|
datos = datos[..10]
|
||||||
|
|
||||||
if datos.size > 1
|
if datos.size > 1
|
||||||
|
Reference in New Issue
Block a user