Jan-10-2022, 10:43 PM
Hello,
So I'm working on an A.I assistant and I got a good chunk of it done. It can display the news, weather, time, do calculations and google searches.
The problem that I am having right now, is that I want it to look for some exact strings when I say my command.
Here's what I mean:
For example, when I ask my A.I what the weather is, it will display today's weather (that's fine).
But If I ask it what the weather for the week is, it will display today's weather only.
I know it does this because since 'weather' is in the command it automatically jumps to the first weather statement (which will display today's weather).
This is the weather portion of the code:
What I want it to do is:
I want it to look for exact strings in my command. So if I say "What is the weather for the week" It will recognize that weather and week are used in the same sentence and it will jump to the "weather for the week" statement (therefore displaying the weather for the week).
I would also like to leave the "What is" part of the command open because that is part of the search statements so it know that I am asking a question that requires an internet search in order to be answered (For example: "What is the sun" or "What is pie")
But at the same time, if I say "What is the time?" I want the program to recognize that I am asking for the time and therefore it should jump to the time statement and display the current time. (So the program should basically ignore the 'what is' part and only look for the 'time' string when it's looking for the if statement that it has to match up with)
What it currently does:
If I say "Time" the program will display the current time
If I say "What is the time?" the program will give me the definition of time.
What it should do:
So If I say "What is the time" The program should display the current time
But if I say "What is the sun" The program will use an internet search to find an answer for "What is the sun" because sun is not explicitly defined in it's command list.
How would I go about doing this?
Thanks.
Here is the full code:
So I'm working on an A.I assistant and I got a good chunk of it done. It can display the news, weather, time, do calculations and google searches.
The problem that I am having right now, is that I want it to look for some exact strings when I say my command.
Here's what I mean:
For example, when I ask my A.I what the weather is, it will display today's weather (that's fine).
But If I ask it what the weather for the week is, it will display today's weather only.
I know it does this because since 'weather' is in the command it automatically jumps to the first weather statement (which will display today's weather).
This is the weather portion of the code:
1 2 3 4 5 6 7 8 9 10 11 |
elif ( 'weather' in command): speak( 'Searching weather networks...' ) command = command.replace( "weather" ,"") #Call weather data for today: displayWeatherData() elif ( 'weather for the week' in command): speak( 'Searching weather networks...' ) command = command.replace( "weather for the week" ,"") #Call weather data for the week: displayWeeksWeatherData() |
I want it to look for exact strings in my command. So if I say "What is the weather for the week" It will recognize that weather and week are used in the same sentence and it will jump to the "weather for the week" statement (therefore displaying the weather for the week).
I would also like to leave the "What is" part of the command open because that is part of the search statements so it know that I am asking a question that requires an internet search in order to be answered (For example: "What is the sun" or "What is pie")
But at the same time, if I say "What is the time?" I want the program to recognize that I am asking for the time and therefore it should jump to the time statement and display the current time. (So the program should basically ignore the 'what is' part and only look for the 'time' string when it's looking for the if statement that it has to match up with)
What it currently does:
If I say "Time" the program will display the current time
If I say "What is the time?" the program will give me the definition of time.
What it should do:
So If I say "What is the time" The program should display the current time
But if I say "What is the sun" The program will use an internet search to find an answer for "What is the sun" because sun is not explicitly defined in it's command list.
How would I go about doing this?
Thanks.
Here is the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
#!/usr/bin/env python3 import json import random import datetime import operator import os import time from time import process_time import requests from bs4 import BeautifulSoup from Weather import * import wikipedia import wolframalpha import pyttsx3 import espeakng import speech_recognition as sr #------------------------------------------------------------------------------------- #Commander Name (You) and A.I Name #------------------------------------------------------------------------------------- Commander = "Commander" AI_Name = 'baxter' #------------------------------------------------------------------------------------- print ( "Initializing B.A.X.T.E.R..." ) def speak(text): mySpeaker = espeakng.Speaker() #mySpeaker.say('Initializing Baxter') def wishMe(): hour = int (datetime.datetime.now().hour) if hour> = 0 and hour< 12 : speak( "Good Morning" + Commander) print ( "Good Morning " + Commander) elif hour> = 12 and hour< 18 : speak( "Good Afternoon" + Commander) print ( "Good Afternoon " + Commander) else : speak( "Good Evening" + Commander) print ( "Good Evening " + Commander) speak( "How may I be of service?" ) print ( "How may I be of service?" ) def listenCommand(): command = 0 hear = sr.Recognizer() with sr.Microphone() as source: print ( "Listening..." ) audio = hear.listen(source) #--------------------------- # Uses google API to listen try : print ( "Recognizing..." ) command = hear.recognize_google(audio, language = 'en-in' ) print ( f '{Commander} : {command}\n' ) except : pass return command #-------------------------------- speak( "Initializing Baxter.........." ) wishMe() def main(): command = listenCommand() command = str (command).lower() #------------------------------------------------------------------------------------- #Search Wikipedia (General Info) #------------------------------------------------------------------------------------- if ( 'who is' in command) or ( 'what is the' in command) or ( 'what is a' in command): speak( 'Searching Wikipedia...' ) command = command.replace( "who is" ,"") command = command.replace( "what is the" ,"") command = command.replace( "what is a" ,"") results = wikipedia.summary(command, sentences = 2 ) print ( "Baxter:" ,results) return speak(results) #------------------------------------------------------------------------------------- #Search Wolfram Alpha (Math/Conversions, Definitions) #------------------------------------------------------------------------------------- elif ( 'calculate' in command) or ( 'what is' in command) or ( 'define' in command): speak( 'Searching Wolfram Alpha...' ) command = command.replace( "calculate" ,"") command = command.replace( "what is" ,"") command = command.replace( "define" ,"") # Wolframalpha App Id appId = 'JH9XHR-W9J76L7H5A' # Wolfram Instance client = wolframalpha.Client(appId) res = client.query(''.join(command)) results = next (res.results).text print ( "Baxter:" ,results) return speak(results) #------------------------------------------------------------------------------------- #Search News #------------------------------------------------------------------------------------- elif ( 'news' in command): speak( 'Searching news networks...' ) command = command.replace( "news" ,"") url = 'https://www.google.com/search?q=windsor+news&client=firefox-b-d&source=lnms&tbm=nws&sa=X&ved=2ahUKEwjFr5SwoJb1AhXELs0KHdabBAEQ_AUoAXoECAEQAw&biw=1024&bih=486' results = requests.get(url) soup = BeautifulSoup(results.text, 'html.parser' ) headlines = soup.find( 'body' ).find_all( 'h3' ) for x in headlines: print ( "Baxter:" ,x.text.strip()) return speak(results) #------------------------------------------------------------------------------------- #Search Weather #------------------------------------------------------------------------------------- elif ( 'weather' in command): speak( 'Searching weather networks...' ) command = command.replace( "weather" ,"") #Call weather data for today: displayWeatherData() elif ( 'weather for the week' in command): speak( 'Searching weather networks...' ) command = command.replace( "weather for the week" ,"") #Call weather data for the week: displayWeeksWeatherData() #------------------------------------------------------------------------------------- #Tell Time #------------------------------------------------------------------------------------- elif ( 'time' in command): speak( 'Scanning local clock networks...' ) command = command.replace( "time" ,"") strTime = datetime.datetime.now().strftime( "%I:%M%P" ) speak( "The time is {strTime}" ) print ( "Baxter:" , "The time is" , strTime) #------------------------------------------------------------------------------------- #Enter the Matrix (Easter Egg) #------------------------------------------------------------------------------------- elif ( 'matrix' in command): speak( 'Entering the matrix...' ) print ( "Baxter:" , "Taking the red pill..." ) command = command.replace( "matrix" ,"") time.sleep( 2 ) os.system( "cmatrix" ) #------------------------------------------------------------------------------------- #------------------------------------------------------------------------------------- #Stop Program/Script Command #------------------------------------------------------------------------------------- elif ( 'stop' in command) or ( 'shutdown' in command) or ( 'quit' in command): speak( "Shutting Down..." ) return exit() else : return 0 #------------------------------------------------------------------------------------- while True : main() |