Compare commits

...

2 Commits

Author SHA1 Message Date
Roberto Alsina 823cc24e8a Typo, usar miles de verdad 2023-06-04 17:39:03 -03:00
Roberto Alsina 49352c8e37 * Ignore composite names
* Show guesses
2023-06-04 15:30:30 -03:00
1 changed files with 35 additions and 13 deletions

View File

@ -4,16 +4,17 @@ require "ishi/html"
require "json"
class Handler
def format_buffer(buffer, canvas_name)
def format_buffer(buffer, canvas_name, title)
# Process the gnuplot output so it works in the page
#
# buffer is the Ishi output
# name is a string to replace for gnuplot_canvas so
# we can have multiple charts in a page
# we can have multiple charts in a page
# title is added on top of the chart
html = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
html = html.join("\n") + %(
html = "<b>#{title}</b>" + html.join("\n") + %(
<div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot">
@ -28,7 +29,7 @@ class Handler
</script>
</div>
)
# This ID needs to be unique in case
# This ID needs to be unique in case
# we have 2 charts in the same page
html.gsub("gnuplot_canvas", canvas_name)
end
@ -190,27 +191,48 @@ class Handler
[r[0].as_i, r[1].as_s]
}
# In this context, remove all composite names
datos.reject! { |r|
r[1].to_s.includes? " "
}
if genero
datos = split_por_genero(datos)[genero]
end
datos = datos[..10]
if datos.size > 1
title = "¿Puede ser ... #{datos[0][1].to_s.titleize}? ¿O capaz que #{datos[1][1].to_s.titleize}? ¡Contame más!"
elsif datos.size == 1
title = "Me parece que ... #{datos[0][1].to_s.titleize}!"
else
title = "No tengo idea!"
end
buffer = IO::Memory.new
Ishi.new(buffer) do
x = (1..10).to_a
y = (1..10).to_a
x = (0..datos.size - 1).to_a
y = datos.map { |r|
r[0].to_f / 1000
}
xtics = Hash(Float64, String).new
datos.each_with_index { |r, i|
xtics[i.to_f] = r[1].to_s.titleize
}
canvas_size(800, 300)
plot(x,y, style: :boxes, fs: 0.25)
.boxwidth(0.5)
.ylabel("Popularidad")
.xlabel("Nombre")
plot(x, y, style: :boxes, fs: 0.25)
.boxwidth(0.5)
.show_key(false)
.ylabel("Popularidad (miles)")
.xtics(xtics)
end
{
body: format_buffer(buffer, "busqueda"),
body: format_buffer(buffer, "busqueda", title),
status_code: 200,
headers: HTTP::Headers{"Content-Type" => "text/html"},
}
end
end
end