def answer_for_location(loc: Tuple): # Send API request res = query_geocode_api_coords(loc[0], loc[1]) # Verify that we have at least one valid result if ( not res or "results" not in res or not len(res["results"]) or not res["results"][0] ): return None # Grab top result from API call top = res["results"][0] # TODO: Fall back on lower-ranked results from the API # if the top result doesn't even contain a locality. # Extract address info from top result street, num, locality, postcode, country_code = _addrinfo_from_api_result(top) descr = "" # Special handling of Icelandic locations since we have more info # about them and street/locality names need to be declined. if country_code == "IS": # We received a street name from the API if street: descr = street_desc(street, num, locality) # We at least have a locality (e.g. "Reykjavík") elif locality: descr = _locality_desc(locality) # Only country else: descr = country_desc("IS") # The provided location is abroad. else: sdesc = ("á " + street) if street else "" if num and street: sdesc += " " + num locdesc = ( "{0} {1}".format(iceprep_for_placename(locality), locality) if locality else "" ) # "[á Boulevard St. Germain] [í París] [í Frakklandi]" descr = "{0} {1} {2}".format(sdesc, locdesc, country_desc(country_code)).strip() if not descr: # Fall back on the formatted address string provided by Google descr = "á " + top.get("formatted_address") answer = cap_first(descr) response = dict(answer=answer) voice = "Þú ert {0}".format(_addr4voice(descr)) return response, answer, voice
def _which_continent_query(subject, q): """ Generate answer to question concerning the continent on which a given country name or placename is located. """ # Get country code cc = isocode_for_country_name(subject) is_city = False if not cc: # OK, the subject is not a country # Let's see if it's a city info = location_info(subject, "placename") if not info: return False # We don't know where it is cc = info.get("country") is_city = True contcode = continent_for_country(cc) continent = ISO_TO_CONTINENT[contcode] continent_dat = nom2dat(continent) # Format answer answer = continent_dat response = dict(answer=answer) if is_city: voice = "Borgin {0} er {1}, sem er land í {2}".format( subject, country_desc(cc), continent_dat) else: voice = "Landið {0} er í {1}".format(subject, continent_dat) q.set_answer(response, answer, voice) q.set_key(subject) q.set_context(dict(subject=continent)) return True
def _which_country_query(subject: str, q: Query): """ Generate answer to question concerning the country in which a given placename is located. """ info = location_info(subject, "placename") if not info: return False cc = info.get("country") if not cc: return False # Get country name w. preposition ("í Þýskalandi") desc = country_desc(cc) # Format answer answer = cap_first(desc) response = dict(answer=answer) voice = "{0} er {1}".format(subject, desc) q.set_answer(response, answer, voice) q.set_key(subject) cname = country_name_for_isocode(cc) if cname is not None: q.set_context(dict(subject=cname)) return True
def _which_continent_query(subject: str, q: Query): """ Generate answer to question concerning the continent on which a given country name or placename is located. """ # Get country code cc = isocode_for_country_name(subject) is_placename = False if not cc: # OK, the subject is not a country # Let's see if it's a placename info = location_info(subject, "placename") if not info: return False # We don't know where it is cc = info.get("country") is_placename = True if not cc: return False contcode = continent_for_country(cc) if contcode is None: continent = "óþekkt heimsálfa" continent_dat = "óþekktri heimsálfu" else: continent = ISO_TO_CONTINENT[contcode] continent_dat = nom2dat(continent) # Format answer answer = continent_dat response = dict(answer=answer) if is_placename: cd = country_desc(cc) voice = "Staðurinn {0} er {1}, sem er land í {2}".format( subject, cd, continent_dat) answer = "{0}, {1}".format(cap_first(cd), continent_dat) else: voice = "Landið {0} er í {1}".format(subject, continent_dat) q.set_answer(response, answer, voice) q.set_key(subject) q.set_context(dict(subject=continent)) return True
def _which_country_query(subject, q): """ Generate answer to question concerning the country in which a given placename is located. """ info = location_info(subject, "placename") if not info: return False cc = info.get("country") if not cc: return False # Get country name w. preposition ("í Þýskalandi") desc = country_desc(cc) # Format answer answer = desc[0].upper() + desc[1:] response = dict(answer=answer) voice = "{0} er {1}".format(subject, desc) q.set_answer(response, answer, voice) q.set_key(subject) return True
def test_query_utility_functions(): """ Tests for various utility functions used by query modules. """ from queries import ( natlang_seq, nom2dat, numbers_to_neutral, is_plural, sing_or_plur, country_desc, cap_first, time_period_desc, distance_desc, krona_desc, strip_trailing_zeros, iceformat_float, icequote, timezone4loc, # parse_num, ) assert natlang_seq(["Jón", "Gunna"]) == "Jón og Gunna" assert natlang_seq(["Jón", "Gunna", "Siggi"]) == "Jón, Gunna og Siggi" assert ( natlang_seq(["Jón", "Gunna", "Siggi"], oxford_comma=True) == "Jón, Gunna, og Siggi" ) assert nom2dat("hestur") == "hesti" assert nom2dat("Hvolsvöllur") == "Hvolsvelli" # assert parse_num("11") == 11 # assert parse_num("17,33") == 17.33 assert numbers_to_neutral("Öldugötu 4") == "Öldugötu fjögur" assert numbers_to_neutral("Fiskislóð 31") == "Fiskislóð þrjátíu og eitt" assert is_plural(22) assert is_plural(11) assert is_plural("76,3") assert is_plural(27.6) assert is_plural("19,11") assert not is_plural("276,1") assert not is_plural(22.1) assert not is_plural(22.41) assert sing_or_plur(21, "maður", "menn") == "21 maður" assert sing_or_plur(11, "köttur", "kettir") == "11 kettir" assert sing_or_plur(2.11, "króna", "krónur") == "2,11 krónur" assert sing_or_plur(172, "einstaklingur", "einstaklingar") == "172 einstaklingar" assert sing_or_plur(72.1, "gráða", "gráður") == "72,1 gráða" assert country_desc("DE") == "í Þýskalandi" assert country_desc("es") == "á Spáni" assert country_desc("IS") == "á Íslandi" assert country_desc("us") == "í Bandaríkjunum" assert cap_first("yolo") == "Yolo" assert cap_first("YOLO") == "YOLO" assert cap_first("Yolo") == "Yolo" assert time_period_desc(3751) == "1 klukkustund og 3 mínútur" assert ( time_period_desc(3751, omit_seconds=False) == "1 klukkustund, 2 mínútur og 31 sekúnda" ) assert time_period_desc(601) == "10 mínútur" assert time_period_desc(610, omit_seconds=False) == "10 mínútur og 10 sekúndur" assert time_period_desc(61, omit_seconds=False) == "1 mínúta og 1 sekúnda" assert ( time_period_desc(121, omit_seconds=False, case="þgf") == "2 mínútum og 1 sekúndu" ) assert distance_desc(1.1) == "1,1 kílómetri" assert distance_desc(1.2) == "1,2 kílómetrar" assert distance_desc(0.7) == "700 metrar" assert distance_desc(0.021) == "20 metrar" assert distance_desc(41, case="þf") == "41 kílómetra" assert distance_desc(0.215, case="þgf") == "220 metrum" assert krona_desc(361) == "361 króna" assert krona_desc(28) == "28 krónur" assert krona_desc(4264.2) == "4.264,2 krónur" assert krona_desc(2443681.1) == "2.443.681,1 króna" assert strip_trailing_zeros("17,0") == "17" assert strip_trailing_zeros("219.117,0000") == "219.117" assert strip_trailing_zeros("170") == "170" assert strip_trailing_zeros("170,0") == "170" assert iceformat_float(666.0) == "666" assert iceformat_float(666, strip_zeros=False) == "666,00" assert iceformat_float(217.296) == "217,3" assert iceformat_float(2528963.9) == "2.528.963,9" assert iceformat_float(123.12341, decimal_places=4) == "123,1234" assert iceformat_float(123.1000, strip_zeros=True) == "123,1" assert iceformat_float(123.0, decimal_places=4, strip_zeros=False) == "123,0000" assert icequote("sæll") == "„sæll“" assert icequote(" Góðan daginn ") == "„Góðan daginn“" assert timezone4loc((64.157202, -21.948536)) == "Atlantic/Reykjavik" assert timezone4loc((40.093368, 57.000067)) == "Asia/Ashgabat"