Beispiel #1
0
 def __init__(self):
     self.voice = VoiceHandler()
     self.command_handler = CommandHandler()
     self.commands = [
         k for k in list(self.command_handler.__class__.__dict__.keys())
         if "__" not in k
     ]
     self.listen_for_commands()
Beispiel #2
0
    def __init__(self):
        # text variable for GUI
        self.text = None

        # necessary lists
        self.function_list = []
        self.stm_list = []
        self.basic_list = []

        # necessary files
        function_file = open("functions.txt", "r+", encoding='utf8')
        stm_file = open("stmsignals.txt", "r+", encoding='utf8')
        basic_file = open("basics.txt", "r+", encoding='utf8')

        # creating necessary lists
        # function file
        line = function_file.readline().rstrip("\n")
        while line != "":
            self.function_list.append(line)
            line = function_file.readline().rstrip("\n")

        # stm file
        line = stm_file.readline().rstrip("\n")

        while line != "":
            self.stm_list.append(line)
            line = stm_file.readline().rstrip("\n")

        # basic file
        line = basic_file.readline().rstrip("\n")
        while line != "":
            self.basic_list.append(line)
            line = basic_file.readline().rstrip("\n")

        # closing unnecessary files
        function_file.close()
        stm_file.close()
        basic_file.close()

        # creating voice handler for information
        self.voice_handler = VoiceHandler()

        # creating internet searcher
        self.internet_handler = InternetHandler()

        # creating uart handler
        self.uart_handler = UartHandler(baudrate=9600)
class Main:
    def __init__(self):
        self.voice = VoiceHandler()
        self.command_handler = CommandHandler()
        self.commands = [k for k in list(self.command_handler.__class__.__dict__.keys()) if "__" not in k]
        self.listen_for_commands()

    def listen_for_commands(self):
        assistend_spoken = False

        while True:
            if not assistend_spoken:
                self.voice.textToSpeech("Waiting for your command sir...")
                assistend_spoken = True

            text: str = self.voice.listenForSpeech()
            if text:
                listed_text = [text] if not " " in text else text.split()
                parsed_command = None
                parsed_arguments = None
                # parse correct commands/arguments
                if listed_text[0] in self.commands:
                    parsed_command = listed_text[0]
                    if len(listed_text) > 1:
                        parsed_arguments = listed_text[1:]
                elif len(listed_text) > 1 and "_".join(listed_text[:2]).lower() in self.commands:
                    parsed_command = "_".join(listed_text[:2]).lower()
                    if len(listed_text) > 2:
                        parsed_arguments = listed_text[2:]
                elif "_".join(listed_text) in self.commands:
                    parsed_command = "_".join(listed_text)
                else:
                    # Search a command with more as 75% similairity
                    for command in self.commands:
                        # compare text to each command and calculate the similairity between the strings
                        sequence = difflib.SequenceMatcher(isjunk=None, a=text, b=command)
                        diffrence = round(sequence.ratio() * 100, 1)  # *100 due being stored in fractions.
                        if diffrence >= 75:
                            parsed_command = command
                            break

                if parsed_command: # if command was valid, run with(out) arguments
                    if parsed_command != "commands":
                        self.voice.textToSpeech("Command %s has been found, executing command" % parsed_command.replace("_", " "))

                    command = getattr(self.command_handler, parsed_command)

                    if not parsed_arguments:
                        command()
                    else:
                        command(parsed_arguments)

                    assistend_spoken = False
                else:
                    print(text)
                    self.voice.textToSpeech("Could not find the command %s" % text)
Beispiel #4
0
class Recognition:
    def __init__(self):
        # text variable for GUI
        self.text = None

        # necessary lists
        self.function_list = []
        self.stm_list = []
        self.basic_list = []

        # necessary files
        function_file = open("functions.txt", "r+", encoding='utf8')
        stm_file = open("stmsignals.txt", "r+", encoding='utf8')
        basic_file = open("basics.txt", "r+", encoding='utf8')

        # creating necessary lists
        # function file
        line = function_file.readline().rstrip("\n")
        while line != "":
            self.function_list.append(line)
            line = function_file.readline().rstrip("\n")

        # stm file
        line = stm_file.readline().rstrip("\n")

        while line != "":
            self.stm_list.append(line)
            line = stm_file.readline().rstrip("\n")

        # basic file
        line = basic_file.readline().rstrip("\n")
        while line != "":
            self.basic_list.append(line)
            line = basic_file.readline().rstrip("\n")

        # closing unnecessary files
        function_file.close()
        stm_file.close()
        basic_file.close()

        # creating voice handler for information
        self.voice_handler = VoiceHandler()

        # creating internet searcher
        self.internet_handler = InternetHandler()

        # creating uart handler
        self.uart_handler = UartHandler(baudrate=9600)

    def do_recognition_and_action(self, text):
        text = text.lower()

        function_code = [0 for x in range(len(self.function_list))]
        for i in range(0, len(self.function_list)):
            words = self.function_list[i].split()
            for j in range(1, len(words)):
                if re.search(words[j], text) is not None:
                    function_code[int(words[0])] += 1

        check_all_zero = True
        for x in range(0, len(function_code)):
            if function_code[x] != 0:
                check_all_zero = False
                break

        if check_all_zero:
            function_code = None
        else:
            function_code = function_code.index(max(function_code))

        if function_code is None:
            self.voice_handler.text_to_speech(
                text="Bu cümleden bir anlam çıkartamadım.")
            self.voice_handler.text_to_speech(
                text="Bu cümlede hangi fonksiyonu temsil ediyor acaba?")
            # func = input("Fonksiyon seç : ")
            func = helpFunctions.basic_numbers(
                self.voice_handler.speech_to_text())
            self.voice_handler.text_to_speech(
                text="Bu cümledeki anahtar kelime ne acaba?")
            # key = input("Anahtar Kelimeyi seç : ")
            key = str(self.voice_handler.speech_to_text())
            self.function_list.append(str(func) + " " + key)
            write_line = "\n" + str(func) + " " + key
            function_file = open("functions.txt", "a", encoding='utf8')
            function_file.writelines(write_line)
            function_file.close()

            function_code = func

        # user settings
        if function_code == 0:
            # user settings
            print("on going...")
        # stm32f4 functions
        elif function_code == 1:
            # stm32f4 codes
            stm_code = [0 for x in range(100)]
            for m in range(0, len(self.stm_list)):
                words = self.stm_list[m].split()
                for n in range(1, len(words)):
                    if re.search(words[n], text) is not None:
                        stm_code[int(words[0])] += 1

            stm_code = stm_code.index(max(stm_code))

            sent_data = []
            more_code = None
            on_off_code = None
            selection_code = None
            for p in range(0, 3):
                words = self.basic_list[p].split()
                for r in range(1, len(words)):
                    if re.search(words[r], text) is not None:
                        if int(words[0]) == 2:
                            more_code = 1
                            break
                        on_off_code = int(words[0])

            number = helpFunctions.search_number_string(text)
            if number is not None:
                number = int(number) * 10
                if on_off_code is None:
                    sent_data.append(stm_code)
                    sent_data.append(int(number))
                    self.uart_handler.writeReg(sent_data)
                elif on_off_code:
                    if more_code == 1:
                        # Uart handler must take value
                        # current_value = int(input("mevcut değeri al : "))
                        current_value = self.uart_handler.readReg(
                            [stm_code, "b"])
                        number = number / 10
                        if re.search("yüzde", text) is not None or re.search(
                                "%", text) is not None:
                            if current_value + number > 100:
                                temp_text = "Verilen değerde bir hata var. Şu anki değer " + str(
                                    current_value)
                                self.voice_handler.text_to_speech(temp_text)
                            else:
                                sent_data.append(stm_code)
                                sent_data.append((current_value + number) * 10)
                                # uart handler
                                self.uart_handler.writeReg(sent_data)
                        else:
                            sent_data.append(stm_code)
                            sent_data.append((current_value + number) * 10)
                            # uart handler
                            self.uart_handler.writeReg(sent_data)
                    else:
                        sent_data.append(stm_code)
                        sent_data.append(number)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)

                elif not on_off_code:
                    if more_code == 1:
                        # uart handler must take value
                        # current_value = int(input("mevcut değeri al : "))
                        current_value = self.uart_handler.readReg(
                            [stm_code, "b"])
                        number = number / 10
                        if re.search("yüzde", text) is not None or re.search(
                                "%", text) is not None:
                            if current_value - number < 0:
                                temp_text = "Verilen değerde bir hata var. Şu anki değer " + str(
                                    current_value)
                                self.voice_handler.text_to_speech(temp_text)
                            else:
                                sent_data.append(stm_code)
                                sent_data.append((current_value - number) * 10)
                                # uart handler
                                self.uart_handler.writeReg(sent_data)
                        else:
                            sent_data.append(stm_code)
                            sent_data.append((current_value - number) * 10)
                            # uart handler
                            self.uart_handler.writeReg(sent_data)

            else:
                for t in range(3, len(self.basic_list)):
                    words = self.basic_list[t].split()
                    for y in range(1, len(words)):
                        if re.search(words[y], text) is not None:
                            selection_code = int(words[0])
                if selection_code == 5:
                    sent_data.append(stm_code)
                    sent_data.append("b")
                    # uart handler
                    current_value = self.uart_handler.readReg(sent_data)
                    temp_text = "Aradığın değer" + str(current_value)
                    self.voice_handler.text_to_speech(temp_text)
                elif selection_code == 6:
                    sent_data.append(stm_code)
                    sent_data.append(4)
                    # uart handler
                    self.uart_handler.writeReg(sent_data)
                elif selection_code == 7:
                    sent_data.append(stm_code)
                    sent_data.append(3)
                    # uart handler
                    self.uart_handler.writeReg(sent_data)
                elif selection_code == 8:
                    sent_data.append(stm_code)
                    sent_data.append(0)
                    # uart handler
                    self.uart_handler.writeReg(sent_data)
                elif selection_code == 9:
                    sent_data.append(stm_code)
                    sent_data.append(5)
                    # uart handler
                    self.uart_handler.writeReg(sent_data)
                elif on_off_code == 1:
                    if selection_code == 4:
                        sent_data.append(stm_code)
                        sent_data.append(-10)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
                    elif selection_code == 3:
                        sent_data.append(stm_code)
                        sent_data.append(500)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
                    else:
                        sent_data.append(stm_code)
                        sent_data.append(1)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
                else:
                    if selection_code == 4:
                        sent_data.append(stm_code)
                        sent_data.append(-5)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
                    elif selection_code == 3:
                        sent_data.append(stm_code)
                        sent_data.append(500)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
                    else:
                        sent_data.append(stm_code)
                        sent_data.append(0)
                        # uart handler
                        self.uart_handler.writeReg(sent_data)
            print(sent_data)
        # internet functions
        elif function_code == 2:
            # internet search
            self.voice_handler.text_to_speech("Neyi aramamı istersin?")
            search = self.voice_handler.speech_to_text()
            if re.search("yemek", search) is not None or re.search(
                    "tarif", search) is not None:
                self.voice_handler.text_to_speech(
                    "Hangi yemeğin tarifini görmek istersin?")
                recipe = self.voice_handler.speech_to_text()
                self.internet_handler.search_web_food(recipe)
            elif re.search("müzik", search) is not None:
                if re.search("ingilizce", search) is not None or re.search(
                        "İngilizce", search) is not None:
                    self.voice_handler.text_to_speech(
                        "Tabii, hangi müziği dinlemek istersin?")
                    self.voice_handler.language = "en-us"
                    music_name = self.voice_handler.speech_to_text()
                    self.voice_handler.language = "tr"
                else:
                    self.voice_handler.text_to_speech(
                        "Tabii, hangi müziği dinlemek istersin?")
                    music_name = self.voice_handler.speech_to_text()

                self.internet_handler.check_web_music(music_name)
            else:
                self.internet_handler.check_web_google(search)
        # weather forecast function
        elif function_code == 3:
            # country = input("Ülke : ")
            country = "tr"
            # city = input("Şehir : ")
            self.voice_handler.text_to_speech(
                "Hangi şehirin hava durumunu öğrenmek istersin")
            city = self.voice_handler.speech_to_text()
            location = city + "," + country
            self.voice_handler.text_to_speech(
                helpFunctions.check_weather(location))
            self.text = helpFunctions.check_weather(location)
        # date and hour questioning
        elif function_code == 4:
            if re.search("saat", text) is not None:
                self.voice_handler.text_to_speech(helpFunctions.check_time())
            else:
                self.voice_handler.text_to_speech(helpFunctions.check_date())
        # close the program
        elif function_code == 5:
            sys.exit()
 def __init__(self):
     self.voiceh = VoiceHandler()
class CommandHandler:
    def __init__(self):
        self.voiceh = VoiceHandler()

    """ Software shortcuts """

    def open_firefox(self, *args, **kwargs):
        print("Opening firefox")
        os.chdir("C:\\Program Files\\Mozilla Firefox")

        if not args:
            os.system("firefox.exe")
        else:
            parsed_url = None
            for i in ["be", "com", "nl", "org", "co"]:
                current_url = "http://www.%s.%s" % ("".join(
                    args[0]).lower(), i)
                try:
                    header = httplib2.Http()
                    response = header.request(current_url, "HEAD")
                    if int(response[0]["status"]) < 400:
                        parsed_url = current_url
                except Exception:
                    continue
                finally:
                    if parsed_url:
                        break
            if parsed_url:
                os.system("firefox.exe %s" % parsed_url)
            else:
                self.voiceh.textToSpeech("Unable to find correct url.")

    def open_partitions(self):
        os.system("diskmgmt.msc")

    def open_notepad(self):
        os.system("notepad.exe")

    def open_calculator(self):
        os.system("calc")

    def open_terminal(self):
        os.system("start cmd.exe")

    """ Power control """

    def sleep(self):
        os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

    def shutdown(self):
        os.system("shutdown.exe -s -t 0")

    def restart(self):
        os.system("shutdown -r -t 5")

    """ Software Control """

    def play_music(self, play=True):
        self.stop_music()
        os.system("spotify.exe")
        time.sleep(5)
        if play:
            play_location = pyautogui.locateOnScreen(
                "assets/images/spotify_play.png", confidence=.9)
            if play_location:
                pyautogui.click(
                    int(play_location.left + (play_location.width / 2)),
                    int(play_location.top + (play_location.height / 2)))

    def next_song(self):
        window = pygetwindow.getWindowsWithTitle("spotify")
        if window:
            window[0].activate()
            next_loc = pyautogui.locateOnScreen("assets/images/next_song.png",
                                                confidence=.9)
            if next_loc:
                pyautogui.click(int(next_loc.left + (next_loc.width / 2)),
                                int(next_loc.top + (next_loc.height / 2)))

    def previous_song(self):
        window = pygetwindow.getWindowsWithTitle("spotify")
        if window:
            window[0].activate()
            prev_loc = pyautogui.locateOnScreen(
                "assets/images/previous_song.png", confidence=.9)
            if prev_loc:
                pyautogui.click(int(prev_loc.left + (prev_loc.width / 2)),
                                int(prev_loc.top + (prev_loc.height / 2)))

    def stop_music(self):
        os.system("taskkill /f /im spotify.exe")

    def play_song(self, *args, **kwargs):
        client_id = ""
        client_secret = ""

        def connect_to_spotify():
            client_credentials_manager = SpotifyClientCredentials(
                client_id=client_id, client_secret=client_secret)
            sp = spotipy.Spotify(
                client_credentials_manager=client_credentials_manager)
            return sp

        def search_track(search_query: str):
            conn = connect_to_spotify()
            result = conn.search(search_query, type="track")
            found_songs = {}
            for set in result["tracks"]["items"]:
                found_songs[set["name"].lower()] = set["uri"]
            return found_songs

        if client_id and client_secret:
            songs = search_track(args[0])
            os.system("start {}".format(songs.get(list(songs.keys())[0])))
            window = pygetwindow.getWindowsWithTitle("spotify")
            window[0].activate()
            time.sleep(0.5)
            loc = pyautogui.locateOnScreen(
                "assets/images/highlighted_song.png", confidence=.9)
            if loc:
                pyautogui.click(loc.left,
                                int(loc.top + (loc.height / 2)),
                                clicks=2)

    """ Miscellaneous """

    def tell_me_a_joke(self):
        print("Telling a joke")
        # Fetch jokes from API
        url = "https://official-joke-api.appspot.com/random_ten"
        url_response = eval(requests.get(url).text)
        random_joke = random.choice(url_response)
        self.voiceh.textToSpeech(random_joke["setup"])
        self.voiceh.textToSpeech(random_joke["punchline"])

    def commands(self):
        self.voiceh.textToSpeech("Voice commands are: ")
        for i in [
                k for k in list(self.__class__.__dict__.keys())
                if "__" not in k
        ]:
            i = i if not "_" in i else i.replace("_", " ")
            self.voiceh.textToSpeech(i)
Beispiel #7
0
class CommandHandler:
    def __init__(self):
        self.voiceh = VoiceHandler()

    """ Software shortcuts """

    def open_firefox(self, *args, **kwargs):
        print("Opening firefox")
        os.chdir("C:\\Program Files\\Mozilla Firefox")

        if not args:
            os.system("firefox.exe")
        else:
            parsed_url = None
            for i in ["be", "com", "nl", "org", "co"]:
                current_url = "http://www.%s.%s" % ("".join(
                    args[0]).lower(), i)
                try:
                    header = httplib2.Http()
                    response = header.request(current_url, "HEAD")
                    if int(response[0]["status"]) < 400:
                        parsed_url = current_url
                except Exception:
                    continue
                finally:
                    if parsed_url:
                        break
            if parsed_url:
                os.system("firefox.exe %s" % parsed_url)
            else:
                self.voiceh.textToSpeech("Unable to find correct url.")

    def open_partitions(self):
        os.system("diskmgmt.msc")

    def open_notepad(self):
        os.system("notepad.exe")

    def open_calculator(self):
        os.system("calc")

    def open_terminal(self):
        os.system("start cmd.exe")

    """ Power control """

    def sleep(self):
        os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

    def shutdown(self):
        os.system("shutdown.exe -s -t 0")

    def restart(self):
        os.system("shutdown -r -t 5")

    """ Software Control """

    def play_music(self, play=True):
        self.stop_music()
        os.system("spotify.exe")
        time.sleep(5)
        if play:
            play_location = pyautogui.locateOnScreen(
                "assets/images/spotify_play.png", confidence=.9)
            if play_location:
                pyautogui.click(
                    int(play_location.left + (play_location.width / 2)),
                    int(play_location.top + (play_location.height / 2)))

    def next_song(self):
        window = pygetwindow.getWindowsWithTitle("spotify")
        if window:
            window[0].activate()
            next_loc = pyautogui.locateOnScreen("assets/images/next_song.png",
                                                confidence=.9)
            if next_loc:
                pyautogui.click(int(next_loc.left + (next_loc.width / 2)),
                                int(next_loc.top + (next_loc.height / 2)))

    def previous_song(self):
        window = pygetwindow.getWindowsWithTitle("spotify")
        if window:
            window[0].activate()
            prev_loc = pyautogui.locateOnScreen(
                "assets/images/previous_song.png", confidence=.9)
            if prev_loc:
                pyautogui.click(int(prev_loc.left + (prev_loc.width / 2)),
                                int(prev_loc.top + (prev_loc.height / 2)))

    def stop_music(self):
        os.system("taskkill /f /im spotify.exe")

    def play_song(self, *args, **kwargs):
        client_id = ""
        client_secret = ""

        def connect_to_spotify():
            client_credentials_manager = SpotifyClientCredentials(
                client_id=client_id, client_secret=client_secret)
            sp = spotipy.Spotify(
                client_credentials_manager=client_credentials_manager)
            return sp

        def search_track(search_query: str):
            conn = connect_to_spotify()
            result = conn.search(search_query, type="track")
            found_songs = {}
            for set in result["tracks"]["items"]:
                found_songs[set["name"].lower()] = set["uri"]
            return found_songs

        if client_id and client_secret:
            songs = search_track(args[0])
            os.system("start {}".format(songs.get(list(songs.keys())[0])))
            window = pygetwindow.getWindowsWithTitle("spotify")
            window[0].activate()
            time.sleep(0.5)
            loc = pyautogui.locateOnScreen(
                "assets/images/highlighted_song.png", confidence=.9)
            if loc:
                pyautogui.click(loc.left,
                                int(loc.top + (loc.height / 2)),
                                clicks=2)
        else:
            self.voiceh.textToSpeech(
                "No developer application set, please follow the guide on the logged link in terminal"
            )
            self.voiceh.textToSpeech(
                "Afterwards set the client id/secret in play song function")
            print(
                "https://developer.spotify.com/documentation/general/guides/app-settings/?fbclid=IwAR1xHP62ZxxsI2GqAibc0tE5EPEUv0_G_uAkPoMQODtJz_il8tEsJbX5P0c#register-your-app"
            )

    """ Miscellaneous """

    def tell_me_a_joke(self):
        print("Telling a joke")
        # Fetch jokes from API
        url = "https://official-joke-api.appspot.com/random_ten"
        url_response = eval(requests.get(url).text)
        random_joke = random.choice(url_response)
        self.voiceh.textToSpeech(random_joke["setup"])
        self.voiceh.textToSpeech(random_joke["punchline"])

    def movie_suggestion(self, *args, **kwargs):
        genre = ["horror", "action", "sci-fi", "thriller", "crime", "mystery"]
        if args:
            highest_ratio = 0
            genre_name = ""
            for i in genre:
                search_correct = difflib.SequenceMatcher(isjunk=None,
                                                         a=args[0],
                                                         b=i)
                current_ratio = search_correct.ratio() * 100
                if current_ratio > highest_ratio:
                    highest_ratio = current_ratio
                    genre_name = i
            genre = genre_name
        else:
            genre = random.choice(genre)

        page = requests.get(
            "https://www.imdb.com/search/title/?genres={genre}".format(
                genre=genre))
        soupobj = bs4.BeautifulSoup(page.text, features="html.parser")
        raw_movie_list = soupobj.find_all("div", class_="lister-item-content")
        movies = {}
        for movie in raw_movie_list:
            subsoup = bs4.BeautifulSoup(str(movie), features="html.parser")
            # scrape title out of code.
            title = [
                i for i in subsoup.find_all("a")
                if "title/tt" in str(i) and not "vote" in str(i)
            ][0]
            title = str(title)[str(title).find('">') + 2:str(title).find('</')]
            for indx, i in enumerate(subsoup.find_all("p",
                                                      class_="text-muted")):
                if indx != 1:
                    continue
                description = str(i)[str(i).find("\n") +
                                     1:str(i).find("</p")].strip()
                movies[title] = description

        random_movie = random.choice(list(movies.keys()))
        self.voiceh.textToSpeech(random_movie)
        self.voiceh.textToSpeech(movies[random_movie])
        print("name: %s\nDescription: %s" %
              (random_movie, movies[random_movie]))
        time.sleep(1)

    def commands(self):
        self.voiceh.textToSpeech("Voice commands are: ")
        for i in [
                k for k in list(self.__class__.__dict__.keys())
                if "__" not in k
        ]:
            i = i if not "_" in i else i.replace("_", " ")
            self.voiceh.textToSpeech(i)