schlauere Rudimentäre Befehlauflösung
This commit is contained in:
69
main.py
69
main.py
@@ -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):
|
||||||
|
|
||||||
# 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
|
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
|
# 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")
|
|
||||||
|
|
||||||
|
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:
|
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
|
context["pending_slot"] = None
|
||||||
|
return True
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user