* Ignore composite names

* Show guesses
This commit is contained in:
Roberto Alsina 2023-06-04 15:30:30 -03:00
parent bfe2029b2d
commit 49352c8e37

View File

@ -4,16 +4,17 @@ require "ishi/html"
require "json" require "json"
class Handler 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 # Process the gnuplot output so it works in the page
# #
# buffer is the Ishi output # buffer is the Ishi output
# name is a string to replace for gnuplot_canvas so # 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 = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")] 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"> <div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas> <canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot"> <table class="plot">
@ -190,25 +191,47 @@ class Handler
[r[0].as_i, r[1].as_s] [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 if genero
datos = split_por_genero(datos)[genero] datos = split_por_genero(datos)[genero]
end end
datos = datos[..10] 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}? ¡Contáme 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 buffer = IO::Memory.new
Ishi.new(buffer) do Ishi.new(buffer) do
x = (1..10).to_a x = (0..datos.size - 1).to_a
y = (1..10).to_a y = datos.map { |r|
r[0].to_f
}
xtics = Hash(Float64, String).new
datos.each_with_index { |r, i|
xtics[i.to_f] = r[1].to_s.titleize
}
canvas_size(800, 300) canvas_size(800, 300)
plot(x, y, style: :boxes, fs: 0.25) plot(x, y, style: :boxes, fs: 0.25)
.boxwidth(0.5) .boxwidth(0.5)
.ylabel("Popularidad") .show_key(false)
.ylabel("Popularidad (miles)")
.xlabel("Nombre") .xlabel("Nombre")
.xtics(xtics)
end end
{ {
body: format_buffer(buffer, "busqueda"), body: format_buffer(buffer, "busqueda", title),
status_code: 200, status_code: 200,
headers: HTTP::Headers{"Content-Type" => "text/html"}, headers: HTTP::Headers{"Content-Type" => "text/html"},
} }