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