4141import net .b07z .sepia .server .core .tools .JSON ;
4242import net .b07z .sepia .server .core .tools .Sdk ;
4343
44+ /**
45+ * Service that loads Covid-19 data via the public ECDC API and generates info messages presenting total cases, deaths and recent cases for a specific country (or "world" summary).
46+ *
47+ * @author Florian Quirin
48+ *
49+ */
4450public class CoronaDataEcdc implements ServiceInterface {
4551
4652 //USED: https://opendata.ecdc.europa.eu/covid19/casedistribution/csv/
@@ -77,7 +83,7 @@ public ServiceAnswers getAnswersPool(String language) {
7783 if (language .equals (LANGUAGES .DE )){
7884 answerPool
7985 .addAnswer (successAnswer , 0 , "Hier sind die aktuellen, weltweiten Corona Zahlen des ECDC. "
80- + "Fälle insgesamt: <2>. Tote insgesamt: <3>. Neue Fälle: <4>. " )
86+ + "Fälle insgesamt: <2>. Tote insgesamt: <3>." )
8187 .addAnswer (successWithCountry , 0 , "Hier sind die aktuellen Corona Zahlen des ECDC für <1>. "
8288 + "Fälle insgesamt: <2>. Tote insgesamt: <3>. Neue Fälle: <4>." )
8389 .addAnswer (okAnswer , 0 , "Die Anfrage ist angekommen aber ich kann sie nicht bearbeiten." )
@@ -88,7 +94,7 @@ public ServiceAnswers getAnswersPool(String language) {
8894 }else {
8995 answerPool
9096 .addAnswer (successAnswer , 0 , "Here are the recent, worldwide Corona numbers from the ECDC. "
91- + "Total cases: <2>. Total deaths: <3>. New cases: <4>. " )
97+ + "Total cases: <2>. Total deaths: <3>." )
9298 .addAnswer (successWithCountry , 0 , "Here are the recent ECDC Corona numbers for <1>. "
9399 + "Total cases: <2>. Total deaths: <3>. New cases: <4>." )
94100 .addAnswer (okAnswer , 0 , "Message received but I could not fulfill your request." )
@@ -159,6 +165,7 @@ public ServiceResult getResult(NluResult nluResult){
159165
160166 //get optional parameters:
161167
168+ //this is the custom 'CountryCode3' parameter defined below
162169 Parameter countryCodeP = nluResult .getRequiredParameter (CountryCode3 .class .getName ());
163170 String threeLetterCode = countryCodeP .getValueAsString ();
164171 String countryLocalName = (String ) countryCodeP .getDataFieldOrDefault (InterviewData .VALUE_LOCAL );
@@ -169,7 +176,7 @@ public ServiceResult getResult(NluResult nluResult){
169176 countryFilter = threeLetterCode ;
170177 }
171178
172- //test call
179+ //get data
173180 try {
174181 JSONObject data = getCovid19Data (countryFilter );
175182 if (Is .nullOrEmpty (data )){
@@ -187,11 +194,13 @@ public ServiceResult getResult(NluResult nluResult){
187194 }catch (Exception e ){
188195 recentNewCases = defaultLocalUnknown ;
189196 }
197+ //set all answer parameters
190198 service .resultInfoPut ("country" , countryLocalName );
191199 service .resultInfoPut ("cases" , Converters .obj2StringOrDefault (data .get ("cases" ), defaultLocalUnknown ));
192200 service .resultInfoPut ("deaths" , Converters .obj2StringOrDefault (data .get ("deaths" ), defaultLocalUnknown ));
193201 service .resultInfoPut ("new_cases" , recentNewCases );
194202
203+ //choose the country or "world" answer
195204 if (countryFilter != null ){
196205 service .setCustomAnswer (successWithCountry );
197206 }
@@ -219,6 +228,7 @@ public ServiceResult getResult(NluResult nluResult){
219228 */
220229 public static class CountryCode3 extends CustomParameter {
221230
231+ //Regular expressions for the supported countries in DE and EN:
222232 private static String gbrRegEx_en = "english|england|britain|british|united kongdom|uk|gbr|gb" ;
223233 private static String gbrRegEx_de = "englisch(en|e|)|england|(gross|)britannien|britisch(en|e|)|uk|vereinigtes koenigreich|uk|gbr|gb" ;
224234
@@ -261,9 +271,11 @@ public String extract(String input) {
261271
262272 //German
263273 if (this .language .equals (LANGUAGES .DE )){
274+ //find any of the supported countries
264275 found = NluTools .stringFindFirst (input , all_de );
265276 if (Is .nullOrEmpty (found )){
266277 return "" ;
278+ //construct the country result with ISO code + local name
267279 }else if (NluTools .stringContains (found , gbrRegEx_de )){
268280 extracted = "GBR;;England;;" ;
269281 }else if (NluTools .stringContains (found , deuRegEx_de )){
@@ -279,9 +291,11 @@ public String extract(String input) {
279291 }
280292 //English
281293 }else if (this .language .equals (LANGUAGES .EN )){
294+ //find any of the supported countries
282295 found = NluTools .stringFindFirst (input , all_en );
283296 if (Is .nullOrEmpty (found )){
284297 return "" ;
298+ //construct the country result with ISO code + local name
285299 }else if (NluTools .stringContains (found , gbrRegEx_en )){
286300 extracted = "GBR;;England;;" ;
287301 }else if (NluTools .stringContains (found , deuRegEx_en )){
0 commit comments