From a3f805d88ed35ed253f3417fe4cfc74f2f0fd2f6 Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Tue, 15 Dec 2015 01:17:43 +0000 Subject: [PATCH] Use fuzzy matching for conversation entity matching Entity may not be picked up exactly by the speech recognition. Instead of doing an exact string match, look for names which are phonetically similar and choose the best match above a certain threshold. This helps as entity names may often be pronoun's (Dave's bedroom) and this allows for some minor mistakes i.e "the bedroom" will successfully match against a switch called "bedroom" It introduces a new library dependency, fuzzywuzzy for comparisons. --- homeassistant/components/conversation.py | 13 ++++++++++--- requirements_all.txt | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/conversation.py b/homeassistant/components/conversation.py index 7cd1193448c..18ddf8fcc8d 100644 --- a/homeassistant/components/conversation.py +++ b/homeassistant/components/conversation.py @@ -9,6 +9,7 @@ https://home-assistant.io/components/conversation/ import logging import re + from homeassistant import core from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF) @@ -21,9 +22,13 @@ ATTR_TEXT = "text" REGEX_TURN_COMMAND = re.compile(r'turn (?P(?: |\w)+) (?P\w+)') +REQUIREMENTS = ['fuzzywuzzy==0.8.0'] + def setup(hass, config): """ Registers the process service. """ + from fuzzywuzzy import process as fuzzyExtract + logger = logging.getLogger(__name__) def process(service): @@ -42,9 +47,11 @@ def setup(hass, config): name, command = match.groups() - entity_ids = [ - state.entity_id for state in hass.states.all() - if state.name.lower() == name] + entities = {state.entity_id: state.name for state in hass.states.all()} + + entity_ids = fuzzyExtract.extractOne(name, + entities, + score_cutoff=65)[2] if not entity_ids: logger.error( diff --git a/requirements_all.txt b/requirements_all.txt index 48a127b0f30..a7aff560a88 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -12,6 +12,9 @@ PyMata==2.07a # homeassistant.components.device_tracker.icloud https://github.com/picklepete/pyicloud/archive/80f6cd6decc950514b8dc43b30c5bded81b34d5f.zip#pyicloud==0.8.0 +# homeassistant.components.conversation +fuzzywuzzy==0.8.0 + # homeassistant.components.device_tracker.netgear pynetgear==0.3