Example #1
0
def define_subject(speech_text):
    # words_of_message = speech_text.split()
    # words_of_message.remove('who')
    # words_of_message.remove('are')
    # words_of_message.remove('what')
    # words_of_message.remove('is')
    # words_of_message.remove('define')
    # cleaned_message = ' '.join(words_of_message)
    # cleaned_message = speech_text.replace('define', '').replace('who are', '').replace('what is', '').strip()

    words = speech_text.split()
    for word in words[:3]:
        if word == 'define' or word == 'who' or word == 'are' or words == 'what' or words == 'is':
            words.remove(word)
    cleaned_message = ' '.join(words)

    try:
        wiki_data = wikipedia.summary(cleaned_message, sentences=5)
        regEx = re.compile(r'([^\(]*)\([^\)]*\) *(.*)')
        m = regEx.match(wiki_data)
        while m:
            wiki_data = m.group(1) + m.group(2)
            m = regEx.match(wiki_data)
        print('AFTER::: ', wiki_data)
        wiki_data = wiki_data.replace("'", "")
        return tts(wiki_data)
    except wikipedia.exceptions.DisambiguationError as e:
        print("Can you please be more specific? You may choose something from the following.; {0}".format(e))
        return tts('Can you please be more specific? You may choose something from the following.')
    except wikipedia.exceptions.PageError:
        return tts('Page id "{}" does not match any pages. Try another id!'.format(cleaned_message))
Example #2
0
def show_all_uploads():
    conn = sqlite3.connect('memory.db')
    cursor = conn.execute("SELECT * FROM image_uploads")
    for row in cursor:
        print(row[0] + ': (' + row[1] + ') on ' + row[2])
    tts('Requested data has been printed on your terminal')
    conn.close()
    return
Example #3
0
def play_random(music_path):
    try:
        music_listing = mp3gen(music_path)
        music_playing = random.choice(music_listing)
        print('music_playing: ', music_playing)
        tts("Now playing your music!")
        return music_player(music_playing)
    except IndexError as e:
        print("No music files found: {0}".format(e))
        return tts('No music files found.')
Example #4
0
def show_all_notes():
    conn = sqlite3.connect('memory.db')
    tts('Your notes are as follows:')

    cursor = conn.execute("SELECT notes FROM notes")

    for row in cursor:
        tts(row[0])

    conn.close()
    return None
Example #5
0
def get_news():
    url = 'https://newsapi.org/v2/top-headlines'
    params = {
        'country': 'us',
        'apiKey': '578265f838fb4cc191ac00e7e60a73a2',
        'category': 'technology',
        'pageSize': 3
    }

    response = requests.get(url, params).json()
    if response['status'] == 'ok':
        articles = response['articles']
        tts('Top 3 headlines in the US are')
        for article in articles:
            print(article['title'])
            tts(article['title'])
    return None
Example #6
0
def control_light(action, light_color=None):
    """
    Control lights using raspberry pi
    :param action: string on/off to turn on or off
    :param light_color: string contain led color
    """
    if not PI:
        print("I can't find the raspberry pi to do the control actions")
        return None

    if action == 'on':
        if light_color == 'red':
            GPIO.output(RED_PIN, True)
            return tts('{} light {}.'.format(light_color, action))
        elif light_color == 'green':
            GPIO.output(GREEN_PIN, True)
        elif light_color == 'yellow':
            GPIO.output(YELLOW_PIN, True)
        elif light_color == 'blue':
            GPIO.output(BLUE_PIN, True)
            return tts('{} light {}.'.format(light_color, action))
        else:
            GPIO.output(RED_PIN, True)
            GPIO.output(GREEN_PIN, True)
            GPIO.output(YELLOW_PIN, True)
            GPIO.output(BLUE_PIN, True)
    elif action == 'off':
        if light_color == 'red':
            GPIO.output(RED_PIN, False)
            return tts('{} light {}.'.format(light_color, action))
        elif light_color == 'green':
            GPIO.output(GREEN_PIN, False)
        elif light_color == 'yellow':
            GPIO.output(YELLOW_PIN, False)
        elif light_color == 'blue':
            GPIO.output(BLUE_PIN, False)
            return tts('{} light {}.'.format(light_color, action))
        else:
            GPIO.output(RED_PIN, False)
            GPIO.output(GREEN_PIN, False)
            GPIO.output(YELLOW_PIN, False)
            GPIO.output(BLUE_PIN, False)
    else:
        return tts(
            'You should tell me explicitly to turn on or off the lights')
Example #7
0
def play_shuffle(music_path):
    try:
        music_listing = mp3gen(music_path)
        random.shuffle(music_listing)
        for i in range(0, len(music_listing)):
            return music_player(music_listing[i])
    except IndexError as e:
        print("No music files found: {0}".format(e))
        return tts('No music files found.')
Example #8
0
def active():
    """Active state"""
    global RECOGNIZE_ERRORS
    print('I am in active state')

    speech_text = recognize()
    if speech_text:
        standby_state = brain(speech_text, bot_name, username, location,
                              music_path, images_path)
        if standby_state == 0:
            return True
        else:
            time.sleep(1)
            tts('I am listening. You can ask me again.')
            return False
    else:
        # tts("I couldn't understand your audio, Try to say something!")
        RECOGNIZE_ERRORS += 1
        return False
def post_tweet(speech_text):
    words_of_message = speech_text.split()
    words_of_message.remove('tweet')
    cleaned_message = ' '.join(words_of_message).capitalize()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    print('cleaned msg: ', cleaned_message)
    api.update_status(status=cleaned_message)

    return tts('Your tweet has been posted on your twitter account')
Example #10
0
def note_something(speech_text):
    conn = sqlite3.connect('memory.db')
    words_of_message = speech_text.split()
    words_of_message.remove('note')
    cleaned_message = ' '.join(words_of_message)

    conn.execute(
        "INSERT INTO notes (notes, notes_date) VALUES (?, ?)",
        (cleaned_message, datetime.strftime(datetime.now(), '%d-%m-%Y')))
    conn.commit()
    conn.close()

    return tts('Your note has been saved.')
Example #11
0
def weather(location):
    lat, lng = geocode_location(location)

    owm = pyowm.OWM(OpenWeatherMap_API_key)

    # Search for current weather
    w = owm.weather_at_coords(lat, lng).get_weather()

    # Weather details
    status = w.get_detailed_status()
    temp = round(w.get_temperature('celsius')['temp'])

    return tts("Now, It is {} and {} degree celsius in {}".format(
        status, temp, location))
Example #12
0
def image_uploader(speech_text, images_path):
    words_of_message = speech_text.split()
    words_of_message.remove('upload')
    cleaned_message = ' '.join(words_of_message)
    image_listing = img_list_gen(images_path)
    client = ImgurClient(client_id, client_secret)
    for i in range(0, len(image_listing)):
        if cleaned_message in image_listing[i]:
            result = client.upload_from_path(image_listing[i], config=None, anon=True)
            conn = sqlite3.connect('memory.db')
            conn.execute("INSERT INTO image_uploads (filename, url, upload_date) VALUES (?, ?, ?)",
                         (image_listing[i], result['link'], datetime.
                          strftime(datetime.now(), '%d-%m-%Y')))
            conn.commit()
            conn.close()
            print(result['link'])
            return tts('Your image has been uploaded')
Example #13
0
def news_reader():
    for key, value in news_dictionary.items():
        tts('Headline, ' + key)
        tts('News, ' + value)
    return None
Example #14
0
                              music_path, images_path)
        if standby_state == 0:
            return True
        else:
            time.sleep(1)
            tts('I am listening. You can ask me again.')
            return False
    else:
        # tts("I couldn't understand your audio, Try to say something!")
        RECOGNIZE_ERRORS += 1
        return False


if __name__ == '__main__':
    # Welcome message
    tts('Hi {}, I am {}. How can I help you?'.format(username, bot_name))

    sleep_mode = active()
    while True:
        if sleep_mode:
            tts('Bye!, I will go sleep now, Ping me if you need anything')
            control_light('on', 'yellow')

            active_mode = standby()
            while True:
                if active_mode:
                    tts('Hi {}, How can I help you?'.format(username))
                    control_light('off', 'yellow')
                    sleep_mode = active()
                    break
                else:
Example #15
0
def open_firefox():
    tts('Aye aye captain, opening Firefox')
    return webdriver.Firefox()
Example #16
0
    def wit():
        """Handling the speech text with wit.ai for artificial actions"""
        WIT_AI_KEY = "NCC2OIS54Y2ROFYCJ2XZDZREMXTNTIR5"

        client = Wit(access_token=WIT_AI_KEY)
        try:
            resp = client.message(speech_text)
        except ConnectionError as e:
            print('ConnectionError:', e)
            return tts('Please, Check your internet connection!')
        pprint(resp)

        entities = resp.get('entities')
        intent = entities.get('intent')
        on_off = entities.get('on_off')
        color = entities.get('color')
        datetime = entities.get('datetime')
        weather_location = entities.get('location')
        greetings = entities.get('greetings')
        thanks = entities.get('thanks')
        bye = entities.get('bye')
        wikipedia_search_query = entities.get('wikipedia_search_query')
        # reminder = entities.get('reminder')

        if intent:
            intent_value = intent[0].get('value')

            if intent_value == 'unknown':
                return undefined()
            elif intent_value == 'lights':
                light_color = 'all'
                if color:
                    light_color = color[0].get('value')
                elif 'red' in speech_text:
                    light_color = 'red'
                elif 'yellow' in speech_text:
                    light_color = 'yellow'
                elif 'blue' in speech_text:
                    light_color = 'blue'
                elif 'green' in speech_text:
                    light_color = 'green'
                elif 'white' in speech_text:
                    light_color = 'white'

                if on_off:
                    action = on_off[0].get('value')
                    return control_light(action, light_color)
                else:
                    return undefined()
            elif intent_value == 'weather':
                w_location = location
                if weather_location:
                    w_location = weather_location[0].get('value')
                return weather(w_location)
            elif intent_value == 'define':
                query = speech_text
                if wikipedia_search_query:
                    query = wikipedia_search_query[0].get('value')
                return define_subject(query)
            elif intent_value == 'doors':
                return tts("Oh Sorry! I'm not connected with any doors")
                # code to control door
            elif intent_value == 'news ':
                # code to view latest news
                return get_news()
            elif intent_value == 'welcome':
                return tts(
                    choice([
                        'I am fine, Thank you.', 'Pretty good',
                        "Can't be better", 'Not bad'
                    ]))
            elif intent_value == 'alarm':
                # code to set alarm
                replay = "I can't set the alarm, please try again"
                if datetime:
                    try:
                        date, time = datetime[0].get('value').split('T')
                        print('date is', date)
                        print('time is: ', time)
                        replay = 'Setting alarm at {}'.format(time[:5])
                    except:
                        pass
                return tts(replay)
            elif intent_value == 'temp':
                return tts(
                    "Oh Sorry! I can't do that without a temperature sensor!")
            elif intent_value == 'sleep':
                return 0
            elif intent_value == 'age':
                return tts('Oh! I am just a baby who learning how to speak!')

        elif greetings:
            return tts(
                choice([
                    "Hi, How's it going?", "Hey, What's up?",
                    'Hello, How are you?'
                ]))
        elif bye:
            # code to go sleep
            # tts('Bye!, I will go sleep now, Ping me if you need anything')
            return 0
        elif thanks:
            return tts(choice(['You are welcome', 'Anytime', 'Glad to help']))
        else:
            return undefined()
Example #17
0
 def undefined():
     return tts("Oh! Sorry, I Couldn't understand you")
Example #18
0
def brain(speech_text, bot_name, username, location, music_path, images_path):
    """
    The main function for logic and actions
    :param location: your location in the profile
    :param bot_name: bot name in the profile
    :param username: user name in the profile
    :param speech_text:
    :return: standby state
    """
    def undefined():
        return tts("Oh! Sorry, I Couldn't understand you")

    def check_message(check):
        words_of_message = speech_text.split()
        if set(check).issubset(set(words_of_message)):
            return True
        else:
            return False

    def wit():
        """Handling the speech text with wit.ai for artificial actions"""
        WIT_AI_KEY = "NCC2OIS54Y2ROFYCJ2XZDZREMXTNTIR5"

        client = Wit(access_token=WIT_AI_KEY)
        try:
            resp = client.message(speech_text)
        except ConnectionError as e:
            print('ConnectionError:', e)
            return tts('Please, Check your internet connection!')
        pprint(resp)

        entities = resp.get('entities')
        intent = entities.get('intent')
        on_off = entities.get('on_off')
        color = entities.get('color')
        datetime = entities.get('datetime')
        weather_location = entities.get('location')
        greetings = entities.get('greetings')
        thanks = entities.get('thanks')
        bye = entities.get('bye')
        wikipedia_search_query = entities.get('wikipedia_search_query')
        # reminder = entities.get('reminder')

        if intent:
            intent_value = intent[0].get('value')

            if intent_value == 'unknown':
                return undefined()
            elif intent_value == 'lights':
                light_color = 'all'
                if color:
                    light_color = color[0].get('value')
                elif 'red' in speech_text:
                    light_color = 'red'
                elif 'yellow' in speech_text:
                    light_color = 'yellow'
                elif 'blue' in speech_text:
                    light_color = 'blue'
                elif 'green' in speech_text:
                    light_color = 'green'
                elif 'white' in speech_text:
                    light_color = 'white'

                if on_off:
                    action = on_off[0].get('value')
                    return control_light(action, light_color)
                else:
                    return undefined()
            elif intent_value == 'weather':
                w_location = location
                if weather_location:
                    w_location = weather_location[0].get('value')
                return weather(w_location)
            elif intent_value == 'define':
                query = speech_text
                if wikipedia_search_query:
                    query = wikipedia_search_query[0].get('value')
                return define_subject(query)
            elif intent_value == 'doors':
                return tts("Oh Sorry! I'm not connected with any doors")
                # code to control door
            elif intent_value == 'news ':
                # code to view latest news
                return get_news()
            elif intent_value == 'welcome':
                return tts(
                    choice([
                        'I am fine, Thank you.', 'Pretty good',
                        "Can't be better", 'Not bad'
                    ]))
            elif intent_value == 'alarm':
                # code to set alarm
                replay = "I can't set the alarm, please try again"
                if datetime:
                    try:
                        date, time = datetime[0].get('value').split('T')
                        print('date is', date)
                        print('time is: ', time)
                        replay = 'Setting alarm at {}'.format(time[:5])
                    except:
                        pass
                return tts(replay)
            elif intent_value == 'temp':
                return tts(
                    "Oh Sorry! I can't do that without a temperature sensor!")
            elif intent_value == 'sleep':
                return 0
            elif intent_value == 'age':
                return tts('Oh! I am just a baby who learning how to speak!')

        elif greetings:
            return tts(
                choice([
                    "Hi, How's it going?", "Hey, What's up?",
                    'Hello, How are you?'
                ]))
        elif bye:
            # code to go sleep
            # tts('Bye!, I will go sleep now, Ping me if you need anything')
            return 0
        elif thanks:
            return tts(choice(['You are welcome', 'Anytime', 'Glad to help']))
        else:
            return undefined()

    if check_message(['who', 'are', 'you']):
        return tts(
            choice([
                'I am {}, your smarter personal assistant.'.format(bot_name),
                'Oh, You forget me, I am {}'.format(bot_name),
                'I am your friend {}'.format(bot_name),
                "{}, didn't I tell you before?".format(bot_name)
            ]))
    elif check_message(['how', 'i', 'look']) or check_message(
        ['how', 'am', 'i']):
        return tts(
            choice([
                'You are goddamn handsome!',
                'My knees go weak when I see you.',
                'You look like the kindest person that I have met.'
            ]))
    elif check_message(['tell', 'joke']):
        return tts(
            choice([
                'Why are mountains so funny? Because they are hill areas.',
                "This might make you laugh. How do robots eat guacamole? With computer chips.",
                'Have you ever tried to eat a clock?'
                'I hear it is very time consuming.',
                'What happened when the wheel was invented? A revolution.',
                'What do you call a fake noodle? An impasta!',
                'Did you hear about that new broom? It is sweeping the nation!',
                'What is heavy forward but not backward? Ton.',
                'No, I always forget the punch line.'
            ]))
    elif check_message(['who', 'am', 'i']):
        return tts(
            'You are {}, a brilliant person. I love you!'.format(username))
    elif check_message(['where', 'born']):
        return tts(
            'I was created by a wonderful CSE team as a graduation project in the faculty of Engineering, '
            'Minya University - Egypt.')
    # elif check_message(['how', 'are', 'you']):
    #     how_are_you()
    elif check_message(['time']):
        return tts("The time is " +
                   datetime.strftime(datetime.now(), '%I:%M %p'))
    elif check_message(['weather']):
        try:
            w_location = speech_text.split(' in ')[1].strip()
        except:
            w_location = location
        return weather(w_location)
    elif check_message(['define']):
        return define_subject(speech_text)
    elif check_message(['sleep']) or check_message(['bye']):
        # tts('Bye!, I will go sleep now, Ping me if you need anything')
        return 0
    elif check_message(['news']):
        return get_news()
    elif check_message(['remind']):
        words = speech_text.split()
        for word in words[:3]:
            if word == 'remind' or word == 'me' or word == 'to':
                words.remove(word)
        clean_msg = ' '.join(words)
        return tts(
            '"{}", Successfully added to your TO-DO list'.format(clean_msg))

    # kareem
    elif check_message(['tweet']):
        return post_tweet(speech_text)
    elif check_message(['upload']):
        return image_uploader(speech_text, images_path)
    elif check_message(['all', 'uploads']) or check_message(
        ['all', 'images']) or check_message(['uploads']):
        return show_all_uploads()
    #

    # khaled
    #elif check_message(['business', 'news']):
    #    news_reader()
    elif check_message(['play', 'music']) or check_message(['music']):
        return play_random(music_path)
    elif check_message(['play']):
        return play_specific_music(speech_text, music_path)
    elif check_message(['party', 'time']) or check_message(['party', 'mix']):
        return play_shuffle(music_path)
    elif check_message(['note']):
        return note_something(speech_text)
    elif check_message(['all', 'notes']) or check_message(['notes']):
        return show_all_notes()
    #elif check_message(['open', 'firefox']):
    #    return open_firefox()
    #

    else:
        state = wit()
        if state == 0:
            return 0
        return True