From 9fe768b8c4d25a344ab04ec2a14978a4f1dfe891 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 13 Jan 2025 12:37:38 +0100 Subject: [PATCH] do not show common name text when no common names exist --- lib/plantnet/parser.ex | 15 ++++++- test/plantnet/parser_test.exs | 9 +++++ test/test_helper.exs | 76 +++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/lib/plantnet/parser.ex b/lib/plantnet/parser.ex index 2604cce..9140d7e 100644 --- a/lib/plantnet/parser.ex +++ b/lib/plantnet/parser.ex @@ -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 = diff --git a/test/plantnet/parser_test.exs b/test/plantnet/parser_test.exs index 344c483..cef558b 100644 --- a/test/plantnet/parser_test.exs +++ b/test/plantnet/parser_test.exs @@ -53,5 +53,14 @@ defmodule PlantIdDiscordBotTest.PlantNet.Parser do assert message == "My best guess is **Prunus cerasifera** with a confidence of **88%**. Common names include **Cherry plum, myrobalan, Cherry Plum, Purple-leaf Plum**.\n\nSpecies info from plant databases:\n[GBIF]() | [PFAF]() | [POWO]()\n\nAlternatives include **Prunus × cistena**." end + + test "no common names" do + message = + PlantNetFixtures.parsed_response_with_urls_no_alternatives() + |> Parser.generate_response_message() + + assert message == + "My best guess is **Prunus cerasifera** with a confidence of **88%**.\n\nSpecies info from plant databases:\n[GBIF]() | [PFAF]() | [POWO]()\n\nConservation status: Data Deficient\n\nAlternatives include **Prunus × cistena**." + end end end diff --git a/test/test_helper.exs b/test/test_helper.exs index b7a8b28..c6a7944 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -421,6 +421,79 @@ defmodule PlantNetFixtures do "version" => "2024-11-19 (7.3)" } + @plantnet_parsed_response_with_urls_no_alternatives %{ + "bestMatch" => "Prunus cerasifera Ehrh.", + "language" => "en", + "preferedReferential" => "k-world-flora", + "query" => %{ + "images" => [ + "https://upload.wikimedia.org/wikipedia/commons/f/ff/Prunus_cerasifera_A.jpg", + "https://le-jardin-de-pascal.com/2195113-large_default/prunus-cerasifera-atropurpurea-prunier-myrobolan-nigra.jpg" + ], + "includeRelatedImages" => false, + "noReject" => false, + "organs" => ["auto", "auto"], + "project" => "all" + }, + "remainingIdentificationRequests" => 488, + "results" => [ + %{ + "gbif" => %{"id" => "3021730"}, + "gbif_url" => "https://www.gbif.org/species/3021730", + "iucn" => %{"category" => "DD", "id" => "172162"}, + "pfaf_url" => "https://pfaf.org/user/Plant.aspx?LatinName=/Prunus+cerasifera", + "powo" => %{"id" => "729568-1"}, + "powo_url" => "https://powo.science.kew.org/taxon/729568-1", + "score" => 0.87871, + "species" => %{ + "commonNames" => [], + "family" => %{ + "scientificName" => "Rosaceae", + "scientificNameAuthorship" => "", + "scientificNameWithoutAuthor" => "Rosaceae" + }, + "genus" => %{ + "scientificName" => "Prunus", + "scientificNameAuthorship" => "", + "scientificNameWithoutAuthor" => "Prunus" + }, + "scientificName" => "Prunus cerasifera Ehrh.", + "scientificNameAuthorship" => "Ehrh.", + "scientificNameWithoutAuthor" => "Prunus cerasifera" + } + }, + %{ + "gbif" => %{"id" => "3022465"}, + "gbif_url" => "https://www.gbif.org/species/3022465", + "pfaf_url" => "https://pfaf.org/user/Plant.aspx?LatinName=/Prunus+×+cistena", + "powo" => %{"id" => "2959315-4"}, + "powo_url" => "https://powo.science.kew.org/taxon/2959315-4", + "score" => 0.31668, + "species" => %{ + "commonNames" => [ + "Dwarf red-leaf plum", + "Purple-leaf sand cherry", + "Purple-leaved sand cherry" + ], + "family" => %{ + "scientificName" => "Rosaceae", + "scientificNameAuthorship" => "", + "scientificNameWithoutAuthor" => "Rosaceae" + }, + "genus" => %{ + "scientificName" => "Prunus", + "scientificNameAuthorship" => "", + "scientificNameWithoutAuthor" => "Prunus" + }, + "scientificName" => "Prunus × cistena N.E.Hansen ex Koehne", + "scientificNameAuthorship" => "N.E.Hansen ex Koehne", + "scientificNameWithoutAuthor" => "Prunus × cistena" + } + } + ], + "version" => "2024-11-19 (7.3)" + } + def raw_response, do: @plantnet_raw_response def parsed_response, do: @plantnet_parsed_response def parsed_response_filtered_by_score, do: @plantnet_parsed_response_filtered_by_score @@ -430,4 +503,7 @@ defmodule PlantNetFixtures do do: @plantnet_parsed_response_filtered_by_score_no_iucn def parsed_response_with_urls_no_iucn, do: @plantnet_parsed_response_with_urls_no_iucn + + def parsed_response_with_urls_no_alternatives, + do: @plantnet_parsed_response_with_urls_no_alternatives end