Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
add --output switch (#27)
Browse files Browse the repository at this point in the history
* add --output switch
  • Loading branch information
Antti Ukkonen authored Nov 25, 2021
1 parent 5c12012 commit 3df83d7
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"
"time"
"path/filepath"

"github.com/spf13/cobra"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -24,6 +25,7 @@ speechly annotate -a APP_ID --reference-date 2021-01-20 --input input.txt > outp
To evaluate already deployed speechly app, you need a set of evaluation examples that users of your application might say.`,
Short: "Create SAL annotations for a list of examples using Speechly.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

Expand Down Expand Up @@ -74,7 +76,19 @@ To evaluate already deployed speechly app, you need a set of evaluation examples
log.Fatal(err)
}

if err := printEvalResultCSV(cmd.OutOrStdout(), res.Responses); err != nil {
var outputWriter io.Writer
outputFile, err := cmd.Flags().GetString("output")
if (err == nil && len(outputFile) > 0) {
outputFile, _ = filepath.Abs(outputFile)
outputWriter, err = os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE, 0644)
if (err != nil) {
log.Fatalf("output path is invalid: %s", err)
}
} else {
outputWriter = cmd.OutOrStdout()
}

if err := printEvalResultCSV(outputWriter, res.Responses); err != nil {
log.Fatalf("Error creating CSV: %s", err)
}
},
Expand Down Expand Up @@ -102,8 +116,15 @@ func readLines(fn string) []string {
func init() {
rootCmd.AddCommand(annotateCmd)
annotateCmd.Flags().StringP("app", "a", "", "app id of the application to evaluate.")
if err := annotateCmd.MarkFlagRequired("app"); err != nil {
log.Fatalf("Failed to init flags: %s", err)
}
annotateCmd.Flags().StringP("input", "i", "", "evaluation utterances, separated by newline.")
annotateCmd.Flags().StringP("reference-date", "r", "", "reference date in ISO format, eg. YYYY-MM-DD. If no date provided, the current date is used.")
if err := annotateCmd.MarkFlagRequired("input"); err != nil {
log.Fatalf("Failed to init flags: %s", err)
}
annotateCmd.Flags().StringP("output", "o", "", "where to store annotated utterances, if not provided, print to stdout.")
annotateCmd.Flags().StringP("reference-date", "r", "", "reference date in YYYY-MM-DD format, if not provided use current date.")
}

func printEvalResultCSV(out io.Writer, items []*wluv1.WLUResponse) error {
Expand Down

0 comments on commit 3df83d7

Please sign in to comment.