forked from github/vulcanizer
/
analyze.go
77 lines (59 loc) · 2.1 KB
/
analyze.go
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
package main
import (
"fmt"
"os"
"github.com/github/vulcanizer"
"github.com/spf13/cobra"
)
func init() {
cmdAnalyze.Flags().StringP("analyzer", "a", "", "The analyzer used to analyze the text Overrides field and index settings.")
cmdAnalyze.Flags().StringP("field", "", "", "Use analyzer settings of the given field to analyze against. Overridden by analyzer setting. index setting also required.")
cmdAnalyze.Flags().StringP("index", "i", "", "Specifies which index to look for field setting. Overridden by analyzer setting. field setting also required.")
cmdAnalyze.Flags().StringP("text", "t", "", "Text to analyze (required).")
err := cmdAnalyze.MarkFlagRequired("text")
if err != nil {
fmt.Printf("Error binding text configuration flag: %s \n", err)
os.Exit(1)
}
rootCmd.AddCommand(cmdAnalyze)
}
var cmdAnalyze = &cobra.Command{
Use: "analyze",
Short: "Analyze text given an analyzer or a field and index.",
Long: `Use Elasticsearch's analyze API to display the tokens of example text. Either "analyzer" OR "field" and "index" arguments are required.`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
text, err := cmd.Flags().GetString("text")
if err != nil {
fmt.Printf("Could not retrieve required argument: text. Error: %s\n", err)
os.Exit(1)
}
analyzer, _ := cmd.Flags().GetString("analyzer")
var tokens []vulcanizer.Token
if analyzer != "" {
ts, err := v.AnalyzeText(analyzer, text)
if err != nil {
fmt.Printf("Error calling analyze API: %s\n", err)
os.Exit(1)
}
tokens = ts
} else {
field, _ := cmd.Flags().GetString("field")
index, _ := cmd.Flags().GetString("index")
if field == "" || index == "" {
fmt.Println("Either `analyzer` or `field` and `index` arguments are required.")
os.Exit(1)
}
ts, err := v.AnalyzeTextWithField(index, field, text)
if err != nil {
fmt.Printf("Error calling analyze API: %s\n", err)
os.Exit(1)
}
tokens = ts
}
fmt.Printf("Printing tokens for the provided text: %s\n", text)
for _, t := range tokens {
fmt.Printf("\t* %s - %s\n", t.Text, t.Type)
}
},
}