-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
347 lines (315 loc) · 7.28 KB
/
Copy pathformat.go
File metadata and controls
347 lines (315 loc) · 7.28 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"text/template"
"time"
"github.com/fatih/color"
"github.com/finkf/gofiler"
"github.com/finkf/pcwgo/api"
)
func format(data interface{}) {
if formatMaybeSpecial(data) {
return
}
switch t := data.(type) {
case *api.Page:
formatPage(t)
case *api.Line:
formatLine(t)
case *api.Token:
formatWord(t)
case *api.SearchResults:
formatSearchResults(t)
case *api.CharMap:
formatCharMap(t)
case *api.PostCorrection:
formatPostCorrection(t)
case api.Suggestions:
formatSuggestions(t)
case *api.SuggestionCounts:
formatSuggestionCounts(t)
case *api.PatternCounts:
formatPatternCounts(t)
case *api.AdaptiveTokens:
formatAdaptiveTokens(t)
case *api.ExtendedLexicon:
formatExtendedLexicon(t)
case gofiler.Profile:
formatProfile(t)
case api.Session:
formatSession(t)
case api.Version:
printf(nil, "%s\n", t.Version)
case *api.Users:
formatUsers(t)
case *api.User:
formatUser(t)
case *api.Books:
formatBooks(t)
case *api.Book:
formatBook(t)
default:
log.Fatalf("error: format: bad type: %T", t)
}
}
func printf(col *color.Color, format string, args ...interface{}) {
for i := range args {
if str, ok := args[i].(string); ok {
args[i] = s(str)
}
}
if col == nil {
_, err := fmt.Printf(format, args...)
chk(err)
return
}
_, err := col.Printf(format, args...)
chk(err)
}
func s(str string) string {
if str == "" {
return "\u03B5"
}
return strings.ReplaceAll(str, " ", "_")
}
var (
green = color.New(color.FgGreen)
red = color.New(color.FgRed)
yellow = color.New(color.FgYellow)
)
func colorForToken(t *api.Token) *color.Color {
if t.IsMatch {
return red
}
if t.IsManuallyCorrected {
return green
}
if t.IsAutomaticallyCorrected {
return yellow
}
return nil
}
func formatPage(page *api.Page) {
for _, line := range page.Lines {
formatLine(&line)
}
}
func formatLine(line *api.Line) {
if opts.format.onlyManual && !line.IsManuallyCorrected {
return
}
if opts.format.words {
formatLineWords(line)
return
}
if !opts.format.noCor {
printf(nil, "%d:%d:%d", line.ProjectID, line.PageID, line.LineID)
for _, w := range line.Tokens {
printf(nil, " ")
printf(colorForToken(&w), w.Cor)
}
printf(nil, "\n")
}
if opts.format.ocr {
printf(nil, "%d:%d:%d", line.ProjectID, line.PageID, line.LineID)
for _, w := range line.Tokens {
printf(nil, " ")
printf(nil, w.OCR)
}
printf(nil, "\n")
}
}
func formatLineWords(line *api.Line) {
if opts.format.onlyManual && !line.IsManuallyCorrected {
return
}
for _, w := range line.Tokens {
formatWord(&w)
}
}
func formatWord(w *api.Token) {
if opts.format.onlyManual && !w.IsManuallyCorrected {
return
}
if !opts.format.noCor {
printf(nil, "%d:%d:%d:%d ", w.ProjectID, w.PageID, w.LineID, w.TokenID)
printf(colorForToken(w), w.Cor)
printf(nil, "\n")
}
if opts.format.ocr {
printf(nil, "%d:%d:%d:%d ", w.ProjectID, w.PageID, w.LineID, w.TokenID)
printf(nil, w.Cor)
printf(nil, "\n")
}
}
func formatCharMap(chars *api.CharMap) {
for char, n := range chars.CharMap {
fmt.Printf("%d %d %s %d\n", chars.BookID, chars.ProjectID, char, n)
}
}
func formatPostCorrection(pcs *api.PostCorrection) {
for _, pc := range pcs.Corrections {
fmt.Printf("%d:%d:%d:%d %s %s %f %t\n",
pcs.BookID, pc.PageID, pc.LineID, pc.TokenID,
pc.OCR, pc.Cor, pc.Confidence, pc.Taken)
}
}
func formatSearchResults(res *api.SearchResults) {
for _, m := range res.Matches {
for _, line := range m.Lines {
if opts.format.words {
for _, w := range line.Tokens {
// Skip not matched tokens
if !w.IsMatch {
continue
}
// It does not make sense to
// mark each matched token in
// red. Just print the normal
// token with its normal
// color.
w.IsMatch = false
formatWord(&w)
}
} else {
formatLine(&line)
}
}
}
}
func formatSuggestions(suggs api.Suggestions) {
for _, sugg := range suggs.Suggestions {
for _, s := range sugg {
printf(nil, "%d %s %s %s %s %s %s %d %f %t\n",
suggs.ProjectID, s.Token, s.Suggestion, s.Modern,
patterns(s.HistPatterns), patterns(s.OCRPatterns),
s.Dict, s.Distance, s.Weight, s.Top)
}
}
}
func formatSuggestionCounts(counts *api.SuggestionCounts) {
for k, v := range counts.Counts {
printf(nil, "%d %d %s %d\n", counts.BookID, counts.ProjectID, k, v)
}
}
func formatPatternCounts(counts *api.PatternCounts) {
for k, v := range counts.Counts {
printf(nil, "%d %d %s %d %t\n", counts.BookID, counts.ProjectID, k, v, counts.OCR)
}
}
func formatAdaptiveTokens(tokens *api.AdaptiveTokens) {
for _, token := range tokens.AdaptiveTokens {
printf(nil, "%d %d %s\n", tokens.BookID, tokens.ProjectID, token)
}
}
func formatExtendedLexicon(lex *api.ExtendedLexicon) {
for entry, n := range lex.Yes {
printf(nil, "%d %d %s %d %t\n", lex.BookID, lex.ProjectID, entry, n, true)
}
for entry, n := range lex.No {
printf(nil, "%d %d %s %d %t\n", lex.BookID, lex.ProjectID, entry, n, false)
}
}
func formatProfile(profile gofiler.Profile) {
pats := func(pats []gofiler.Pattern) []string {
var ret []string
for _, pat := range pats {
ret = append(ret, fmt.Sprintf("%s:%s:%d",
pat.Left, pat.Right, pat.Pos))
}
return ret
}
for k, v := range profile {
top := true
for _, c := range v.Candidates {
printf(nil, "%s %s %s %s %s %s %d %f %t\n",
k, c.Suggestion, c.Modern,
strings.Join(pats(c.HistPatterns), ","),
strings.Join(pats(c.OCRPatterns), ","),
c.Dict, c.Distance, c.Weight, top)
top = false
}
}
}
func formatSession(s api.Session) {
printf(nil, "%d %s %s %t %s %s\n",
s.User.ID, s.User.Email, s.User.Name, s.User.Admin,
s.Auth, time.Unix(s.Expires, 0).Format(time.RFC3339))
}
func formatUsers(users *api.Users) {
for i := range users.Users {
formatUser(&users.Users[i])
}
}
func formatUser(user *api.User) {
printf(nil, "%d %s %s %s %t\n",
user.ID, user.Name, user.Email, user.Institute, user.Admin)
}
func formatBooks(books *api.Books) {
for i := range books.Books {
formatBook(&books.Books[i])
}
}
func formatBook(book *api.Book) {
printf(nil, "%d %d %s %s %d %s %s %d %s %s %s\n",
book.BookID, book.ProjectID, book.Author, book.Title,
len(book.PageIDs), bookType(book), bookStatusString(book),
book.Year, book.Language, book.ProfilerURL, book.Description)
}
func bookType(book *api.Book) string {
if book.IsBook {
return "prj"
}
return "pkg"
}
func bookStatusString(book *api.Book) string {
res := []byte("---")
if book.Status["post-corrected"] {
res[0] = 'c'
}
if book.Pooled {
res[1] = 'g'
}
if book.Status["profiled"] {
res[2] = 'p'
}
return string(res)
}
func formatMaybeSpecial(data interface{}) bool {
if formatMaybeJSON(data) {
return true
}
if formatMaybeTemplate(data) {
return true
}
return false
}
func formatMaybeJSON(data interface{}) bool {
if !opts.format.json {
return false
}
chk(json.NewEncoder(os.Stdout).Encode(data))
return true
}
func formatMaybeTemplate(data interface{}) bool {
if opts.format.template == "" {
return false
}
t, err := template.New("pocwebc").
Parse(strings.Replace(opts.format.template, "\\n", "\n", -1))
chk(err)
err = t.Execute(os.Stdout, data)
chk(err)
return true
}
type patterns []string
func (ps patterns) String() string {
if len(ps) == 0 {
return "::"
}
return strings.Join(ps, ",")
}