Improve readability of duplicate report frontend parsing / generation

This commit is contained in:
Liam 2024-07-19 08:50:16 -04:00
parent 11b414bf7b
commit 79f508f603
3 changed files with 26 additions and 22 deletions

View file

@ -15,7 +15,8 @@ defmodule Philomena.DuplicateReports do
def generate_reports(source) do
source = Repo.preload(source, :intensity)
duplicates_of(source.intensity, source.image_aspect_ratio, 0.2, 0.05)
{source.intensity, source.image_aspect_ratio}
|> find_duplicates(dist: 0.2)
|> where([i, _it], i.id != ^source.id)
|> Repo.all()
|> Enum.map(fn target ->
@ -25,7 +26,11 @@ defmodule Philomena.DuplicateReports do
end)
end
def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05, limit \\ 10) do
def find_duplicates({intensities, aspect_ratio}, opts \\ []) do
aspect_dist = Keyword.get(opts, :aspect_dist, 0.05)
limit = Keyword.get(opts, :limit, 10)
dist = Keyword.get(opts, :dist, 0.25)
# for each color channel
dist = dist * 3

View file

@ -17,10 +17,11 @@ defmodule PhilomenaWeb.ImageReverse do
{analysis, intensities} ->
{width, height} = analysis.dimensions
aspect = width / height
dist = normalize_dist(image_params)
dist = parse_dist(image_params)
limit = parse_limit(image_params)
DuplicateReports.duplicates_of(intensities, aspect, dist, dist, limit)
{intensities, aspect}
|> DuplicateReports.find_duplicates(dist: dist, aspect_dist: dist, limit: limit)
|> preload([:user, :intensity, [:sources, tags: :aliases]])
|> Repo.all()
end
@ -43,25 +44,18 @@ defmodule PhilomenaWeb.ImageReverse do
# The distance metric is taxicab distance, not Euclidean,
# because this is more efficient to index.
defp normalize_dist(%{"distance" => distance}) do
defp parse_dist(%{"distance" => distance}) do
distance
|> parse_dist()
|> max(0.01)
|> min(1.0)
end
defp normalize_dist(_dist), do: 0.25
defp parse_dist(dist) do
case Decimal.parse(dist) do
{value, _rest} ->
Decimal.to_float(value)
_ ->
0.0
|> Decimal.parse()
|> case do
{value, _rest} -> Decimal.to_float(value)
_ -> 0.25
end
|> clamp(0.01, 1.0)
end
defp parse_dist(_params), do: 0.25
defp parse_limit(%{"limit" => limit}) do
limit
|> Integer.parse()
@ -69,9 +63,12 @@ defmodule PhilomenaWeb.ImageReverse do
{limit, _rest} -> limit
_ -> 10
end
|> max(1)
|> min(50)
|> clamp(1, 50)
end
defp parse_limit(_), do: 10
defp parse_limit(_params), do: 10
defp clamp(n, min, _max) when n < min, do: min
defp clamp(n, _min, max) when n > max, do: max
defp clamp(n, _min, _max), do: n
end

View file

@ -28,6 +28,8 @@ h1 Reverse Search
br
= number_input f, :distance, value: 0.25, min: 0, max: 1, step: 0.01, class: "input"
= hidden_input f, :limit, value: 10
.field
= submit "Reverse Search", class: "button"