Skip to content

Feat/dictation endpoint #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {

## Testing

Unit tests are executed by Github Actions, but Integration tests have to be executed manually by providing a valid token via `WITAI_INTEGRATION_TOKEN` env var.
Unit tests are executed by Github Actions.

### Unit tests

Expand All @@ -49,6 +49,8 @@ go test -race -v

### Integration tests

Integration tests have to be executed manually by providing a valid token via `WITAI_INTEGRATION_TOKEN` env var.

Integration tests are connecting to real Wit.ai API, so you need to provide a valid token:

```
Expand Down
51 changes: 51 additions & 0 deletions dictation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

package witai

import (
"encoding/json"
"io"
"net/http"
)

type DictationRequest struct {
File io.Reader
ContentType string
}

type DictationToken struct {
End int `json:"end"`
Start int `json:"start"`
Token string `json:"token"`
}

type DictationSpeech struct {
Confidence float64 `json:"confidence"`
Tokens []DictationToken `json:"tokens"`
}

type DictationResponse struct {
Speech DictationSpeech `json:"speech"`
Text string `json:"text"`
Type string `json:"type"`
}

// Dictation - Returns the text transcription from an audio file or stream.
func (c *Client) Dictation(req DictationRequest) (*DictationResponse, error) {
resp, err := c.request(http.MethodPost, "/dictation", req.ContentType, req.File)
if err != nil {
return nil, err
}

defer resp.Close()

var msgResp *DictationResponse
decoder := json.NewDecoder(resp)
for {
err := decoder.Decode(&msgResp)
if err != nil {
break
}
}
return msgResp, err
}
24 changes: 24 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,27 @@ func getIntegrationClient() *Client {
})
return c
}

func TestIntegrationDictation(t *testing.T) {
c := getIntegrationClient()

f, err := os.Open("./testdata/test.mp3")
if err != nil {
t.Fatalf("unable to open test file, err: %v", err)
}
defer f.Close()

req := DictationRequest{
File: f,
ContentType: "audio/mpeg3",
}

resp, err := c.Dictation(req)
if err != nil {
t.Fatalf("unexpected err, got %v", err)
}

if resp.Text != "Hi this is a test file" {
t.Fatalf("unexpected transcript, got %s", resp.Text)
}
}
Binary file added testdata/.DS_Store
Binary file not shown.
Binary file added testdata/test.mp3
Binary file not shown.
Loading