-
Notifications
You must be signed in to change notification settings - Fork 20
/
mergetokens.go
162 lines (151 loc) · 3.7 KB
/
mergetokens.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
/*
Merges two tokens files into one.
./mergetokens input1 input2 merged
*/
package main
import (
"os"
"fmt"
"errors"
"github.com/AlasdairF/Custom"
"github.com/alasdairforsythe/pansearch"
"github.com/alasdairforsythe/capcode/go"
"github.com/alasdairforsythe/norm"
)
var (
usingCapcode uint8
charsetFlag uint8
normalizer norm.Normalizer
level uint8
reserve uint8
)
func applyCapcode(data []byte) []byte {
if usingCapcode == 2 {
return capcode.Encode(data)
} else if usingCapcode == 1 {
return capcode.NoCapcodeEncode(data)
}
return data
}
func normalize(data []byte) []byte {
processed, err := normalizer.Normalize(data)
if err == nil {
return applyCapcode(processed)
} else { // if failed try it the other way around
if !normalizer.SpecifiedLowercase() {
processed = applyCapcode(data)
processed, err = normalizer.Normalize(processed)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
return processed
}
func saveTokensToFile(filename string, data [][]byte) error {
fi, err := os.Create(filename)
if err != nil {
return err
}
defer fi.Close()
w := custom.NewZlibWriter(fi)
defer w.Close()
w.WriteByte(usingCapcode)
w.WriteByte(charsetFlag)
w.WriteByte(normalizer.Flag)
w.WriteByte(level)
w.WriteByte(reserve)
w.WriteByte(0) // reserved
w.WriteByte(0) // reserved
w.WriteByte(0) // reserved
w.WriteUint64(uint64(len(data)))
for _, b := range data {
w.WriteBytes8(b)
}
return nil
}
func loadTokensFromFile(filename string) (uint8, uint8, uint8, uint8, uint8, [][]byte, error) {
fi, err := os.Open(filename)
if err != nil {
return 0, 0, 0, 0, 0, nil, err
}
defer fi.Close()
r := custom.NewZlibReader(fi)
_usingCapcode := r.ReadByte()
_charsetFlag := r.ReadByte()
_norm := r.ReadByte()
_level := r.ReadByte()
_reserve := r.ReadByte()
r.ReadByte()
r.ReadByte()
r.ReadByte()
l := int(r.ReadUint64())
data := make([][]byte, l)
for i:=0; i<l; i++ {
data[i] = r.ReadBytes8()
}
// Make sure we're at the end
if r.EOF() != nil { // it can be longer if it includes scores, so just do a quick sanity check
if _charsetFlag > 2 || _level > 5 || len(data[0]) > 40 || len(data[1]) > 40 || len(data[len(data)-1]) > 40 {
return 0, 0, 0, 0, 0, nil, errors.New(filename + ` not valid tokens file.`)
}
}
return _usingCapcode, _charsetFlag, _norm, _level, _reserve, data, nil
}
func main() {
if len(os.Args) <= 2 {
fmt.Println(`Usage: ./mergetokens input1 input2 merged`)
}
var err error
var outfile string
var tokens1, tokens2 [][]byte
usingCapcode, charsetFlag, normalizer.Flag, level, reserve, tokens1, err = loadTokensFromFile(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if os.Args[1] == os.Args[2] {
fmt.Println(`Output filename must be a separate file to the input.`)
os.Exit(1)
}
if len(os.Args) == 4 {
if os.Args[2] == os.Args[3] {
fmt.Println(`Output filename must be a separate file to the input.`)
os.Exit(1)
}
outfile = os.Args[3]
_, _, _, _, _, tokens2, err = loadTokensFromFile(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
} else {
outfile = os.Args[2]
}
counter := new(pansearch.Counter)
for _, tok := range tokens1 {
counter.Add(tok, 1)
}
for _, tok := range tokens2 {
counter.Add(tok, 1)
}
if level < 4 {
for _, v := range extraTokens {
counter.Add(normalize([]byte(v)), 1)
counter.Add(normalize([]byte(" " + string(v))), 1)
if v[len(v)-1] == '/' {
counter.Add([]byte(string(v) + "D"), 1)
}
}
}
counter.Build_Multithreaded()
err = saveTokensToFile(outfile, counter.Keys())
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Println(`Merged:`, outfile)
}
var extraTokens = []string{}