{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "7edbd867", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['french lavender', 'french lavender', 'french lavender', 'french lavender', 'french lavender', 'italian lavender', 'italian lavender']\n" ] } ], "source": [ "import requests\n", "def get_common_names_gbif(scientific_name: str) -> list[str]:\n", " \"\"\"Get all common names for a scientific name via GBIF.\"\"\"\n", " # Step 1: get GBIF taxon key\n", " r = requests.get(\n", " \"https://api.gbif.org/v1/species/match\",\n", " params={\"name\": scientific_name, \"strict\": False}\n", " )\n", " key = r.json().get(\"usageKey\")\n", " if not key:\n", " return []\n", "\n", " # Step 2: get vernacular names\n", " r2 = requests.get(f\"https://api.gbif.org/v1/species/{key}/vernacularNames\")\n", " names = r2.json().get(\"results\", [])\n", " return [n[\"vernacularName\"].lower() for n in names if n.get(\"language\") == \"eng\"]\n", "\n", "# Example:\n", "print(get_common_names_gbif(\"Circium vulgare\")) " ] }, { "cell_type": "code", "execution_count": null, "id": "67faa7dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lavender\n" ] } ], "source": [ "# import growth database \n", "import pandas as pd\n", "growth_df = pd.read_csv(\"../data/growth_csv/growth.csv\")\n", "\n", "from rapidfuzz import process\n", "\n", "# identify scientific name with the common name in the growth database\n", "def find_common_name(scientific_name: str) -> str:\n", " \"\"\"Find the common name for a scientific name in the growth database.\"\"\"\n", " common_names = get_common_names_gbif(scientific_name)\n", " for name in common_names:\n", " \n", " # check if it matches even partially with the common name in the growth database using fuzzy matching\n", " match = process.extractOne(name, growth_df[\"Plant Name\"], score_cutoff=80)\n", " if match:\n", " return match[0] # return the matched common name from the growth database\n", " else:\n", " for word in name.split():\n", " print(f\" Checking if '{word}' is in growth database common names...\")\n", " match = process.extractOne(word, growth_df[\"Plant Name\"], score_cutoff=80)\n", " if match:\n", " print(f\" Found a match for '{word}': '{match[0]}' with score {match[1]}\")\n", " return match[0] # return the matched common name from the growth database\n", " \n", " return None\n", "\n", "print(find_common_name(\"Circium vulgare\")) # should return \"Spear Thistle\"\n", "\n", "def get_growth_info(scientific_name: str) -> dict:\n", " common_name = find_common_name(scientific_name)\n", " if not common_name:\n", " return {}\n", " return growth_df[growth_df[\"Plant Name\"] == common_name].iloc[0].to_dict()\n", "\n", "# print(get_growth_info(\"Lavandula stoechas\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "4e41ad2a", "metadata": {}, "outputs": [], "source": [ "from geo import city_to_coordinates" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.5" } }, "nbformat": 4, "nbformat_minor": 5 }