schlauere Rudimentäre Befehlauflösung

This commit is contained in:
2026-01-18 22:16:12 +01:00
parent 20afbeb122
commit 4a4bd6f286

69
main.py
View File

@@ -35,7 +35,8 @@ context = {
"intent": None,
"slots": {},
"required_slots": [],
"pending_slot": None
"pending_slot": None,
"action": None,
}
audio_queue = queue.Queue()
@@ -73,11 +74,17 @@ def speak(text):
INTENTS = {
"weather": {
"keywords": ["wetter", "temperatur", "regen"],
"required_slots": ["location"]
"required_slots": {
"location": r"\bin\b\s*(\w+)"
},
"subactions": ["info"]
},
"timer": {
"keywords": ["timer"],
"required_slots": ["duration"]
"required_slots": {
"duration": r"(sekunde|minute|stunde)"
},
"subactiions": ["start", "stop", "status"]
}
}
@@ -122,9 +129,9 @@ def handle_text(text):
print(f"[STT] {text}")
# 1. Rückfrage beantworten
if context["pending_slot"]:
context["slots"][context["pending_slot"]] = text
context["pending_slot"] = None
# if context["pending_slot"]:
# context["slots"][context["pending_slot"]] = text
# context["pending_slot"] = None
# 2. Intent erkennen
if not context["intent"]:
@@ -135,39 +142,46 @@ def handle_text(text):
return
context["intent"] = intent
context["required_slots"] = INTENTS[intent]["required_slots"]
context["required_slots"] = INTENTS[intent]["required_slots"] # man könnte per liste drüber iterieren wenn man mehrere required slots hat
check_required(text)
# 3. Fehlende Slots prüfen
for slot in context["required_slots"]:
if slot not in context["slots"]:
context["pending_slot"] = slot
ask_for_slot(slot)
if not check_required(text):
return
# 3. Fehlende Slots prüfen
# for slot in context["required_slots"]:
# if slot not in context["slots"]:
# context["pending_slot"] = slot
# ask_for_slot(slot)
# return
# 4. Skill ausführen
result = SKILLS[context["intent"]](context["slots"])
speak(result)
reset_context()
def check_required(text):
if context["intent"] == "weather" or context["pending_slot"] != None:
if not re.search(r'in\b', text):
print("LOCATION ABFRAGEN")
intent_data = INTENTS[context["intent"]]
text = text.lower()
for slot, pattern in intent_data.get("required_slots", {}).items():
if slot not in context["slots"]:
match = re.search(pattern, text)
if match:
context["slots"][slot] = match.group(1) # schau an
else:
print("LOCATION ERMITTELN")
context["pending_slot"] = slot
ask_for_slot(slot)
return False
for action in intent_data.get("subactions", []):
if action in text:
context["slots"]["action"] = action
context["pending_slot"] = None
elif context["intent"] == "timer":
if not re.search(r'\b(sekunde|minute|stunde)n?\b', text):
print("DURATION ABFRAGEN")
else:
print("DURATION ERMITTELN")
context["pending_slot"] = None
return True
def ask_for_slot(slot):
@@ -262,6 +276,7 @@ def real_wakeword_detector():
print("WAKE WORD DETECTED")
speak("Ja, wie kann ich helfen?")
with sd.InputStream(
samplerate=porcupine.sample_rate,
channels=1,