Jari Flederick Jari Flederick - 2 years ago 173
Python Question

Python Personal Assistent

Oke so i decided to get into python with quite a project. I want to make a personal assistent but i have a problem.

import speech_recognition as sr
import os
import webbrowser

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)

# recognize speech using Microsoft Bing Voice Recognition
BING_KEY = "some_key_goes_here"
print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
except sr.UnknownValueError:
print("Microsoft Bing Voice Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))

command = r.recognize_bing(audio, key=BING_KEY)
words = command.split()


if words[0] == "atlas":
if words[1] == "launch":
print("Launch Program")
if words[2] == "chrome":
print("Launch Chrome")
os.startfile("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
print("What do you want to launch?")
elif words[1] == "internet" and words[2] == "search":
c = len(words)
c = int(float(c))
url = "https://www.google.be/webhp?hl=nl#hl=nl&q=" + words[3:c]
print("Unknown Command")
print("Unknown Command")

input("Press Enter to continue...")

The problem occures with the "c" i want it to search the internet on all the things i say not just the 3th word. I tryed using the variable c but it doesn't work unfortunatly. Also help with cleaning up the code is accepted

EDIT: Error is: "Can't convert 'list' object to str implicitly"

Answer Source


c = len(words)
c = int(float(c))

gets the length of words as an integer, casts it to a floating-point number, casts it back to an integer, and then slices words from its third element up to the second-to-last, because the end of a slice is not inclusive.

If you want to form a Google query, join the terms with +, and don't specify an end to the slice so that it actually goes to the end:

elif words[1] == "internet" and words[2] == "search":
    url = "https://www.google.be/webhp?hl=nl#hl=nl&q=" + '+'.join(words[3:])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download