def top_stories(): url = 'rss.cnn.com/rss/edition.rss' tree = ET.ElementTree(file=urllib.urlopen(url)) root = tree.getroot() index1 = 0 index2 = 0 index3 = 0 x = 0 # ToDo: test this section; determines the necesary length of the array for item in root.iterfind('channel/item/title'): index1 += 1 goodtext = [None] * index1 mytitle = [None] * index1 # gets the titles of each article for item in root.iterfind('channel/item/title'): mytitle[index3] = item.text index3 += 1 # gets the description of each article for item in root.iterfind('channel/item/description'): mytext = item.text goodtext[index2] = mytext.split('<br', 1)[0] index2 += 1 # if there is no description then use the article title instead for stuff in goodtext: if stuff == '': talking(mytitle[x]) else: talking(stuff) x += 1
def todo(action): """ talking('would you like to add to your to do list or clear it') key = ['add','clear','ad'] i_said = listening() a = pre_selection(i_said, key) print a""" # prints todo list if action == [11, 12]: the_start = True while the_start: try: my_file = open(config.todo_loc, 'r') except IOError: print 'No ToDo list exists, creating a new file' my_file = open(config.todo_loc, 'w') my_file.close() my_file = open(config.todo_loc, 'r') f = my_file.readlines() my_file.close() talking('what would you like to add to your list') while True: try: i_said = listening() f.insert(0, i_said + '\n') break except TypeError, e: print e print 'nothing was said' my_file = open(config.todo_loc, 'w') my_file.writelines(f) my_file.close() talking('your task has been added, would you like to add more') try: i_said = listening() key = ['yes', 'ad', 'add', 'more', 'done', 'no', 'finished'] find_truth = pre_selection(i_said, key) for num in find_truth: print find_truth yes_test = [0, 1, 2, 3] no_test = [4, 5, 6] a = num in yes_test if a: for num2 in find_truth: b = num2 in no_test if b: the_start = False else: print 'adding more' elif not a: for num2 in find_truth: b = num2 in no_test if b: the_start = False except TypeError, e: the_start = False print e break
def wolfram_alpha(my_question): searching = True search_item = 0 some_text = None look_for = my_question talking('let me think') key = config.wolf_key my_question = my_question.replace('ava ', '') my_question = my_question.replace(' ', '+') print my_question url = 'http://api.wolframalpha.com/v2/query?appid=' + key + '&input=' + my_question + '&format=plaintext' tree = ET.ElementTree(file=urllib.urlopen(url)) for elem in tree.iterfind('pod[@title="Result"]/subpod/plaintext'): some_text = elem.text try: some_text = some_text.replace('\'', ' feet') some_text = some_text.replace('\"', ' inches') talking(some_text) time.sleep(1) except Exception, e: print e pattern = re.compile('([^\s\w]|_)+') b_string = re.sub(pattern, '', look_for) phrase = b_string print phrase pattern = re.compile("\\b(lot|lots|a|an|who|can|you|what|is|info|somethings|whats|have|i|something|to|know|like" "|Id|information|about|tell|me)\\W", re.I) phrase_noise_removed = [pattern.sub("", phrase)] print phrase_noise_removed[0] term_search = wikipedia.search(phrase_noise_removed[0]) # ToDo: test this portion. It checks to see if what AVA is searching for is the correct thing while searching: text = 'would you like me to get information on %s' % term_search[search_item] talking(text) rsp = listening() my_rsp = comprehension.pre_selection(rsp, ['yes', 'no']) if (my_rsp == [0]) and (search_item <= 2): print term_search[search_item] the_summary = (wikipedia.summary(term_search[search_item], 2)) print the_summary talking(the_summary) searching = False elif my_rsp == [1]: talking('ok') search_item += 1
def tell_joke(): jokes = ['A ham sandwich walks into a bar and orders a beer. Bartender says, Sorry we do not serve food here.', 'Why do chicken coops only have two doors? Because if they had four, they would be chicken sedans!', 'Why did the Clydesdale give the pony a glass of water? Because he was a little horse!', 'How do you make a Kleenex dance? Put a little boogie in it!', 'Two peanuts were walking down the street. One was a salted.', 'How do you make holy water? You boil the hell out of it.', 'A woman is on trial for beating her husband to death with his guitar collection. Judge says, First ' 'offender? She says, No, first a Gibson! Then a Fender!', 'I had a dream that I was a muffler last night. I woke up exhausted!', 'How do you tell the difference between a frog and a horny toad? A frog says, Ribbit, ribbit and a horny ' 'toad says, Rub it, rub it.', 'A three legged dog walks into a bar and says to the bartender, Im looking for the man who shot my paw.', 'Whats Forrest Gumps password? 1 forrest 1'] x = randint(0, len(jokes)-1) print len(jokes) print 'telling joke number %i' % x talking(jokes[x])
def selection(what_i_said): find_truth = pre_selection(what_i_said, slct) if not find_truth: talking('How can I help you sir') menus.menu_choice() what_i_said = listening() what_i_said = what_i_said.replace('what\'s','what is') find_truth = pre_selection(what_i_said, slct) print find_truth while True: if (find_truth == [0, 1]) or (find_truth == [15]): games.play_hi_low() break elif find_truth == [2]: break elif (find_truth == [3, 4, 16, 17]) or (find_truth == [4, 9, 16, 17]) or (find_truth == [3, 4, 9, 16, 17]) \ or (find_truth == [3, 4]) or (find_truth == [4, 9]) or (find_truth == [3, 4, 9]): weather.current_weather() break elif (find_truth == [4, 5]) or (find_truth == [5]) or (find_truth == [4, 5, 16, 17]) or \ (find_truth == [4, 16, 17]): weather.forecast() break elif find_truth == [6, 7, 8]: games.RPSGame() break elif find_truth == [10]: games.tell_joke() break elif (find_truth == [11, 12]) or (find_truth == [11, 13]) or (find_truth == [11, 14]): todo_action.todo(find_truth) break elif (find_truth == [16, 17]) or (find_truth == [17, 18]) or (find_truth == [17, 19]) or \ (find_truth == [17, 20]) or (find_truth == [17, 21]) or (find_truth == [16, 22]) or \ (find_truth == [22, 18]) or (find_truth == [22, 19]) or (find_truth == [22, 20]) or \ (find_truth == [22, 21]): knowledge.wolfram_alpha(what_i_said) break else: decide(what_i_said) break
def forecast(): key = config.wunder_key while True: my_zip = get_zip() text = 'getting weather information on ' + my_zip[1] + ',' + my_zip[2] talking(text) url = 'http://api.wunderground.com/api/' + key + '/geolookup/forecast/q/' + my_zip[0] + '.json' try: f = urllib.urlopen(url) except IOError: print IOError talking('You are not connected to the internet') break json_string = f.read() parsed_json = json.loads(json_string) for day in parsed_json['forecast']['simpleforecast']['forecastday']: x = day['date']['day'] y = str(x) forecast_text = 'The weather for ' + day['date']['weekday'] + ', ' + day['date']['monthname'] + ' ' + y + \ ' will be ' + day['conditions'] + ' with a high of ' + day['high'][ 'fahrenheit'] + ' degrees fahrenheit and a low of ' + day['low'][ 'fahrenheit'] + ' degrees ferinheight' talking(forecast_text) f.close() break
def current_weather(): key = config.wunder_key while True: my_zip = get_zip() text = 'getting weather information on ' + my_zip[1] + ',' + my_zip[2] talking(text) url = 'http://api.wunderground.com/api/' + key + '/geolookup/conditions/q/PA/' + my_zip[0] + '.json' try: f = urllib.urlopen(url) except IOError: print IOError talking('You are not connected to the internet') break json_string = f.read() parsed_json = json.loads(json_string) city = parsed_json['location']['city'] weather = parsed_json['current_observation']['weather'] temperature_string = parsed_json['current_observation']['temp_f'] temperature_string = str(temperature_string) feels_like_string = parsed_json['current_observation']['feelslike_f'] weather_text = 'Weather in ' + city + ': ' + weather.lower() + '. The temperature is ' + temperature_string + \ ' and feels like ' + feels_like_string + '.' talking(weather_text) f.close() break
def send_email(my_msg, my_subj): # this can be a List of Email Ids email_list = config.mail_list for emailID in email_list: try: text = 'sending email to ' + emailID talking(text) username = config.mail_sender access = b64decode(config.mail_access) fromaddr = config.mail_sender toaddrs = emailID msg = 'Subject: Here is your ' + my_subj + ' from your personal assistant\r\n' + my_msg + \ '\nThis message was sent using my personal Python Assistant\n Python is Great !!!' server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username, access) server.sendmail(fromaddr, toaddrs, msg) server.quit() talking('your Email has been sent') except Exception, e: print e
def interpret(num): if num == 1: talking('Rock') return 'Rock' elif num == 2: talking('Paper') return 'Paper' elif num == 3: talking('Scissors') return 'Scissors'
def __init__(self): comp_score = 0 player_score = 0 tie_game = 0 player = 0 playing = True validity = True talking('Lets play a game of Rock, Paper, Scissors') while playing: while validity: i_said = listening() bro_commands = ['rock', 'paper', 'scissors', 'quit', 'Rock'] player_hand = pre_selection(i_said, bro_commands) if (player_hand == [0]) or (player_hand == [4]): player = 1 break elif player_hand == [1]: player = 2 break elif player_hand == [2]: player = 3 break elif player_hand == [3]: player = 4 break else: print 'Invalid Choice' if player == 4: if player_score > comp_score: text = 'final score, player %i, computer %i, Congratulations you win' % (player_score, comp_score) elif player_score < comp_score: text = 'final score, player %i, computer %i, Computer wins' % (player_score, comp_score) else: text = 'final score, player %i, computer %i, tie game' % (player_score, comp_score) talking(text) break else: comp = self.comp_hand() result = self.play_hand(comp, player) player_choice = self.interpret(player) comp_choice = self.interpret(comp) print '\nYou played %s and the computer played %s' % (player_choice, comp_choice) talking(result) print '' print '-' * 34 if result == 'Computer wins!': comp_score += 1 elif result == 'Player wins!': player_score += 1 elif result == 'Tie game': tie_game += 1 print 'Player: %i Computer: %i Tie Games: %i' % (player_score, comp_score, tie_game) print '-' * 34 print ''
the_start = False else: print 'adding more' elif not a: for num2 in find_truth: b = num2 in no_test if b: the_start = False except TypeError, e: the_start = False print e break # Clears the todo list elif action == [11, 14]: try: talking('clearing your to do list') my_file = open(config.todo_loc, 'r') my_file.close() f = [] my_file = open(config.todo_loc, 'w') my_file.writelines(f) my_file.close() talking('your list has been cleared') except IOError: print 'file not created' # Reads back the todo list elif action == [11, 13]: try: my_file = open(config.todo_loc, 'r') except IOError: print 'No ToDo list exists, creating a new file'
def maker(): your_maker = 'I was created august 24th by the all knowing Quincy' talking(your_maker)
def identity(): who_am_i = 'I am the automated virtual assistant better known as ava. I was created to make your life easier' talking(who_am_i)
def play_hi_low(): play_again = True while play_again: cont = True num_guess = 0 comp_num = randint(1, 10) print comp_num talking('I have picked a number between one and ten. Can you guess what it is?') while True: player_guess = listening() # checks for the number 6 (has difficulty understanding difference between 6 and sex) if player_guess == 'sex': player_guess = '6' # checks for the number 4 (has difficulty understanding difference between 4 and thor) if player_guess == 'thor': player_guess = '4' try: player_guess = int(player_guess) except (ValueError, TypeError), e: print e if player_guess == comp_num: num_guess += 1 text = 'Congratulations! You won in %i guesses!' % num_guess talking(text) talking('Do you want to play again. Yes or no?') i_said = listening() key = ['yes', 'no', 'quit'] redo = pre_selection(i_said, key) while cont: if redo == [0]: cont = False break elif (redo == [1, 2]) or (redo == [1]) or (redo == [2]): # if anything else is said, assume a quit play_again = False cont = False break else: talking('I am sorry. please say either yes or no') break break elif player_guess < comp_num: talking('Guess higher') num_guess += 1 elif player_guess > comp_num: talking('Guess lower') num_guess += 1