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
|
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"rescribe.xyz/utils/pkg/hocr"
)
//TO DO: make writetofile return an error and handle that accordingly
// potential TO DO: add text versions where footer is cropped on odd/even pages only
// the trimblanks function trims the blank lines from a text input
func trimblanks(hocrfile string) string {
filein := bytes.NewBufferString(hocrfile)
var noblanks string
scanner := bufio.NewScanner(filein)
for scanner.Scan() {
eachline := scanner.Text()
trimmed := strings.TrimSpace(eachline)
if len(trimmed) != 0 {
noblanks = noblanks + eachline + "\n"
}
}
return noblanks
}
// the dehyphenateString function is copy-pasted from Nick's code (see rescribe.xyz/utils/cmd/dehyphenator/main.go), written to dehyphenate
// a string or hocr file. Only one small change from the original: the string is dehyphenated and concatenated WITHOUT line breaks
func dehyphenateString(in string) string {
var newlines []string
lines := strings.Split(in, "\n")
for i, line := range lines {
words := strings.Split(line, " ")
last := words[len(words)-1]
// the - 2 here is to account for a trailing newline and counting from zero
if len(last) > 0 && last[len(last) - 1] == '-' && i < len(lines) - 2 {
nextwords := strings.Split(lines[i+1], " ")
if len(nextwords) > 0 {
line = line[0:len(line)-1] + nextwords[0]
}
if len(nextwords) > 1 {
lines[i+1] = strings.Join(nextwords[1:], " ")
} else {
lines[i+1] = ""
}
}
newlines = append(newlines, line)
}
return strings.Join(newlines, " ")
}
// the fullcrop function takes a text input and crops the first and the last line (if text is at least 2 lines long)
func fullcrop(noblanks string) string {
alllines := strings.Split(noblanks, "\n")
if len(alllines) <= 2 {
return ""
} else {
return strings.Join(alllines[1:len(alllines)-2], "\n")
}
}
// the headcrop function takes a text input and crops the first line provided text is longer than 1 line
func headcrop(noblanks string) string {
alllines := strings.Split(noblanks, "\n")
switch {
case len(alllines) == 2:
return strings.Join(alllines[1:], "\n")
case len(alllines) < 2:
return ""
default:
return strings.Join(alllines[1:], "\n")
}
}
// the footcrop function takes a text input and crops the last line provided text is longer than 1 line
func footcrop(noblanks string) string {
alllines := strings.Split(noblanks, "\n")
switch {
case len(alllines) == 2:
return strings.Join(alllines[0:len(alllines)-2], "\n")
case len(alllines) < 2:
return ""
default:
return strings.Join(alllines[0:len(alllines)-2], "\n")
}
}
// the convertselect function selects the hocr from the bookdirectory above a given confidence threshold and
// converts it to text, trims each text and appends all into one textbase and saves it as a text file.
// the function returns one full version, one with headers and footers cropped, one with only
//headers cropped
func convertselect(bookdirectory, hocrfilename string, confthresh int) (string, string, string, string) {
var alltxt string
var croptxt string
var killheadtxt string
var footkilltxt string
hocrfilepath := filepath.Join(bookdirectory, hocrfilename)
confpath := filepath.Join(bookdirectory, "conf")
readConf, err := os.Open(confpath)
if err != nil {
log.Fatalf("failed to open file: %s", err)
}
defer readConf.Close()
scanner := bufio.NewScanner(readConf)
var confline string
var confvalue int
for scanner.Scan() {
confline = scanner.Text()
if strings.Contains(confline, hocrfilename) {
substring := strings.Split(confline, " ")
if len(substring) != 2 {
log.Fatalf("Bailing as conf file %s doesn't seem to be formatted correctly (wants 2 fields separated by ' ')\n", confpath)
}
confvalue, _ = strconv.Atoi(substring[1])
}
}
readConf.Close()
if confvalue > confthresh {
hocrfiletext, err := hocr.GetText(hocrfilepath)
if err != nil {
log.Fatal(err)
}
trimbest := trimblanks(hocrfiletext)
alltxt = dehyphenateString(trimbest)
croptxt = dehyphenateString(fullcrop(trimbest))
killheadtxt = dehyphenateString(headcrop(trimbest))
footkilltxt = dehyphenateString(footcrop(trimbest))
}
return alltxt, croptxt, killheadtxt, footkilltxt
}
// the writetofile function takes a directory, filename and text input and creates a text file within the bookdirectory from them.
func writetofile(bookdirectory, textfilebase, txt string) error {
alltxtfile := filepath.Join(bookdirectory, textfilebase)
file, err := os.Create(alltxtfile)
if err != nil {
return fmt.Errorf("Error opening file %s: %v", alltxtfile, err)
}
defer file.Close()
if _, err := file.WriteString(txt); err != nil {
log.Println(err)
}
return err
}
func main() {
confthresh := flag.Int("c", 30, "Chosen confidence threshold. Default:30")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: command -c confidence-threshold bookdirectory \n")
fmt.Fprintf(os.Stderr, "Creates different text versions from the hocr files of a bookdirectory.\n")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
bookdirectory := flag.Arg(0)
confthreshstring := strconv.Itoa(*confthresh)
fmt.Println("Postprocessing", bookdirectory, "with threshold", *confthresh)
bestpath := filepath.Join(bookdirectory, "best")
readBest, err := ioutil.ReadFile(bestpath)
if err != nil {
log.Fatalf("failed to read file: %s", err)
}
Bestin := string([]byte(readBest))
bestslice := strings.Split(Bestin, "\n")
sort.Strings(bestslice)
var all, crop, killhead, killfoot string
for _, v := range bestslice {
if v != "" {
alltxt, croptxt, killheadtxt, footkilltxt := convertselect(bookdirectory, v, *confthresh)
all = all + " " + alltxt
crop = crop + " " + croptxt
killhead = killhead + " " + killheadtxt
killfoot = killfoot + " " + footkilltxt
}
}
bookname:= filepath.Base(bookdirectory)
b := bookname + "_" + confthreshstring
err1 := writetofile(bookdirectory, b + "_all.txt", all)
if err1 != nil {
log.Fatalf("Ah shit, we're going down, Nick says ABORT! %v", err1)
}
err2 := writetofile(bookdirectory, b + "_crop.txt", crop)
if err2 != nil {
log.Fatalf("Ah shit, we're going down, Nick says ABORT! %v", err2)
}
err3 := writetofile(bookdirectory, b + "_nohead.txt", killhead)
if err3 != nil {
log.Fatalf("Ah shit, we're going down, Nick says ABORT! %v", err3)
}
err4 := writetofile(bookdirectory, b + "_nofoot.txt", killfoot)
if err4 != nil {
log.Fatalf("Ah shit, we're going down, Nick says ABORT! %v", err4)
}
}
|