11package cmd
22
33import (
4- "bufio"
54 "fmt"
65 "io"
76 "log"
87 "os"
98 "path/filepath"
10- "regexp"
119 "strings"
12- "time"
13-
14- "github.com/spf13/cobra"
15- timestamppb "google.golang.org/protobuf/types/known/timestamppb"
1610
1711 wluv1 "github.com/speechly/api/go/speechly/slu/v1"
18- "github.com/speechly/cli/pkg/clients "
12+ "github.com/spf13/cobra "
1913)
2014
2115var annotateCmd = & cobra.Command {
@@ -69,60 +63,19 @@ To evaluate already deployed Speechly app, you need a set of evaluation examples
6963 appId = args [0 ]
7064 }
7165
72- wluClient , err := clients .WLUClient (ctx )
73- if err != nil {
74- log .Fatalf ("Error connecting to API: %s" , err )
75- }
76-
77- refD := time .Now ()
78- refDS , err := cmd .Flags ().GetString ("reference-date" )
66+ refD , err := readReferenceDate (cmd )
7967 if err != nil {
80- log .Fatalf ("reference-date is invalid: %s" , err )
81- }
82-
83- if len (refDS ) > 0 {
84- refD , err = time .Parse ("2006-01-02" , refDS )
85- if err != nil {
86- log .Fatalf ("reference-date is invalid: %s" , err )
87- }
68+ log .Fatalf ("Faild to get reference date: %s" , err )
8869 }
8970
90- data := readLines (inputFile )
91-
9271 deAnnotate , err := cmd .Flags ().GetBool ("de-annotate" )
9372 if err != nil {
9473 log .Fatalf ("Missing de-annotated flag: %s" , err )
9574 }
9675
97- annotated := data
98- transcripts := make ([]string , len (data ))
99- for i , line := range data {
100- transcripts [i ] = removeAnnotations (line )
101- }
102- data = transcripts
103-
104- if deAnnotate {
105- for _ , line := range data {
106- fmt .Println (line )
107- }
108- os .Exit (0 )
109- }
110-
111- wluRequests := make ([]* wluv1.WLURequest , len (data ))
112- for i , line := range data {
113- wluRequests [i ] = & wluv1.WLURequest {
114- Text : line ,
115- ReferenceTime : timestamppb .New (refD ),
116- }
117- }
118- textsRequest := & wluv1.TextsRequest {
119- AppId : appId ,
120- Requests : wluRequests ,
121- }
122-
123- res , err := wluClient .Texts (ctx , textsRequest )
76+ res , annotated , err := runThroughWLU (ctx , appId , inputFile , deAnnotate , refD )
12477 if err != nil {
125- log .Fatal ( err )
78+ log .Fatalf ( "WLU failed: %s" , err )
12679 }
12780
12881 evaluate , err := cmd .Flags ().GetBool ("evaluate" )
@@ -131,7 +84,7 @@ To evaluate already deployed Speechly app, you need a set of evaluation examples
13184 }
13285
13386 if evaluate {
134- EvaluateAnnotatedUtterances (wluResponsesToString (res .Responses ), annotated )
87+ evaluateAnnotatedUtterances (wluResponsesToString (res .Responses ), annotated )
13588 os .Exit (0 )
13689 }
13790
@@ -153,50 +106,6 @@ To evaluate already deployed Speechly app, you need a set of evaluation examples
153106 },
154107}
155108
156- func removeAnnotations (line string ) string {
157- removeNormalizedPattern := regexp .MustCompile (`\|.+?]\(([^)]+)\)` )
158- line = removeNormalizedPattern .ReplaceAllString (line , "]" )
159-
160- removablePattern := regexp .MustCompile (`\*([^ ]+)(?: |$)|\(([^)]+)\)` )
161- line = removablePattern .ReplaceAllString (line , "" )
162-
163- entityValuePattern := regexp .MustCompile (`\[([^]]+)]` )
164- return entityValuePattern .ReplaceAllStringFunc (line , func (s string ) string {
165- pipeIndex := strings .Index (s , "|" )
166- if pipeIndex == - 1 {
167- pipeIndex = len (s ) - 1
168- }
169- return s [1 :pipeIndex ]
170- })
171- }
172-
173- func readLines (fn string ) []string {
174- if fn != "--" {
175- file , err := os .Open (fn )
176- if err != nil {
177- log .Fatal (err )
178- }
179- defer func () {
180- err := file .Close ()
181- if err != nil {
182- log .Fatal (err )
183- }
184- }()
185- return scanLines (file )
186- } else {
187- return scanLines (os .Stdin )
188- }
189- }
190-
191- func scanLines (file * os.File ) []string {
192- var lines []string
193- scanner := bufio .NewScanner (file )
194- for scanner .Scan () {
195- lines = append (lines , scanner .Text ())
196- }
197- return lines
198- }
199-
200109func init () {
201110 RootCmd .AddCommand (annotateCmd )
202111 annotateCmd .Flags ().StringP ("app" , "a" , "" , "Application to evaluate. Can be given as the first positional argument." )
@@ -220,16 +129,3 @@ func printEvalResultTXT(out io.Writer, items []*wluv1.WLUResponse) error {
220129 }
221130 return nil
222131}
223-
224- func wluResponsesToString (responses []* wluv1.WLUResponse ) []string {
225- results := make ([]string , len (responses ))
226- for i , resp := range responses {
227- segmentStrings := make ([]string , len (resp .Segments ))
228- for j , segment := range resp .Segments {
229- segmentStrings [j ] = segment .AnnotatedText
230- }
231- results [i ] = strings .Join (segmentStrings , " " )
232-
233- }
234- return results
235- }
0 commit comments