* 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"
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,49 @@ 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}? ¡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
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
}
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)")
.xlabel("Nombre")
.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