2017-08-01 73 views
0

我需要建立一個機器人,我想問一下如何開始這樣的任務。開發一個機器人

我的理解是,我需要一個框架,它提供語音到文本。

然後我需要生成意圖從文本。然後用我的算法找出做什麼和響應。

能否請您通過引導我到框架,以尋找我所提到的2個目的幫幫我嗎?

謝謝

+0

,你可以找到很多的框架,這樣做在很多langage 的通過你說什麼,你想建立一個帶語音識別的機器人,對吧?你有很多他們在Python中,但你可以'欺騙',並使用谷歌API也可以這樣做 – sheplu

+0

嗨@sheplu,我很熟悉java和python,你能推薦任何具體的東西嗎? Google的API提供了意圖,對嗎?爲什麼它是'欺騙'? – user1002065

+0

我想在Python最好的一個是這樣的一個https://pypi.python.org/pypi/SpeechRecognition/ – sheplu

回答

0

SpeechRecognition是一個不錯的選擇!

這裏有一個工作代碼:

import speech_recognition as sr 

# obtain audio from the microphone 
r = sr.Recognizer() 
with sr.Microphone() as source: 
    print("Please wait. Calibrating microphone...") 
    # listen for 5 seconds and create the ambient noise energy level 
    r.adjust_for_ambient_noise(source, duration=5) 
    print("Say something!") 
    audio = r.listen(source) 

# recognize speech using Sphinx 
try: 
    print("Sphinx thinks you said '" + r.recognize_sphinx(audio) + "'") 
except sr.UnknownValueError: 
    print("Sphinx could not understand audio") 
except sr.RequestError as e: 
    print("Sphinx error; {0}".format(e)) 

上述以外,我發現了一個利基腳本,讓你運行自己電報機器人

// Telegram bot 
// which converts given voice(speech) to text 
// using wit.ai HTTP API 
// 
// [email protected] 
// 
// last update: 2016.05.17. 
package main 

import (
    "fmt" 
    "io" 
    "io/ioutil" 
    "log" 
    "net/http" 
    "os" 
    "os/exec" 

    bot "github.com/meinside/telegram-bot-go" 
    witai "github.com/meinside/wit.ai-go" 
) 

const (
    TelegramApiToken = ":abcdefghijklmn_ABCDEFGHIJKLMNOPQRST" // XXX - Edit this value to yours 
    WitaiApiToken = "ABCDEFGHIJKLMNOPQRST"     // XXX - Edit this value to yours 

    MonitorIntervalSeconds = 1 
    Verbose    = false // XXX - set this to 'true' for verbose messages 

    TempDir  = "/tmp"     // XXX - Edit this value to yours 
    FfmpegBinPath = "/usr/local/bin/ffmpeg" // XXX - Edit this value to yours 
) 

// Download given url and return the downloaded path 
func downloadFile(url string) (filepath string, err error) { 
    log.Printf("> downloading voice file: %s\n", url) 

    var file *os.File 
    if file, err = ioutil.TempFile(TempDir, "downloaded_"); err == nil { 
     filepath = file.Name() 

     defer file.Close() 

     var response *http.Response 
     if response, err = http.Get(url); err == nil { 
      defer response.Body.Close() 

      if _, err = io.Copy(file, response.Body); err == nil { 
       log.Printf("> finished downloading voice file: %s\n", filepath) 
      } 
     } 
    } 

    return filepath, err 
} 

// Convert .ogg to .mp3 (using ffmpeg) 
// 
// NOTE: wit.ai doesn't support stereo sound for now 
// (https://wit.ai/docs/http/20160516#post--speech-link) 
func oggToMp3(oggFilepath string) (mp3Filepath string, err error) { 
    mp3Filepath = fmt.Sprintf("%s.mp3", oggFilepath) 

    // $ ffmpeg -i input.ogg -ac 1 output.mp3 
    params := []string{"-i", oggFilepath, "-ac", "1", mp3Filepath} 
    cmd := exec.Command("ffmpeg", params...) 

    if _, err = cmd.CombinedOutput(); err != nil { 
     mp3Filepath = "" 
    } 

    return mp3Filepath, err 
} 

// Download a file from given url and convert it to a text. 
// 
// Downloaded or converted files will be deleted automatically. 
func speechToText(w *witai.Client, fileUrl string) (text string, err error) { 
    var oggFilepath, mp3Filepath string 

    // download .ogg, 
    if oggFilepath, err = downloadFile(fileUrl); err == nil { 
     // .ogg => .mp3, 
     if mp3Filepath, err = oggToMp3(oggFilepath); err == nil { 
      // .mp3 => text 
      if result, err := w.QuerySpeechMp3(mp3Filepath, nil, "", "", 1); err == nil { 
       log.Printf("> analyzed speech result: %+v\n", result) 

       if result.Text != nil { 
        text = fmt.Sprintf("\"%s\"", *result.Text) 

        /* 
         // traverse for more info 
         sessionId := "abcdef" 
         if results, err := w.ConverseAll(sessionId, *result.Text, nil); err == nil { 
          for i, r := range results { 
           log.Printf("> converse[%d] result: %v\n", i, r) 
          } 
         } else { 
          log.Printf("failed to converse: %s\n", err) 
         } 
        */ 
       } 
      } 

      // delete converted file 
      if err = os.Remove(mp3Filepath); err != nil { 
       log.Printf("*** failed to delete converted file: %s\n", mp3Filepath) 
      } 
     } else { 
      log.Printf("*** failed to convert .ogg to .mp3: %s\n", err) 
     } 

     // delete downloaded file 
     if err = os.Remove(oggFilepath); err != nil { 
      log.Printf("*** failed to delete downloaded file: %s\n", oggFilepath) 
     } 
    } 

    return text, err 
} 

func main() { 
    b := bot.NewClient(TelegramApiToken) 
    b.Verbose = Verbose 

    w := witai.NewClient(WitaiApiToken) 
    w.Verbose = Verbose 

    if unhooked := b.DeleteWebhook(); unhooked.Ok { // delete webhook 
     // wait for new updates 
     b.StartMonitoringUpdates(0, MonitorIntervalSeconds, func(b *bot.Bot, u bot.Update, err error) { 
      if err == nil && u.HasMessage() { 
       b.SendChatAction(u.Message.Chat.Id, bot.ChatActionTyping) // typing... 

       if u.Message.HasVoice() { // when voice is received, 
        if sent := b.GetFile(u.Message.Voice.FileId); sent.Ok { 
         if message, err := speechToText(w, b.GetFileUrl(*sent.Result)); err == nil { 
          if len(message) <= 0 { 
           message = "Failed to analyze your voice." 
          } 
          if sent := b.SendMessage(u.Message.Chat.Id, &message, map[string]interface{}{}); !sent.Ok { 
           log.Printf("*** failed to send message: %s\n", *sent.Description) 
          } 
         } else { 

          message := fmt.Sprintf("Failed to analyze your voice: %s", err) 
          if sent := b.SendMessage(u.Message.Chat.Id, &message, map[string]interface{}{}); !sent.Ok { 
           log.Printf("*** failed to send message: %s\n", *sent.Description) 
          } 
         } 
        } 
       } else { // otherwise, 
        message := "Let me hear your voice." 
        if sent := b.SendMessage(u.Message.Chat.Id, &message, map[string]interface{}{}); !sent.Ok { 
         log.Printf("*** failed to send message: %s\n", *sent.Description) 
        } 
       } 
      } 
     }) 
    } else { 
     panic("failed to delete webhook") 
    } 
} 

$ wget的 https://gist.githubusercontent.com/meinside/56f2616c61f59768b4ce487bea4f9fd5/raw/e9984ec1ac46a47d1b7b5ccf1a178147588c3552/stt_bot_sample.go

運行你編輯代碼:

$ go run stt_bot_sample.go 

Bot in Action ! (image)