-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
159 lines (123 loc) · 3.41 KB
/
main.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
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
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"text/scanner"
"time"
)
var (
terms string
period string
transTerm string
topN int
configFile string
transInfoTempl = "\n%d)Term-info:%s\t%s\nTranslation:%s\n"
)
func init() {
flag.StringVar(&terms, "says", "", "Stores information about new terms. Accept a string with terms delimited by comma or a single term")
flag.IntVar(&topN, "top", 10, "Return topN most frequently used terms - default top 10")
flag.StringVar(&period, "stats", time.Now().Format(time.UnixDate), "Return statistics about terms in a specified period")
flag.StringVar(&transTerm, "trans", "", "Get all the translations for a specific keyword ordered by frequency")
flag.StringVar(&configFile, "config", "", "Insert the path of the configuration file that wilde will use")
}
func selectTranslation(termData []WRData) int64 {
scan := scanner.Scanner{}
scan.Init(os.Stdin)
fmt.Print("Select one of the translations (e.g., 1): ")
scan.Scan()
if !scan.IsValid() {
return -1
}
index, err := strconv.ParseInt(scan.TokenText(), 10, 64)
if err != nil || index > int64(len(termData))-1 {
return -1
}
return index
}
func displayTranslations(data []WRData) {
for i, t := range data {
fmt.Printf(transInfoTempl, i, t.TermUse, t.OtherInfo, t.Trans)
if t.OrigLangExample != "" {
fmt.Printf("%s example: %s\n\n", t.FromLang, t.OrigLangExample)
}
if t.DstLangExample != "" {
fmt.Printf("%s example: %s\n\n", t.ToLang, t.DstLangExample)
}
}
}
func getSelectedTranslations(dataTrans map[string][]WRData) map[string]WRData {
selTransData := make(map[string]WRData)
fmt.Printf("Translation from \"%s\" to \"%s\"\n", Configuration.FromLang, Configuration.ToLang)
for t, data := range dataTrans {
displayTranslations(data)
transIndex := selectTranslation(data)
if transIndex == -1 {
fmt.Println("Skipped translation")
continue
}
selTransData[t] = data[transIndex]
}
return selTransData
}
func visualizeTop(tops []string) {}
func main() {
flag.Parse()
// Not enough parameters
if flag.NFlag() != 2 {
fmt.Printf("Not enough parameters specified\n\n")
flag.Usage()
return
}
if f := flag.Lookup("config"); f == nil {
fmt.Printf("Configuration file not specified\n\n")
flag.Usage()
return
}
LoadConfiguration(configFile)
// initialize database connection
if err := OpenConnection(Configuration.Host, Configuration.DbName); err != nil {
panic(err)
}
flag.Visit(func(flag *flag.Flag) {
switch flag.Name {
case "says":
if terms == "" {
return
}
dataTrans, err := GetAllTerms(arrayTerms(strings.Split(terms, ",")), Configuration.FromLang, Configuration.ToLang)
if err != nil {
fmt.Printf("%+v", err)
return
}
selData := getSelectedTranslations(dataTrans)
if err := InsertTermsTranslations(selData); err != nil {
panic(err)
}
case "top":
visualizeTop(GetTopTerms(topN))
case "stats":
stats := GetTermStatistics()
fmt.Printf("%s\n", stats)
case "trans":
if transTerm == "" {
return
}
trans, err := GetTranslations(transTerm)
if err != nil {
fmt.Printf("Geting Translations failed err: %q\n", err.Error())
return
}
fmt.Printf("\nFor the term %q, translations are: \n\n", transTerm)
for i, t := range trans {
fmt.Printf("%d) %s\n", i, t.String())
}
case "config":
// NOP
default:
panic("Undefined operation")
}
})
}