schlauere Rudimentäre Befehlauflösung

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

67
main.py
View File

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