do not show common name text when no common names exist

This commit is contained in:
Owen
2025-01-13 12:37:38 +01:00
committed by Owen Rees
parent d68f4abf4b
commit 9fe768b8c4
3 changed files with 98 additions and 2 deletions
+13 -2
View File
@@ -82,7 +82,7 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do
best_result_iucn_category = best_result["iucn"]["category"]
score = round(best_result["score"] * 100) |> Integer.to_string()
"My best guess is **#{best_guess_name}** with a confidence of **#{score}%**. Common names include **#{Enum.join(best_result["species"]["commonNames"], ", ")}**.\n\nSpecies info from plant databases:\n[GBIF](<#{best_result["gbif_url"]}>) | [PFAF](<#{best_result["pfaf_url"]}>) | [POWO](<#{best_result["powo_url"]}>)#{if best_result_iucn_category, do: "\n\nConservation status: #{iucn_parser(best_result_iucn_category)}"}#{get_alternatives(other_results)}"
"My best guess is **#{best_guess_name}** with a confidence of **#{score}%**.#{get_common_names(best_result)}\n\nSpecies info from plant databases:\n[GBIF](<#{best_result["gbif_url"]}>) | [PFAF](<#{best_result["pfaf_url"]}>) | [POWO](<#{best_result["powo_url"]}>)#{if best_result_iucn_category, do: "\n\nConservation status: #{iucn_parser(best_result_iucn_category)}"}#{get_alternatives(other_results)}"
end
@spec generate_gbif_url(map()) :: map()
@@ -112,7 +112,17 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do
end)
end
@spec get_alternatives(map()) :: String.t()
@spec get_common_names(map()) :: String.t()
defp get_common_names(data) do
common_names = data["species"]["commonNames"]
if length(common_names) > 0 do
" Common names include **" <>
Enum.join(common_names, ", ") <> "**."
end
end
@spec iucn_parser(String.t()) :: String.t()
defp iucn_parser(abbreviation) do
case abbreviation do
"DD" -> "Data Deficient"
@@ -128,6 +138,7 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do
end
end
@spec get_alternatives(map()) :: String.t()
defp get_alternatives(data) do
if length(data) > 0 do
alternatives =