-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
313 lines (305 loc) · 9.78 KB
/
dictionary.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package mydictionary
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
)
// dictionary
type dictionaryStruct struct {
name string
readable bool
writable bool
index int
xlsx *excelize.File
sheetName string
columnIndex map[string]int
content []VocabularyAnswerStruct
}
// open and check .xlsx file
func (dictionary *dictionaryStruct) check(filePath string) (err error) {
var (
recheckCounter int
recheckUpperLimit int
contentTemp [][]string
columnNumber int
)
// file -> ram image
dictionary.xlsx, err = excelize.OpenFile(filePath)
if err != nil {
return
}
// recheck
recheckCounter = 0
recheckUpperLimit = 10
RECHECK:
dictionary.sheetName = dictionary.xlsx.GetSheetMap()[1]
if strings.Compare(dictionary.sheetName, "") == 0 {
// no worksheet in workbook: create a worksheet
if strings.Compare(dictionary.name, "") == 0 {
dictionary.xlsx.NewSheet("dictionary")
} else {
dictionary.xlsx.NewSheet(dictionary.name)
}
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
contentTemp = dictionary.xlsx.GetRows(dictionary.sheetName)
if len(contentTemp) == 0 {
// empty worksheet: create row 1
dictionary.xlsx.SetCellValue(dictionary.sheetName, "A1", sn)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
columnNumber = len(contentTemp[0])
// check existence of sheet header (column) in row 1
dictionary.columnIndex = map[string]int{wd: -1, def: -1, sn: -1, qc: -1, qt: -1, nt: -1}
for i := 0; i < columnNumber; i++ {
switch contentTemp[0][i] {
case wd:
dictionary.columnIndex[wd] = i
break
case def:
dictionary.columnIndex[def] = i
break
case sn:
dictionary.columnIndex[sn] = i
break
case qc:
dictionary.columnIndex[qc] = i
break
case qt:
dictionary.columnIndex[qt] = i
break
case nt:
dictionary.columnIndex[nt] = i
default:
break
}
}
if dictionary.columnIndex[wd] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", wd)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if dictionary.columnIndex[def] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", def)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if dictionary.columnIndex[sn] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", sn)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if dictionary.columnIndex[qc] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", qc)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if dictionary.columnIndex[qt] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", qt)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
if dictionary.columnIndex[nt] == -1 {
dictionary.xlsx.SetCellValue(dictionary.sheetName, excelize.ToAlphaString(len(contentTemp[0]))+"1", nt)
// recheck
if recheckCounter < recheckUpperLimit {
recheckCounter++
goto RECHECK
} else {
err = fmt.Errorf("there are format errors in file \"%s\"", filePath)
return
}
}
return
}
// read data from .xlsx file and put to dictionary
func (dictionary *dictionaryStruct) read(filePath string) (err error) {
var (
str string
vocabularyAnswer VocabularyAnswerStruct
)
if dictionary.readable {
// check
err = dictionary.check(filePath)
if err != nil {
return
}
// get space of content
dictionary.content = make([]VocabularyAnswerStruct, 0)
// ram image -> content
for i := 2; ; i++ {
// `xlsx:wd` -> .Word
str = dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[wd]), i))
if strings.Compare(str, "") == 0 {
break
}
vocabularyAnswer.Word = str
// `xlsx:def` -> .Define
str = dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[def]), i))
str = strings.TrimSpace(str)
vocabularyAnswer.Definition = strings.Split(str, "\n")
if len(vocabularyAnswer.Definition) == 1 &&
strings.Compare(vocabularyAnswer.Definition[0], "") == 0 {
vocabularyAnswer.Definition = nil
}
// `xlsx:sn` -> .SerialNumber
vocabularyAnswer.SerialNumber, err = strconv.Atoi(dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[sn]), i)))
if err != nil {
vocabularyAnswer.SerialNumber = i - 1
}
// `xlsx:qc` -> .QueryCounter
vocabularyAnswer.QueryCounter, err = strconv.Atoi(dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[qc]), i)))
if err != nil {
vocabularyAnswer.QueryCounter = 0
}
// reset err
err = nil
// `xlsx:qt` -> .QueryTime
vocabularyAnswer.QueryTime = dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[qt]), i))
// `xlsx:nt` -> .Note
str = dictionary.xlsx.GetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[nt]), i))
str = strings.TrimSpace(str)
vocabularyAnswer.Note = strings.Split(str, "\n")
if len(vocabularyAnswer.Note) == 1 &&
strings.Compare(vocabularyAnswer.Note[0], "") == 0 {
vocabularyAnswer.Note = nil
}
// others
vocabularyAnswer.SourceName = dictionary.name
vocabularyAnswer.Location.TableType = Dictionary
vocabularyAnswer.Status = ""
// add to dictionary
dictionary.content = append(dictionary.content, vocabularyAnswer)
// set location
dictionary.content[len(dictionary.content)-1].Location.TableIndex = dictionary.index
dictionary.content[len(dictionary.content)-1].Location.ItemIndex = len(dictionary.content) - 1
}
}
return
}
// get data dictionary and write to .xlsx file
func (dictionary *dictionaryStruct) write() (information string, err error) {
if dictionary.readable && dictionary.writable {
// content -> ram image
for i := 0; i < len(dictionary.content); i++ {
// .QueryCounter -> `xlsx:qc`
dictionary.xlsx.SetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[qc]), i+2), dictionary.content[i].QueryCounter)
// .QueryTime -> `xlsx:qt`
dictionary.xlsx.SetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[qt]), i+2), dictionary.content[i].QueryTime)
// .Note -> `xlsx:nt`
dictionary.xlsx.SetCellValue(dictionary.sheetName, fmt.Sprintf("%s%d", excelize.ToAlphaString(dictionary.columnIndex[nt]), i+2), strings.Join(dictionary.content[i].Note, "\n"))
}
// ram image -> file
err = dictionary.xlsx.Save()
if err != nil {
return
}
// output
information = fmt.Sprintf("Dictionary \"%s\" has been updated.\n\n", dictionary.xlsx.Path)
}
return
}
// query and update
func (dictionary *dictionaryStruct) queryAndUpdate(vocabularyAsk VocabularyAskStruct) (vocabularyAnswerList []VocabularyAnswerStruct) {
var (
vocabularyAnswer VocabularyAnswerStruct
tm time.Time
)
if dictionary.readable {
for i := 0; i < len(dictionary.content); i++ {
// basic
if strings.Compare(dictionary.content[i].Word, vocabularyAsk.Word) == 0 {
if dictionary.writable {
// update dictionary
if vocabularyAsk.DoNotRecord == false {
// update
tm = time.Now()
dictionary.content[i].QueryCounter++
dictionary.content[i].QueryTime = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second())
}
}
vocabularyAnswer = dictionary.content[i]
vocabularyAnswer.Status = Basic
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
if vocabularyAsk.Advance {
continue
} else {
break
}
}
if vocabularyAsk.Advance {
// advance
if strings.Contains(dictionary.content[i].Word, vocabularyAsk.Word) {
vocabularyAnswer = dictionary.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
for j := 0; j < len(dictionary.content[i].Definition); j++ {
if strings.Contains(dictionary.content[i].Definition[j], vocabularyAsk.Word) {
vocabularyAnswer = dictionary.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
}
for j := 0; j < len(dictionary.content[i].Note); j++ {
if strings.Contains(dictionary.content[i].Note[j], vocabularyAsk.Word) {
vocabularyAnswer = dictionary.content[i]
vocabularyAnswer.Status = Advance
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
goto ADVANCE_END
}
}
ADVANCE_END:
}
}
}
return
}