-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.go
More file actions
152 lines (127 loc) · 4.55 KB
/
structs.go
File metadata and controls
152 lines (127 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package rec
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
type Utterance struct {
UserName string `json:"username"`
Text string `json:"text"`
RecordingID string `json:"recording_id"`
Message string `json:"message"`
Num int `json:"num"`
Of int `json:"of"`
}
type UttList struct {
Name string `json:"name"`
Utts []Utterance `json:"utts"`
}
type Audio struct {
FileType string `json:"file_type"`
Data string `json:"data,omitempty"`
}
type ProcessInput struct {
ScriptName string `json:"scriptname"`
UserName string `json:"username"`
Audio Audio `json:"audio"`
Text string `json:"text"`
RecordingID string `json:"recording_id"`
Weights map[string]float64 `json:"weights,omitempty"`
}
type AudioDir struct {
BaseDir string
UserDir string
}
func (ad AudioDir) Path() string {
return filepath.Join(ad.BaseDir, ad.UserDir)
}
type AudioRef struct {
Dir AudioDir
BaseName string // without file extension
}
func NewAudioRef(baseDir string, userDir string, baseName string) AudioRef {
dir := AudioDir{BaseDir: baseDir, UserDir: userDir}
return AudioRef{Dir: dir, BaseName: baseName}
}
func (ar AudioRef) Path(extension string) string {
fName := ar.FileName(extension)
return filepath.Join(ar.Dir.Path(), fName)
}
func (ar AudioRef) FileName(extension string) string {
return fmt.Sprintf("%s%s", ar.BaseName, extension)
}
type AudioFile struct {
BasePath AudioRef
Extension string
}
func NewAudioFile(baseDir string, userDir string, baseName string, extension string) AudioFile {
audioRef := NewAudioRef(baseDir, userDir, baseName)
return AudioFile{BasePath: audioRef, Extension: extension}
}
func (af AudioFile) Path() string {
fName := af.BasePath.FileName(af.Extension)
return filepath.Join(af.BasePath.Dir.Path(), fName)
}
func (af AudioFile) AudioDir() AudioDir {
return af.BasePath.Dir
}
type ProcessResponse struct {
Ok bool `json:"ok"`
Confidence float64 `json:"confidence"` // value between 0 and 1
RecognitionResult string `json:"recognition_result"`
RecordingID string `json:"recording_id"`
Message string `json:"message"`
ComponentResults []RecogniserResponse `json:"component_results,omitempty"`
}
type RecogniserResponse struct {
Status bool `json:"status"`
// InputConfidence properties:
// * config or config|<property> - config as set in from config file
// * recogniser - from recogniser
// * user - user defined weight (as defined in call to process URL)
// * combined - the combined confidence from the three values above (config * recogniser * user)
InputConfidence map[string]float64 `json:"input_confidence,omitempty"`
Confidence float64 `json:"confidence"` // value between 0 and 1
RecognitionResult string `json:"recognition_result"`
RecordingID string `json:"recording_id"`
Message string `json:"message"`
Source string `json:"source"`
}
var spaceAndAfter = regexp.MustCompile(" .*$")
var prInputConfidenceRe = regexp.MustCompile("(\"input_confidence\": {)\n\\s*")
var prInputConfidenceChildrenRe = regexp.MustCompile("(\"(?:config[^\"]*|combined|recogniser|user)\": [0-9.]+,?)\n\\s*(}?)")
var prConfidenceTooLongRe = regexp.MustCompile("(\"(?:config[^\"]*|combined|recogniser|user|confidence)\": [0-9].[0-9]{4})[0-9]+")
func (pr ProcessResponse) PrettyJSONForced() string {
res, _ := pr.PrettyJSON()
return res
}
func (pr ProcessResponse) PrettyJSON() (string, error) {
js, err := PrettyMarshal(pr)
if err != nil {
return "", fmt.Errorf("failed to marshal response : %v", err)
}
res := string(js)
res = prInputConfidenceRe.ReplaceAllString(res, "$1")
res = prInputConfidenceChildrenRe.ReplaceAllString(res, "$1$2 ")
res = prConfidenceTooLongRe.ReplaceAllString(res, "$1")
res = strings.Replace(res, "} ,", "},", -1)
return res, nil
}
func (pr ProcessResponse) Source() string {
return spaceAndAfter.ReplaceAllString(pr.Message, "")
}
func (pr RecogniserResponse) String() string {
status := "OK"
if !pr.Status {
status = "FAIL"
}
return fmt.Sprintf("[%s] %s | %s %f %v %s %s", pr.Source, pr.RecognitionResult, status, pr.Confidence, pr.InputConfidence, pr.RecordingID, pr.Message)
}
func (pr ProcessResponse) String() string {
status := "OK"
if !pr.Ok {
status = "FAIL"
}
return fmt.Sprintf("[%s] %s | %s %v %s", pr.Message, pr.RecognitionResult, status, pr.Confidence, pr.RecordingID)
}