stt.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package yask
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. )
  11. // STTConfigDefault returns STTConfig with default parameters
  12. func STTConfigDefault(yaFolderID, yaAPIKey string, data io.Reader) *STTConfig {
  13. return &STTConfig{
  14. Lang: "ru-RU",
  15. Topic: "general",
  16. ProfanityFilter: false,
  17. Format: FormatLPCM,
  18. Rate: Rate8k,
  19. YaFolderID: yaFolderID,
  20. YaAPIKey: yaAPIKey,
  21. Data: data,
  22. }
  23. }
  24. // STTConfig is config for speech to text methods
  25. type STTConfig struct {
  26. Lang string
  27. Topic string
  28. ProfanityFilter bool
  29. Format string
  30. Rate int
  31. YaFolderID string
  32. YaAPIKey string
  33. Data io.Reader
  34. }
  35. // uri returns url with get parameters for http request
  36. func (s *STTConfig) uri() string {
  37. vars := url.Values{
  38. "lang": []string{s.Lang},
  39. "topic": []string{s.Topic},
  40. "profanityFilter": []string{strconv.FormatBool(s.ProfanityFilter)},
  41. "format": []string{s.Format},
  42. "sampleRateHertz": []string{strconv.FormatInt(int64(s.Rate), 10)},
  43. "folderId": []string{s.YaFolderID},
  44. }
  45. url := fmt.Sprintf("%v?%v", YaSTTUrl, vars.Encode())
  46. return url
  47. }
  48. // SpeechToTextShort returns text from a PCM or OGG sound stream using the service Yandex Speech Kit
  49. func SpeechToTextShort(conf *STTConfig) (string, error) {
  50. req, err := http.NewRequest(
  51. "POST",
  52. conf.uri(),
  53. conf.Data,
  54. )
  55. if err != nil {
  56. return "", err
  57. }
  58. req.Header.Set("Transfer-encoding", "chunked")
  59. req.Header.Set("Authorization", fmt.Sprintf("Api-Key %v", conf.YaAPIKey))
  60. cl := new(http.Client)
  61. resp, err := cl.Do(req)
  62. if err != nil {
  63. return "", err
  64. }
  65. if resp.StatusCode != http.StatusOK {
  66. return "", unmarshallYaError(resp.Body)
  67. }
  68. rSource, err := ioutil.ReadAll(resp.Body)
  69. if err != nil {
  70. return "", err
  71. }
  72. m := make(map[string]interface{})
  73. if err = json.Unmarshal(rSource, &m); err != nil {
  74. return "", err
  75. }
  76. result := fmt.Sprint(m["result"])
  77. return result, nil
  78. }