(* Select key for a given name using data from gbif.org *) maintainKeyGbif[name_String] := Module[{nameul, file, mdata, mocc, ranks, dat, key, kingdom, rank, synonym, status, r, occ, rank0, key0, rocc0, properNames, link}, properNames = { "Gymnopilus spectabilis" ~~ __ ~~ "Weinm"(* to exclude Phaeolepiota aurea *) }; nameul = StringReplace[name, " " -> "_"]; file = FileNameJoin[{dirSearch, nameul <> ".m"}]; If[FileExistsQ[file], Get[file]; mdata = Length[data]; If[mdata == 0, key0 = 0, mocc = (Plus @@ occurrences) + 1; ranks = Table[ dat = data[[n]]; {key, kingdom, rank, synonym, status, scientificName} = ToString /@ ({"key", "kingdom", "rank", "synonym", "status", "scientificName"} /. dat); {key, kingdom, rank, synonym, status} = ToLowerCase /@ {key, kingdom, rank, synonym, status}; r = 0; If[kingdom === "fungi", r = r + 1000.0]; If[kingdom === "protozoa", r = r + 100]; If[synonym === "false", r = r + 10]; If[status === "accepted", r = r + 5]; occ = occurrences[[n]]; rocc = N[occ/mocc]; r = r + 1000 occ/mocc; If[!StringFreeQ[scientificName, properNames, IgnoreCase -> True], r = r + 1000]; {r, key, kingdom, rank, synonym, status, scientificName, rocc}, {n, mdata}]; ranks = Sort[ranks, (Last[#1] > Last[#2]) &]; key1 = First[ranks][[2]]; ranks = Sort[ranks, (First[#1] > First[#2]) &]; rank0 = First[ranks]; key0 = rank0[[2]]; rocc0 = Last[rank0]; ranks = Prepend[ranks, {"Score", "key", "kingdom", "rank", "synonym", "status", "scientificName", "rocc"}]; If[(*key1 =!= key0*) rocc0 < 0.7 && mdata > 1 && rank0[[6]] =!= "accepted", Print["Warning: ", name, ": maintainKeyGbif: ", ranks // TableForm]]; ]; (* If[mdata == 0 *) link = "https://www.gbif.org/species/" <> ToString[key0]; keyData[key0, "link"] = link; nameKey[name] = key0 // ToExpression; key0, noPrint["Error: maintainKeyGbif: ", name, ": file ", file, " does not exist."]; 0 ]];