# import growth database import pandas as pd from rapidfuzz import process import requests growth_df = pd.read_csv("../data/growth_csv/growth.csv") def get_common_names_gbif(scientific_name: str) -> list[str]: """Get all common names for a scientific name via GBIF.""" # Step 1: get GBIF taxon key r = requests.get( "https://api.gbif.org/v1/species/match", params={"name": scientific_name, "strict": False} ) key = r.json().get("usageKey") if not key: return [] # Step 2: get vernacular names r2 = requests.get(f"https://api.gbif.org/v1/species/{key}/vernacularNames") names = r2.json().get("results", []) return [n["vernacularName"].lower() for n in names if n.get("language") == "eng"] # identify scientific name with the common name in the growth database def find_common_name_match(scientific_name: str) -> str: """Find the common name for a scientific name in the growth database.""" common_names = get_common_names_gbif(scientific_name) for name in common_names: # check if it matches even partially with the common name in the growth database using fuzzy matching match = process.extractOne(name, growth_df["Plant Name"], score_cutoff=80) if match: return match[0] # return the matched common name from the growth database else: for word in name.split(): print(f" Checking if '{word}' is in growth database common names...") match = process.extractOne(word, growth_df["Plant Name"], score_cutoff=80) if match: print(f" Found a match for '{word}': '{match[0]}' with score {match[1]}") return match[0] # return the matched common name from the growth database return None print(find_common_name_match("Circium vulgare")) # should return "Spear Thistle" def get_growth_info(scientific_name: str) -> dict: common_name = find_common_name_match(scientific_name) if not common_name: return {} return growth_df[growth_df["Plant Name"] == common_name].iloc[0].to_dict()