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"
def handle_plain_text(q): """ Handle a plain text query, contained in the q parameter which is an instance of the query.Query class. Returns True if the query was handled, and in that case the appropriate properties on the Query instance have been set, such as the answer and the query type (qtype). If the query is not recognized, returns False. """ ql = q.query_lower.rstrip("?") # Timezone being asked about tz = None # Whether user asked for the time in a particular location specific_desc = None if ql in _TIME_QUERIES: # Use location to determine time zone tz = timezone4loc(q.location, fallback="IS") elif ql.startswith("hvað er klukkan á ") or ql.startswith( "hvað er klukkan í "): # Query about the time in a particular location, i.e. country or city loc = ql[18:] # Cut away question prefix, leaving only placename # Capitalize each word in country/city name loc = capitalize_placename(loc) # Look up nominative # This only works for single-word city/country names found # in BÍN and could be improved (e.g. fails for "Nýju Jórvík") bin_res = BIN_Db().lookup_nominative(loc) words = [m.stofn for m in bin_res] words.append( loc) # In case it's not in BÍN (e.g. "New York", "San José") # Check if any word is a recognised country or city name for w in words: cc = isocode_for_country_name(w) if cc and cc in country_timezones: # Look up country timezone # Use the first timezone although some countries have more than one # The timezone list returned by pytz is ordered by "dominance" tz = country_timezones[cc][0] else: # It's not a country name, look up in city database info = lookup_city_info(w) if info: top = info[0] location = (top.get("lat_wgs84"), top.get("long_wgs84")) tz = timezone4loc(location) if tz: # We have a timezone break # "Klukkan í Lundúnum er" - Used for voice answer specific_desc = "{0} er".format(ql[8:]) # Beautify query by capitalizing the country/city name q.set_beautified_query("{0}{1}?".format(q.beautified_query[:18], loc)) # We have a timezone. Return formatted answer. if tz: now = datetime.now(timezone(tz)) desc = specific_desc or "Klukkan er" # Create displayable answer answer = "{0:02}:{1:02}".format(now.hour, now.minute) # A detailed response object is usually a list or a dict response = dict(answer=answer) # A voice answer is a plain string that will be # passed as-is to a voice synthesizer voice = "{0} {1}:{2:02}.".format(desc, now.hour, now.minute) q.set_qtype(_TIME_QTYPE) q.set_key(tz) # Query key is the timezone q.set_answer(response, answer, voice) return True return False
def handle_plain_text(q: Query) -> bool: """Handle a plain text query, contained in the q parameter which is an instance of the query.Query class. Returns True if the query was handled, and in that case the appropriate properties on the Query instance have been set, such as the answer and the query type (qtype). If the query is not recognized, returns False.""" ql = q.query_lower.rstrip("?") # Timezone being asked about tz = None # Whether user asked for the time in a particular location specific_desc = None if ql in _TIME_QUERIES: # Use location to determine time zone tz = timezone4loc(q.location, fallback="IS") else: locq = [x for x in _TIME_IN_LOC_QUERIES if ql.startswith(x.lower())] if not locq: return False # Not matching any time queries # This is a query about the time in a particular location, i.e. country or city # Cut away question prefix, leaving only loc name loc = ql[len(locq[0]):].strip() if not loc: return False # No location string # Intelligently capitalize country/city/location name loc = capitalize_placename(loc) # Look up nominative loc_nom = NounPhrase(loc).nominative or loc prep = "í" # Check if loc is a recognised country or city name cc = isocode_for_country_name(loc_nom) if cc and cc in country_timezones: # Look up country timezone # Use the first timezone although some countries have more than one # The timezone list returned by pytz is ordered by "dominance" tz = country_timezones[cc][0] prep = iceprep_for_cc(cc) else: # It's not a country name, look up in city database info = lookup_city_info(loc_nom) if info: top = info[0] location = ( cast(float, top.get("lat_wgs84")), cast(float, top.get("long_wgs84")), ) tz = timezone4loc(location) prep = iceprep_for_placename(loc_nom) if tz: # "Klukkan í Lundúnum er" - Used for voice answer dat = NounPhrase(loc_nom).dative or loc specific_desc = "Klukkan {0} {1} er".format(prep, dat) else: # Unable to find the specified location q.set_qtype(_TIME_QTYPE) q.set_key(loc) q.set_answer( *gen_answer("Ég gat ekki flett upp staðsetningunni {0}".format( icequote(loc)))) return True # We have a timezone. Return formatted answer. if tz: now = datetime.now(timezone(tz)) desc = specific_desc or "Klukkan er" # Create displayable answer answer = "{0:02}:{1:02}".format(now.hour, now.minute) # A detailed response object is usually a list or a dict response = dict(answer=answer) # A voice answer is a plain string that will be # passed as-is to a voice synthesizer voice = "{0} {1}:{2:02}.".format(desc, now.hour, now.minute) q.set_qtype(_TIME_QTYPE) q.set_key(tz) # Query key is the timezone q.set_answer(response, answer, voice) return True return False