Skip to content

Commit

Permalink
Added invocation to inquire about individual dishes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcTM01 committed May 31, 2024
1 parent a41b071 commit 7afd073
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 172 deletions.
8 changes: 4 additions & 4 deletions alexa-skill/lambda-pdm/src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
CatchAllExceptionHandler,
)
from src.handler.fallback_intent_handler import FallbackIntentHandler
from src.handler.get_filtered_mensa_offerings_intent_handler import (
GetFilteredMensaOfferingsIntentHandler,
)
from src.handler.get_mensa_offerings_intent_handler import (
GetMensaOfferingsIntentHandler,
)
from src.handler.get_vegan_or_vegetarian_mensa_offerings_intent_handler import (
GetVeganOrVegetarianMensaOfferingsIntentHandler,
)
from src.handler.help_intent_handler import (
HelpIntentHandler,
)
Expand All @@ -38,7 +38,7 @@
sb.add_request_handler(SessionEndedRequestHandler())
sb.add_request_handler(FallbackIntentHandler())

sb.add_request_handler(GetVeganOrVegetarianMensaOfferingsIntentHandler())
sb.add_request_handler(GetFilteredMensaOfferingsIntentHandler())
sb.add_request_handler(GetMensaOfferingsIntentHandler())

sb.add_exception_handler(CatchAllExceptionHandler())
Expand Down
58 changes: 53 additions & 5 deletions alexa-skill/lambda-pdm/src/locales/de-DE/LC_MESSAGES/skill.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ msgstr "Entschuldigung, ich kann diese Anfrage nicht verarbeiten. Dies kann dara
msgid "NO_MENSA_SPECIFIED"
msgstr "Entschuldigung, ich habe nicht verstanden, nach welcher Mensa Sie gefragt haben. Bitte versuchen Sie es erneut."

msgid "MULTIPLE_MENSAS_SPECIFIED"
msgstr "Bitte geben Sie genau eine Mensa an. Ich konnte mich nicht zwischen {0} entscheiden. Bitte versuchen Sie es erneut."
msgid "NO_DISH_TYPE_SPECIFIED"
msgstr "Entschuldigung, ich habe nicht verstanden, nach welchem Gericht Sie gefragt haben. Bitte versuchen Sie es erneut."

msgid "NO_DATE_SPECIFIED"
msgstr "Entschuldigung, ich habe nicht verstanden, nach welchem Datum Sie gefragt haben. Bitte versuchen Sie es erneut."
Expand Down Expand Up @@ -94,13 +94,13 @@ msgid "vegan"
msgstr "vegan"

msgid "FILTERED_DISH_ANNOUNCEMENT_NO_DISHES"
msgstr "Es gibt leider keine {type}en Gerichte in der {mensa} am {date}."
msgstr "Es gibt leider keine {type} in der {mensa} am {date}."

msgid "FILTERED_DISH_ANNOUNCEMENT_ONE_DISH"
msgstr "Es gibt ein {type}es Gericht in der {mensa} am {date}: {dish}."
msgstr "Es gibt ein {type} in der {mensa} am {date}: {dish}."

msgid "FILTERED_DISH_ANNOUNCEMENT_MULTIPLE_DISHES"
msgstr "Es gibt {num} {type}e Gerichte in der {mensa} am {date}: {dishes}."
msgstr "Es gibt {num} {type} in der {mensa} am {date}: {dishes}."

# Annoucement for dish types
msgid "ANNOUNCEMENT_PREPOSITION_TABLE_DISH"
Expand Down Expand Up @@ -193,3 +193,51 @@ msgstr "Samstag"
msgid "DAY_6"
msgstr "Sonntag"

# Dish Type Filter
msgid "FILTER_VEGETARIAN_SINGLE"
msgstr "vegetarisches Gericht"

msgid "FILTER_VEGETARIAN_PLURAL"
msgstr "vegetarische Gerichte"

msgid "FILTER_VEGAN_SINGLE"
msgstr "veganes Gericht"

msgid "FILTER_VEGAN_PLURAL"
msgstr "vegane Gerichte"

msgid "FILTER_CLASSICS_SINGLE"
msgstr "Klassiker"

msgid "FILTER_CLASSICS_PLURAL"
msgstr "Klassiker"

msgid "FILTER_PASTA_SINGLE"
msgstr "Nudelgericht"

msgid "FILTER_PASTA_PLURAL"
msgstr "Nudelgerichte"

msgid "FILTER_WOK_SINGLE"
msgstr "Wokgericht"

msgid "FILTER_WOK_PLURAL"
msgstr "Wokgerichte"

msgid "FILTER_PIZZA_SINGLE"
msgstr "Pizza"

msgid "FILTER_PIZZA_PLURAL"
msgstr "Pizzen"

msgid "FILTER_BURGER_SINGLE"
msgstr "Burger"

msgid "FILTER_BURGER_PLURAL"
msgstr "Burger"

msgid "FILTER_TABLE_DISH_SINGLE"
msgstr "Tellergericht"

msgid "FILTER_TABLE_DISH_PLURAL"
msgstr "Tellergerichte"
61 changes: 55 additions & 6 deletions alexa-skill/lambda-pdm/src/locales/en-US/LC_MESSAGES/skill.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ msgstr "Sorry, I can't process this request. This may be because I don't yet kno
msgid "NO_MENSA_SPECIFIED"
msgstr "I'm sorry, I didn't understand which mensa you were asking about. Please try again."

msgid "MULTIPLE_MENSAS_SPECIFIED"
msgstr "Please specify exactly one mensa. I could not decide between {0}. Do Please try again."
msgid "NO_DISH_TYPE_SPECIFIED"
msgstr "I'm sorry, I didn't understand which dish you were asking about. Please try again."

msgid "NO_DATE_SPECIFIED"
msgstr "I'm sorry, I didn't understand which date you were asking about. Please try again."
Expand Down Expand Up @@ -94,13 +94,13 @@ msgid "vegan"
msgstr "vegan"

msgid "FILTERED_DISH_ANNOUNCEMENT_NO_DISHES"
msgstr "Unfortunately, no {type} dishes are offered at {mensa} on {date}."
msgstr "Unfortunately, no {type} are offered at {mensa} on {date}."

msgid "FILTERED_DISH_ANNOUNCEMENT_ONE_DISH"
msgstr "There is one {type} dish offered at {mensa} on {date}: {dish}."
msgstr "There is one {type} offered at {mensa} on {date}: {dish}."

msgid "FILTERED_DISH_ANNOUNCEMENT_MULTIPLE_DISHES"
msgstr "There are {num} {type} dishes to choose from at {mensa} on {date}: {dishes}."
msgstr "There are {num} {type} to choose from at {mensa} on {date}: {dishes}."

# Annoucement for dish types
msgid "ANNOUNCEMENT_PREPOSITION_TABLE_DISH"
Expand Down Expand Up @@ -191,4 +191,53 @@ msgid "DAY_5"
msgstr "Saturday"

msgid "DAY_6"
msgstr "Sunday"
msgstr "Sunday"

# Dish Type Filter
msgid "FILTER_VEGETARIAN_SINGLE"
msgstr "vegetarian dish"

msgid "FILTER_VEGETARIAN_PLURAL"
msgstr "vegetarian dishes"

msgid "FILTER_VEGAN_SINGLE"
msgstr "vegan dish"

msgid "FILTER_VEGAN_PLURAL"
msgstr "vegan dishes"

msgid "FILTER_CLASSICS_SINGLE"
msgstr "classical dish"

msgid "FILTER_CLASSICS_PLURAL"
msgstr "classical dishes"

msgid "FILTER_PASTA_SINGLE"
msgstr "pasta dish"

msgid "FILTER_PASTA_PLURAL"
msgstr "pasta dishes"

msgid "FILTER_WOK_SINGLE"
msgstr "Wok dish"

msgid "FILTER_WOK_PLURAL"
msgstr "Wok dishes"

msgid "FILTER_PIZZA_SINGLE"
msgstr "Pizza"

msgid "FILTER_PIZZA_PLURAL"
msgstr "Pizzas"

msgid "FILTER_BURGER_SINGLE"
msgstr "Burger"

msgid "FILTER_BURGER_PLURAL"
msgstr "Burgers"

msgid "FILTER_TABLE_DISH_SINGLE"
msgstr "Table dish"

msgid "FILTER_TABLE_DISH_PLURAL"
msgstr "Table dishes"
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import datetime
from enum import Enum
from typing import Union, cast

import ask_sdk_core.utils as ask_utils
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response

from src.data.mensas import Mensa
from src.data.menu_model import DishType, MensaDayMenus, MensaMenu, NutritionFlag
from src.handler.intent_handler_with_mensa_and_date import IntentHandlerWithMensaAndDate
from src.utils import alexa_slots
from src.utils.localization import I18nFunction


class DishTypeFilter(str, Enum):
"""Enum for the different types of dishes that can be filtered."""

VEGETARIAN = "VEGETARIAN"
VEGAN = "VEGAN"
CLASSICS = "CLASSICS"
PASTA = "PASTA"
WOK = "WOK"
PIZZA = "PIZZA"
BURGER = "BURGER"
TABLE_DISH = "TABLE_DISH"

def matches(self, dish: MensaMenu) -> bool:
"""Check if the dish matches the filter."""
if self == DishTypeFilter.VEGETARIAN:
return NutritionFlag.VEGETARIAN in dish.nutrition_flags
if self == DishTypeFilter.VEGAN:
return NutritionFlag.VEGAN in dish.nutrition_flags
if self == DishTypeFilter.CLASSICS:
return dish.dish_type == DishType.CLASSICS
if self == DishTypeFilter.PASTA:
return dish.dish_type == DishType.PASTA
if self == DishTypeFilter.WOK:
return dish.dish_type == DishType.WOK
if self == DishTypeFilter.PIZZA:
return dish.dish_type in {
DishType.PIZZA_OF_THE_DAY,
DishType.PIZZA_CLASSICS,
}
if self == DishTypeFilter.BURGER:
return dish.dish_type in {
DishType.BURGER_CLASSICS,
DishType.BURGER_OF_THE_WEEK,
}
if self == DishTypeFilter.TABLE_DISH:
return dish.dish_type in {
DishType.TABLE_DISH,
DishType.VEGETARIAN_TABLE_DISH,
}
return False


def _get_dish_type_filter_from_request(
handler_input: HandlerInput,
) -> Union[Response, DishTypeFilter]:
_ = cast(
I18nFunction,
handler_input.attributes_manager.request_attributes["_"],
)
dish_types = alexa_slots.get_slot_ids_from_custom_slot(handler_input, "dishType")
if dish_types is None or len(dish_types) == 0:
return handler_input.response_builder.speak(
_("NO_DISH_TYPE_SPECIFIED")
).response

dish_type = next(iter(dish_types))
return DishTypeFilter(dish_type)


class GetFilteredMensaOfferingsIntentHandler(IntentHandlerWithMensaAndDate):
"""Handler for GetFilteredMensaOfferingsIntentHandler."""

def can_handle(self, handler_input: HandlerInput) -> bool:
"""Overwritten."""
return ask_utils.is_intent_name("GetFilteredMensaOfferingsIntentHandler")(
handler_input
)

def handle_with_mensa_and_date(
self,
handler_input: HandlerInput,
mensa: Mensa,
date: datetime.date,
mensa_offerings: MensaDayMenus,
) -> Response:
"""Overwritten."""
_ = cast(
I18nFunction,
handler_input.attributes_manager.request_attributes["_"],
)
response_or_filter = _get_dish_type_filter_from_request(handler_input)
if isinstance(response_or_filter, Response):
return response_or_filter

filtered_dishes = [
menu for menu in mensa_offerings.menus if response_or_filter.matches(menu)
]
if len(filtered_dishes) <= 0:
speak_output = _("FILTERED_DISH_ANNOUNCEMENT_NO_DISHES").format(
type=_(f"FILTER_{response_or_filter.value}_PLURAL"),
mensa=_(mensa.mensaId),
date=date,
)
return handler_input.response_builder.speak(speak_output).response
elif len(filtered_dishes) == 1:
speak_output = _("FILTERED_DISH_ANNOUNCEMENT_ONE_DISH").format(
type=_(f"FILTER_{response_or_filter.value}_SINGLE"),
mensa=_(mensa.mensaId),
date=date,
dish=filtered_dishes[0].generate_full_announcement(_),
)
return handler_input.response_builder.speak(speak_output).response
else:
speak_output = _("FILTERED_DISH_ANNOUNCEMENT_MULTIPLE_DISHES").format(
type=_(f"FILTER_{response_or_filter.value}_PLURAL"),
mensa=_(mensa.mensaId),
date=date,
num=len(filtered_dishes),
dishes=". ".join(
map(
lambda dish: dish.generate_full_announcement(_), filtered_dishes
)
),
)
return handler_input.response_builder.speak(speak_output).response

This file was deleted.

Loading

0 comments on commit 7afd073

Please sign in to comment.