mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Remove unused util module in conversation (#141293)
This commit is contained in:
parent
4472dc533d
commit
8904f174d2
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
import logging
|
import logging
|
||||||
import re
|
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
from hassil.recognize import RecognizeResult
|
from hassil.recognize import RecognizeResult
|
||||||
@ -91,8 +90,6 @@ __all__ = [
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REGEX_TYPE = type(re.compile(""))
|
|
||||||
|
|
||||||
SERVICE_PROCESS_SCHEMA = vol.Schema(
|
SERVICE_PROCESS_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(ATTR_TEXT): cv.string,
|
vol.Required(ATTR_TEXT): cv.string,
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
"""Util for Conversation."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
def create_matcher(utterance: str) -> re.Pattern[str]:
|
|
||||||
"""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}
|
|
||||||
parts = re.split(r"({\w+}|\[[\w\s]+\] *)", utterance)
|
|
||||||
# Pattern to extract name from GROUP part. Matches {name}
|
|
||||||
group_matcher = re.compile(r"{(\w+)}")
|
|
||||||
# Pattern to extract text from OPTIONAL part. Matches [the color]
|
|
||||||
optional_matcher = re.compile(r"\[([\w ]+)\] *")
|
|
||||||
|
|
||||||
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:
|
|
||||||
pattern.append(rf"(?P<{group_match.groups()[0]}>[\w ]+?)\s*")
|
|
||||||
|
|
||||||
# Optional part
|
|
||||||
elif optional_match is not None:
|
|
||||||
pattern.append(rf"(?:{optional_match.groups()[0]} *)?")
|
|
||||||
|
|
||||||
pattern.append("$")
|
|
||||||
return re.compile("".join(pattern), re.IGNORECASE)
|
|
@ -1,56 +0,0 @@
|
|||||||
"""Test the conversation utils."""
|
|
||||||
|
|
||||||
from homeassistant.components.conversation.util import create_matcher
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_matcher() -> None:
|
|
||||||
"""Test the create matcher method."""
|
|
||||||
# Basic sentence
|
|
||||||
pattern = create_matcher("Hello world")
|
|
||||||
assert pattern.match("Hello world") is not None
|
|
||||||
|
|
||||||
# Match a part
|
|
||||||
pattern = create_matcher("Hello {name}")
|
|
||||||
match = pattern.match("hello world")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "world"
|
|
||||||
no_match = pattern.match("Hello world, how are you?")
|
|
||||||
assert no_match is None
|
|
||||||
|
|
||||||
# Optional and matching part
|
|
||||||
pattern = create_matcher("Turn on [the] {name}")
|
|
||||||
match = pattern.match("turn on the kitchen lights")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
||||||
match = pattern.match("turn on kitchen lights")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
||||||
match = pattern.match("turn off kitchen lights")
|
|
||||||
assert match is None
|
|
||||||
|
|
||||||
# Two different optional parts, 1 matching part
|
|
||||||
pattern = create_matcher("Turn on [the] [a] {name}")
|
|
||||||
match = pattern.match("turn on the kitchen lights")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
||||||
match = pattern.match("turn on kitchen lights")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
||||||
match = pattern.match("turn on a kitchen light")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen light"
|
|
||||||
|
|
||||||
# Strip plural
|
|
||||||
pattern = create_matcher("Turn {name}[s] on")
|
|
||||||
match = pattern.match("turn kitchen lights on")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen light"
|
|
||||||
|
|
||||||
# Optional 2 words
|
|
||||||
pattern = create_matcher("Turn [the great] {name} on")
|
|
||||||
match = pattern.match("turn the great kitchen lights on")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
||||||
match = pattern.match("turn kitchen lights on")
|
|
||||||
assert match is not None
|
|
||||||
assert match.groupdict()["name"] == "kitchen lights"
|
|
Loading…
x
Reference in New Issue
Block a user