From 79f508f6031e7ccec54c0a39a8c2ea9449d7f6eb Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Jul 2024 08:50:16 -0400 Subject: [PATCH] Improve readability of duplicate report frontend parsing / generation --- lib/philomena/duplicate_reports.ex | 9 ++++- lib/philomena_web/image_reverse.ex | 37 +++++++++---------- .../templates/search/reverse/index.html.slime | 2 + 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex index d802f364..9bf85cdb 100644 --- a/lib/philomena/duplicate_reports.ex +++ b/lib/philomena/duplicate_reports.ex @@ -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 diff --git a/lib/philomena_web/image_reverse.ex b/lib/philomena_web/image_reverse.ex index 2ef5e427..e8e32f85 100644 --- a/lib/philomena_web/image_reverse.ex +++ b/lib/philomena_web/image_reverse.ex @@ -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 diff --git a/lib/philomena_web/templates/search/reverse/index.html.slime b/lib/philomena_web/templates/search/reverse/index.html.slime index 876be913..d8b934df 100644 --- a/lib/philomena_web/templates/search/reverse/index.html.slime +++ b/lib/philomena_web/templates/search/reverse/index.html.slime @@ -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"