#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2

    Pyspeech capture some 'keywords'


    Hi, I'm looking for a way to use pyspeech to capture some keywords. For example, when i said the words "Play music", the python will have to run the function to play music. However, I have to use the specific word 'play music' to run that function.

    My question is, is there a way to capture only a word 'music'?, no matters what i'm saying but if the pyspeech can capture the word 'music' in my sentence, it has to run the music function. For example, if i said 'I want to listen my music' or 'Run music' something like this it has to play music anyway. If you have some idea or solution to solve this problem, please help

    Here is my code:

    Code:
    import speech, pymedia
    
    player = pymedia.Player()
    player.start()
    
    def playMusic():
       player.startPlayback('C:\MusicLibrary\song.mp3')
    
    def command_callback(phrase, listener):
       if phrase == 'Music':     
              speech.say("Preparing music. . .")
              playMusic()
    
    Command = speech.listenfor(['Music', 'Play music'],command_callback)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Looks like nobody knows about this stuff, anyway I've tried something and I hope some of you guys might have suggestions to me. Here's what I'm doing right now:

    Code:
    import speech, re, pymedia, os
    
    player = pymedia.Player()
    player.start()
    ListSong = []
    i = 0
    for file in os.listdir('C:\MusicLibrary'):
       ListSong.append('C:\MusicLibrary'+'\\'+file)
    
    response = []
    response = speech.input(' ')
      
    def STSong():
       global i 
       player.startPlayback(ListSong[i])
    
    def command_callback(phrase, listener):
      if response != []:
        m = re.search("music", response)
        k = m.group(0)
        speech.say(k)
        phrase = k
        if phrase == "music":
          STSong()
    
    AllCommand = speech.listenfor(('%s', response), command_callback)
    As you can see, this code can capture the word 'music' from what I said, but there's two problems right now.

    1. I've to repeat what I'm saying twice whenever I run the scripts because the first said is "response = speech.input(' ')" and the second one is in the command_callback() to make the music played.

    2. After I gave the input to "response", it always keeps the value, is there a way to refresh and return the "response" back to the beginning? I mean after I gave the input to "response" and it played the music for me. I want this "response" to be cleared and go back to the top for waiting the new command. If somebody knows about this, please help me find out. I've been looking forward to see you guys' opinion

IMN logo majestic logo threadwatch logo seochat tools logo