def street_desc(street_nom: str, street_num: int, locality_nom: str) -> str: """ Generate description of being on a particular (Icelandic) street with correct preposition and case + locality e.g. 'á Fiskislóð 31 í Reykjavík'. """ street_dat = None locality_dat = None # Start by looking up address in staðfangaskrá to get # the dative case of street name and locality. # This works better than BÍN lookup since not all street # names are present in BÍN. addrinfo = iceaddr_lookup(street_nom, placename=locality_nom, limit=1) if len(addrinfo): street_dat = addrinfo[0]["heiti_tgf"] if locality_nom and locality_nom == addrinfo[0]["stadur_nf"]: locality_dat = addrinfo[0]["stadur_tgf"] # OK, if staðfangaskrá can't help us, try to use BÍN to # get dative version of name. Some names given by Google's # API are generic terms such as "Göngustígur" and the like. if not street_dat: street_dat = nom2dat(street_nom) if not locality_dat: locality_dat = nom2dat(locality_nom) # Create street descr. ("á Fiskislóð 31") street_comp = iceprep_for_street(street_nom) + " " + street_dat if street_num: street_comp += " " + str(street_num) # Append locality if available ("í Reykjavík") if locality_dat: ldesc = iceprep_for_placename(locality_nom) + " " + locality_dat street_comp += " " + ldesc return street_comp
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 _loc_desc_query(subject: str, q: Query): """ Generate answer to a question about where a country or placename is located. """ # Get country code cc = isocode_for_country_name(subject) if not cc: # Not a country, try placename lookup return _which_country_query(subject, q) 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) answer = "{0} er land í {1}.".format(subject, continent_dat) voice = answer response = dict(answer=answer) q.set_answer(response, answer, voice) q.set_key(subject) q.set_context(dict(subject=subject)) 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 _locality_desc(locality_nom: str) -> str: """ Return an appropriate preposition plus a locality name in dative case """ locality_dat = nom2dat(locality_nom) return iceprep_for_placename(locality_nom) + " " + locality_dat
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"