-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from dvonthenen/dv/prerecorded-object-definition
Encapsulate the Prerecorded Object to Hide Internal Details
- Loading branch information
Showing
30 changed files
with
1,571 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package prerecorded | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
// ErrInvalidInput required input was not found | ||
ErrInvalidInput = errors.New("required input was not found") | ||
|
||
// ErrInvalidURIExtension couldn't find a period to indicate a file extension | ||
ErrInvalidURIExtension = errors.New("couldn't find a period to indicate a file extension") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package interfaces | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func (resp *PreRecordedResponse) ToWebVTT() (string, error) { | ||
if resp.Results.Utterances == nil { | ||
return "", errors.New("this function requires a transcript that was generated with the utterances feature") | ||
} | ||
|
||
vtt := "WEBVTT\n\n" | ||
|
||
vtt += "NOTE\nTranscription provided by Deepgram\nRequest ID: " + resp.Metadata.RequestId + "\nCreated: " + resp.Metadata.Created + "\n\n" | ||
|
||
for i, utterance := range resp.Results.Utterances { | ||
utterance := utterance | ||
start := SecondsToTimestamp(utterance.Start) | ||
end := SecondsToTimestamp(utterance.End) | ||
vtt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript) | ||
} | ||
return vtt, nil | ||
} | ||
|
||
func (resp *PreRecordedResponse) ToSRT() (string, error) { | ||
if resp.Results.Utterances == nil { | ||
return "", errors.New("this function requires a transcript that was generated with the utterances feature") | ||
} | ||
|
||
srt := "" | ||
|
||
for i, utterance := range resp.Results.Utterances { | ||
utterance := utterance | ||
start := SecondsToTimestamp(utterance.Start) | ||
end := SecondsToTimestamp(utterance.End) | ||
end = strings.ReplaceAll(end, ".", ",") | ||
srt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript) | ||
|
||
} | ||
return srt, nil | ||
} | ||
|
||
func SecondsToTimestamp(seconds float64) string { | ||
hours := int(seconds / 3600) | ||
minutes := int((seconds - float64(hours*3600)) / 60) | ||
seconds = seconds - float64(hours*3600) - float64(minutes*60) | ||
return fmt.Sprintf("%02d:%02d:%02.3f", hours, minutes, seconds) | ||
} | ||
|
||
func GetJson(resp *http.Response, target interface{}) error { | ||
defer resp.Body.Close() | ||
|
||
return json.NewDecoder(resp.Body).Decode(target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package interfaces | ||
|
||
// share/common structs | ||
type Metadata struct { | ||
TransactionKey string `json:"transaction_key"` | ||
RequestId string `json:"request_id"` | ||
Sha256 string `json:"sha256"` | ||
Created string `json:"created"` | ||
Duration float64 `json:"duration"` | ||
Channels int `json:"channels"` | ||
Models []string `json:"models"` | ||
ModelInfo map[string]struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
Arch string `json:"arch"` | ||
} `json:"model_info"` | ||
Warnings []*Warning `json:"warnings,omitempty"` | ||
} | ||
|
||
type Warning struct { | ||
Parameter string `json:"parameter"` | ||
Type string `json:"type"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type Hit struct { | ||
Confidence float64 `json:"confidence"` | ||
Start float64 `json:"start"` | ||
End float64 `json:"end"` | ||
Snippet string `json:"snippet"` | ||
} | ||
|
||
type Search struct { | ||
Query string `json:"query"` | ||
Hits []Hit `json:"hits"` | ||
} | ||
|
||
type WordBase struct { | ||
Word string `json:"word"` | ||
Start float64 `json:"start"` | ||
End float64 `json:"end"` | ||
Confidence float64 `json:"confidence"` | ||
Speaker *int `json:"speaker,omitempty"` | ||
SpeakerConfidence float64 `json:"speaker_confidence,omitempty"` | ||
Punctuated_Word string `json:"punctuated_word,omitempty"` | ||
Sentiment string `json:"sentiment,omitempty"` | ||
} | ||
|
||
type Alternative struct { | ||
Transcript string `json:"transcript"` | ||
Confidence float64 `json:"confidence"` | ||
Words []WordBase `json:"words"` | ||
Summaries []*SummaryV1 `json:"summaries,omitempty"` | ||
Paragraphs *ParagraphGroup `json:"paragraphs,omitempty"` | ||
Topics []*TopicBase `json:"topics,omitempty"` | ||
Entities []*EntityBase `json:"entities,omitempty"` | ||
} | ||
|
||
type ParagraphGroup struct { | ||
Transcript string `json:"transcript"` | ||
Paragraphs []ParagraphBase `json:"paragraphs"` | ||
} | ||
|
||
type ParagraphBase struct { | ||
Sentences []SentenceBase `json:"sentences"` | ||
NumWords int `json:"num_words"` | ||
Start float64 `json:"start"` | ||
End float64 `json:"end"` | ||
} | ||
|
||
type SentenceBase struct { | ||
Text string `json:"text"` | ||
Start float64 `json:"start"` | ||
End float64 `json:"end"` | ||
} | ||
|
||
type EntityBase struct { | ||
Label string `json:"label"` | ||
Value string `json:"value"` | ||
Confidence float64 `json:"confidence"` | ||
StartWord int `json:"start_word"` | ||
EndWord int `json:"end_word"` | ||
} | ||
|
||
type TopicBase struct { | ||
Text string `json:"text"` | ||
StartWord int `json:"start_word"` | ||
EndWord int `json:"end_word"` | ||
Topics []Topic `json:"topics"` | ||
} | ||
|
||
type Topic struct { | ||
Topic string `json:"topic"` | ||
Confidence float64 `json:"confidence"` | ||
} | ||
|
||
type Channel struct { | ||
Search []*Search `json:"search,omitempty"` | ||
Alternatives []Alternative `json:"alternatives"` | ||
DetectedLanguage string `json:"detected_language,omitempty"` | ||
} | ||
|
||
type Utterance struct { | ||
Start float64 `json:"start"` | ||
End float64 `json:"end"` | ||
Confidence float64 `json:"confidence"` | ||
Channel int `json:"channel"` | ||
Transcript string `json:"transcript"` | ||
Words []WordBase `json:"words"` | ||
Speaker *int `json:"speaker,omitempty"` | ||
Id string `json:"id"` | ||
} | ||
|
||
type Results struct { | ||
Utterances []*Utterance `json:"utterances,omitempty"` | ||
Channels []Channel `json:"channels"` | ||
Summary *SummaryV2 `json:"summary,omitempty"` | ||
} | ||
|
||
type SummaryV1 struct { | ||
Summary string `json:"summary"` | ||
StartWord int `json:"start_word"` | ||
EndWord int `json:"end_word"` | ||
} | ||
|
||
type SummaryV2 struct { | ||
Short string `json:"short"` | ||
Result string `json:"result"` | ||
} | ||
|
||
// Response | ||
type PreRecordedResponse struct { | ||
Request_id string `json:"request_id,omitempty"` | ||
Metadata Metadata `json:"metadata"` | ||
Results Results `json:"results"` | ||
} |
Oops, something went wrong.