Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Voice Command Phrase Matching Help

If you have a question or need help, this is the place to be.
Post Reply
m19brandon
Experienced User
Posts: 177
Joined: Mon Feb 03, 2014 10:36 pm

Voice Command Phrase Matching Help

Post by m19brandon »

So, I have all this stuff in EG and in Android's Tasker to help automate my home but I dont know if I am doing it correctly or in the best way possible :cry:. I have been parsing voice commands with RegEx Matching and Im searching for something better. Most of RegEx Matching has been done in Android's Tasker but I have been moving that over to EG because my server is much faster and a full keyboard. My Python script is post at the bottom, please comment.

Last night I came across this article and liked the idea and will try it out in EG, but if anyone else has an idea I would like to hear your ideas. http://misterhouse.blogspot.com/2014/10 ... watch.html

My Python RegEx Parsing

Code: Select all

from datetime import datetime
import re

match_objects = {}

#This is a reg Search function that if true add to a dict
def regSearch(testname, regex, command, exclude=''):
    r = re.search(regex, command, re.IGNORECASE)
    if r: 
        if exclude <> '':
            e = re.search(exclude, command, re.IGNORECASE)
            print e
            if e:
                resutls = False
            else:
                match_objects.setdefault(testname, r)
                resutls = True
        else:
            match_objects.setdefault(testname, r)
            resutls = True
    else:
        resutls = False
    return resutls

#Time run test
tm_start = datetime.now()

cmd = eg.event.payload.arcomm
#Google
#cmd = 'search for the haunt house'
#Netflix
#cmd =  'search for netflix for the batman'
########################################################
#Netflix with on netflix is not working
#cmd   'search for movie batmand on netflix'
########################################################
#The Daily Show
#cmd =  'daily show'
#YouTube
#cmd = 'Youtube search for SourceFeed'
#Lights
#cmd = 'lights'

cmd = cmd.strip().lower()

print 'Command: ' + cmd

#g_re = r'(?P<action>google to|goosgle for|google 4|google|search for|search 4|search)(?P<query>\b.+\b)?'
#Google
google_re = r'(?P<action>search google|google search|google|search)(?P<pretext> to| 2| for| 4)?(?P<query>\b.+\b)?'
google_re_ex = r'(?P<ex>netflix|netf|youtube|you tube)'
#google_re = r'(?P<action>search google|google search|google|search)(?P<pretext> to| 2| for| 4)?(?P<query>\b.+\b)?(?<!netflix)'


#g_re = r'(?P<action>search google|google search|google|search)(?P<pretext> to | 2 | for | 4 )?(?P<query>\b.+\b)?'
#Netflix
netflix_re = r'(?P<action>search netflix|netflix search|netflix)(?P<pretext>.* to watch|.* 2 watch|.* for|.* 2|.* 4|.* watch|.* called|.* tv show named}.* tv show)?(?P<query>\b.+\b)?'
youtube_re = r'(?P<action>search youtube|youtube search|youtube)(?P<pretext>.* to watch|.* 2 watch|.* for|.* 2|.* 4|.* watch|.* called|.* tv show named}.* tv show)?(?P<query>\b.+\b)?'

cable_re = '(?P<action>search tv|tv search|youtube)(?P<pretext>.* to watch|.* 2 watch|.* for|.* 2|.* 4|.* watch|.* called|.* tv show named}.* tv show)?(?P<query>\b.+\b)?'

iwant_re = '((?P<pretext>.* to watch|.* 2 watch|.* for|.* 2|.* 4|.* watch|.* called|.* tv show named}.* tv show)?(?P<query>\b.+\b)?'

light_re = r'(?P<action>light|lights|life|lite)'

if regSearch('google', google_re, cmd, google_re_ex):
    print 'Google Pattern Found'
    g = match_objects['google']
    if g.group('query'):
        print g.group('query')
    else:
        print 'No search query was received, opening URL Google'
    
elif regSearch('netflix', netflix_re, cmd):
    print 'Netflix Pattern Found'
    g = match_objects['netflix']
    if g.group('query'):
        print g.group('query')
    else:
        print 'No search query was received, opening URL Netlfix'
elif 'daily show' in cmd:
    print 'Daily Show Pattern Found'		
elif regSearch('youtube', youtube_re, cmd):
    print 'YouTube Pattern Found'
    g = match_objects['youtube']
    if g.group('query'):
        print g.group('query')
    else:
        print 'No search query was received, opening URL YouTube'
elif regSearch('cable', cable_re, cmd):
    print 'Cable Pattern Found'
    g = match_objects['cable']
    if g.group('query'):
        print g.group('query')
    else:
        print 'No search query was received, opening URL YouTube'
elif regSearch('light', light_re, cmd):
    print 'Light Pattern Found'
    g = match_objects['light']
    eg.TriggerEvent("lights_master_bedroom_toggle")
else:
    print 'cmd is unknown: ' + cmd
#eg.event.payload.arcomm = ''

tm_end = datetime.now()

tm_diff = tm_end - tm_start
print tm_diff
User avatar
jitterjames
Experienced User
Posts: 677
Joined: Thu Aug 13, 2009 4:36 pm
Location: Quebec, Canada
Contact:

Re: Voice Command Phrase Matching Help

Post by jitterjames »

You might be interested in using VoxCommando instead.
Post Reply