# Performance gains by avoiding the interpreter function

I have this.

data = ResourceData["Epidemic Data for Novel Coronavirus COVID-19",
"USCounties"];

usaCountyCases[s_, c_] :=
data[Select[MatchQ[Interpreter["USState"][s], #State] &]][
Select[MatchQ[
Interpreter["USCounty"][c <> " county, " <> s], #County] &]][
All, #ConfirmedCases["LastValue"] &] // Normal // First

usaCountyCases["California", "Alpine"]


gives 124 (as of today). How do I speed this up? TIA.

• Replace the Interpreter bit with Entity["AdministrativeDivision", {s, "UnitedStates"}]? You will lose some flexibility though: for instance, North Dakota will have to be entered as "NorthDakota" (no space). Jul 26 at 2:08
• Thanks. And how would you replace Interpreter for the counties?
– atat
Jul 26 at 3:38
• One more thing of note: It appears that this dataset is no longer actively updated, having been last updated in January 2022. Jul 26 at 18:08
• How do I get the updated coronavirus then?
– atat
Jul 30 at 10:27
• I don't know. CDC? WHO maybe? I'm not sure if or where updated data is available. Jul 30 at 11:16

1. You don't need to filter twice, by state and then by county, since the county already contains the state information.
2. The following modification to your function stuffs the string with the name of the county and the state into the appropriate Entity form, avoiding the calls to Interpreter.
3. The input strings are sanitized to remove spaces, so you can enter, for instance, "North Dakota", and the function will know to use "NorthDakota" in its query.
4. I've added the dataset as a parameter to your function so it is not hard-coded and you can use it on different datasets (e.g. subsets of the original).
ClearAll[usaCountyCases]
usaCountyCases[state_String, county_String, dataset_Dataset] := Module[{c},
c = Entity[
{
StringReplace[county, " " -> ""] <> "County",
StringReplace[state, " " -> ""], "UnitedStates"
}
];
First[#, #]& @
Normal@
Query[
Select[#County == c &],
#ConfirmedCases["LastValue"]&
][dataset]
]

usaCountyCases["California", "Alpine", data]
(* 124 *)

usaCountyCases["North Dakota", "Cass", data]
(* 54993 *)


From a performance perspective, these calls returned results practically instantaneously when I tried them.