Skip to content

Instantly share code, notes, and snippets.

@noor-jafri
Last active May 1, 2019 20:23
Show Gist options
  • Save noor-jafri/df6bd02ff4156a3df673a1859872790b to your computer and use it in GitHub Desktop.
Save noor-jafri/df6bd02ff4156a3df673a1859872790b to your computer and use it in GitHub Desktop.
import nltk
import speech_recognition as sr
# You can run this code on the same RPI where Flask Server is running or on your Hosts to access
# remote station.
#
#
#Variables for Usage
what_action=""
noun=""
adjective=""
action=""
all_flag=0
# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech API
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
command = r.recognize_google(audio)
except sr.UnknownValueError:
print("Audio Couldn't Understand by Google Speech Recognition")
except sr.RequestError as e:
print("Request Failure; {0}".format(e))
token = nltk.word_tokenize(command)
tagged = nltk.pos_tag(token)
#Chopping up the NLP Command to readable Variables
#Use nltk.help.upenn_tagset() for full documentation
for tag in tagged:
#chopping verbs
if "VB" in tag:
action = tag[0].lower()
#chopping adverbs
if "RP" in tag:
what_action = tag[0].lower()
#chopping nouns
if "NN" in tag[1]:
noun = tag[0].lower()
#checking plurals
if "S" in tag[1]:
all_flag = 1
#chopping adjectives
if "JJ" in tag:
adjective = tag[0].lower()
print (tagged)
def light():
url=[]
if (all_flag==0):
url.append("http://<your_raspberry_ip>:5000/"+adjective+noun+what_action)
else:
url.append("http://<your_raspberry_ip>:5000/redled"+what_action)
url.append("http://<your_raspberry_ip>:5000/blueled"+what_action)
url.append("http://<your_raspberry_ip>:5000/greenled"+what_action)
#accessing all the urls
for u in url:
status = urllib.urlopen(url)
#Turn the music volume up and down
def music():
url=[]
url.append("http://<your_raspberry_ip>:5000/"+noun+what_action)
for u in url:
status = urllib.urlopen(url)
#turn the ac tempreature up and down
def ac():
url=[]
url.append("http://<your_raspberry_ip>:5000/"+noun+what_action)
for u in url:
status = urllib.urlopen(url)
#Defining Primary objects on which we can perforn actions
options_objects = {
"ac" : ac,
"lights" : light,
"light" : light,
"music" : music
}
options_objects[noun]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment