core/homeassistant/components/conversation/util.py

35 lines
1.2 KiB
Python
Raw Normal View History

"""Util for Conversation."""
import re
def create_matcher(utterance):
"""Create a regex that matches the utterance."""
# Split utterance into parts that are type: NORMAL, GROUP or OPTIONAL
# Pattern matches (GROUP|OPTIONAL): Change light to [the color] {name}
2019-07-31 19:25:30 +00:00
parts = re.split(r"({\w+}|\[[\w\s]+\] *)", utterance)
# Pattern to extract name from GROUP part. Matches {name}
2019-07-31 19:25:30 +00:00
group_matcher = re.compile(r"{(\w+)}")
# Pattern to extract text from OPTIONAL part. Matches [the color]
2019-07-31 19:25:30 +00:00
optional_matcher = re.compile(r"\[([\w ]+)\] *")
2019-07-31 19:25:30 +00:00
pattern = ["^"]
for part in parts:
group_match = group_matcher.match(part)
optional_match = optional_matcher.match(part)
# Normal part
if group_match is None and optional_match is None:
pattern.append(part)
continue
# Group part
if group_match is not None:
2021-04-09 16:58:27 +00:00
pattern.append(fr"(?P<{group_match.groups()[0]}>[\w ]+?)\s*")
# Optional part
elif optional_match is not None:
2021-04-09 16:58:27 +00:00
pattern.append(fr"(?:{optional_match.groups()[0]} *)?")
2019-07-31 19:25:30 +00:00
pattern.append("$")
return re.compile("".join(pattern), re.I)