Bots 开发机器人

Bots 开发机器人,bots,speech-to-text,intentservice,Bots,Speech To Text,Intentservice,我需要建立一个机器人,我想问如何开始这样的任务 我的理解是,我需要一个框架来提供从语音到文本的转换 然后我需要从文本中生成意图。然后使用我的算法来找出该做什么和响应 你能帮我找到我提到的两个目的的框架吗 谢谢语音识别是一个不错的选择 下面是一个工作代码: 除上述内容外,我还发现了一个小众脚本,允许您运行自己的电报机器人 你可以在很多语言中找到很多框架来实现这一点,通过你所说的,你想用语音识别构建一个机器人,对吗?Python中有很多这样的工具,但是你可以“作弊”并使用Google API来实现。

我需要建立一个机器人,我想问如何开始这样的任务

我的理解是,我需要一个框架来提供从语音到文本的转换

然后我需要从文本中生成意图。然后使用我的算法来找出该做什么和响应

你能帮我找到我提到的两个目的的框架吗


谢谢

语音识别是一个不错的选择

下面是一个工作代码:

除上述内容外,我还发现了一个小众脚本,允许您运行自己的电报机器人


你可以在很多语言中找到很多框架来实现这一点,通过你所说的,你想用语音识别构建一个机器人,对吗?Python中有很多这样的工具,但是你可以“作弊”并使用Google API来实现。tooHi@sheplu,我对java和Python很熟悉,你能推荐一些具体的工具吗?谷歌的API提供了意图,对吗?为什么这是一个“欺骗”?我认为python中最好的一个是@sheplu所说的Google语音API,它的免费时间不超过每月6000万次作弊,因为你不必编写自己的框架@我忘了编辑我的评论来添加这个。谢谢!
 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
//
// meinside@gmail.com
//
// 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 = "01234567:abcdefghijklmn_ABCDEFGHIJKLMNOPQRST" // XXX - Edit this value to yours
    WitaiApiToken    = "01234567ABCDEFGHIJKLMNOPQRST"                 // 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 := "01234567890abcdef"
                        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")
    }
}
$ go run stt_bot_sample.go