def sentence(state: QueryStateDict, result: Result) -> None: """ Called when sentence processing is complete """ q: Query = state["query"] if "qtype" in result and result.qtype == "Unit": # Successfully matched a query type # If no number is mentioned in the query # ('Hvað eru margir metrar í kílómetra?') # we use 1.0 val_from = result.get("number", 1.0) # Convert between units, getting also the base SI unit and quantity valid, val, si_unit, si_quantity = _convert( val_from, result.unit_from, result.unit_to ) if not valid: answer = voice_answer = "Það er ekki hægt að umbreyta {0} í {1}.".format( to_dative(result.unit_from), to_accusative(result.unit_to) ) response = dict(answer=answer) else: answer = iceformat_float(val) if (0.0 < val < 1.0e-3) or (val > 0.0 and answer == "0"): answer = "næstum núll " + result.unit_to_nf val = 0.0 else: answer += " " + result.unit_to_nf verb = "eru" if int(val_from) % 10 == 1 and int(val_from) % 100 != 11: # 'Einn lítri er...', 'Tuttugu og einn lítri er...', # but on the other hand 'Ellefu lítrar eru...' verb = "er" elif "hálf" in result.desc: # Hack to reply 'Hálfur kílómetri er 500 metrar' verb = "er" unit_to = result.unit_to response = dict(answer=answer) voice_answer = "{0} {1} {2}.".format(result.desc, verb, answer).capitalize() # Store the resulting quantity in the query context q.set_context( { "quantity": { "unit": unit_to, "value": val, "si_unit": si_unit, "si_value": si_quantity, } } ) q.set_key(result.unit_to) q.set_answer(response, answer, voice_answer) q.set_qtype(_UNIT_QTYPE) q.lowercase_beautified_query() return q.set_error("E_QUERY_NOT_UNDERSTOOD")
def to_accusative(np: str) -> str: """ Return the noun phrase after casting it from nominative to accusative case """ np = straeto.BusStop.voice(np) return query.to_accusative(np, filter_func=_filter_func)