-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
362 lines (326 loc) · 8.66 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright (C) 2024 semi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/jwalton/gchalk"
)
var Data TextData
var KPS []float64
//var SameKeyKPS []float64
type Argument int
const (
NullArg Argument = iota
LayoutArg
NgramArg
PathArg
)
type Command struct {
Names []string
Description string
Arg Argument
CountArg bool
}
var Commands = []Command{
{
Names: []string{"load"},
Description: "loads a text file as a corpus",
Arg: PathArg,
},
{
Names: []string{"rank", "r"},
Description: "returns a ranked list of layouts",
Arg: NullArg,
},
{
Names: []string{"analyze", "a"},
Description: "outputs detailed analysis of a layout",
Arg: LayoutArg,
},
{
Names: []string{"interactive"},
Description: "enters interactive analysis mode for the given layout",
Arg: LayoutArg,
},
{
Names: []string{"generate", "g"},
Description: "attempts to generate an optimal layout based on weights.hjson",
Arg: NullArg,
},
{
Names: []string{"improve"},
Description: "attempts to improve a layout according to the restrictions in layouts/_generate",
Arg: LayoutArg,
},
{
Names: []string{"heatmap"},
Description: "outputs a heatmap for the given layout at heatmap.png",
Arg: LayoutArg,
},
{
Names: []string{"sfbs"},
Description: "lists the sfb frequency and most frequent sfbs",
Arg: LayoutArg,
CountArg: true,
},
{
Names: []string{"dsfbs"},
Description: "lists the dsfb frequency and most frequent dsfbs",
Arg: LayoutArg,
CountArg: true,
},
{
Names: []string{"lsbs"},
Description: "lists the lsb frequency and most frequent lsbs",
Arg: LayoutArg,
CountArg: true,
},
{
Names: []string{"speed"},
Description: "lists each finger and its unweighted speed",
Arg: LayoutArg,
CountArg: true,
},
{
Names: []string{"bigrams"},
Description: "lists the worst key pair relationships",
Arg: LayoutArg,
CountArg: true,
},
{
Names: []string{"ngram"},
Description: "lists the frequency of a given ngram",
Arg: NgramArg,
},
}
func getLayout(s string) *Layout {
s = strings.ToLower(s)
if l, ok := Layouts[s]; ok {
return &l
}
fmt.Printf("layout [%s] was not found\n", s)
os.Exit(1)
return nil
}
func checkLayoutProvided(args []string) {
if len(args) <= 1 {
fmt.Println("You must provide the name of a layout!")
os.Exit(1)
}
}
func runCommand(args []string) {
var layout *Layout
var path *string
var ngram *string
var cmd string
count := 0
if len(args) == 0 {
usage()
return
}
for _, command := range Commands {
matches := false
for _, name := range command.Names {
if name == args[0] {
matches = true
break
}
}
if !matches {
continue
}
cmd = command.Names[0]
if command.Arg == NullArg {
break
}
if len(args) == 1 {
commandUsage(&command)
return
}
if command.Arg == PathArg {
if _, err := os.Stat(args[1]); errors.Is(err, os.ErrNotExist) {
fmt.Printf("file [%s] does not exist\n", args[1])
return
}
path = &args[1]
} else if command.Arg == NgramArg {
ngram = &args[1]
} else if command.Arg == LayoutArg {
layout = getLayout(args[1])
}
if command.CountArg && len(args) == 3 {
num, err := strconv.Atoi(args[2])
if err != nil {
fmt.Printf("optional count argument must be a number, not [%s]\n", args[2])
return
}
count = num
}
break
}
if cmd == "" {
usage()
}
if cmd == "load" {
Data = GetTextData(*path)
name := filepath.Base(*path)
name = name[:len(name)-len(filepath.Ext(name))]
name = name + ".json"
outpath := filepath.Join(Config.Paths.Corpora, name)
println(outpath)
WriteData(Data, outpath)
} else if cmd == "rank" {
type x struct {
name string
score float64
}
var sorted []x
for _, v := range Layouts {
sorted = append(sorted, x{v.Name, Score(v)})
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].score < sorted[j].score
})
for _, l := range sorted {
spaces := strings.Repeat(Config.Output.Rank.Spacer, 1+LongestLayoutName-len(l.name))
fmt.Printf("%s%s%.2f\n", l.name, spaces, l.score)
}
} else if cmd == "analyze" {
PrintAnalysis(*layout)
} else if cmd == "generate" {
best := Populate(Config.Generation.InitialPopulation)
optimal := Score(best)
type x struct {
name string
score float64
}
var sorted []x
for k, v := range Layouts {
sorted = append(sorted, x{k, Score(v)})
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].score < sorted[j].score
})
for _, l := range sorted {
spaces := strings.Repeat(Config.Output.Rank.Spacer, 1+LongestLayoutName-len(l.name))
fmt.Printf("%s%s%d%%\n", l.name, spaces, int(100*optimal/(Score(Layouts[l.name]))))
}
} else if cmd == "interactive" {
Interactive(*layout)
} else if cmd == "heatmap" {
Heatmap(*layout)
} else if cmd == "improve" {
ImproveFlag = true
ImproveLayout = *layout
best := Populate(1000)
optimal := Score(best)
fmt.Printf("%s %d%%\n", layout.Name, int(100*optimal/(Score(ImproveLayout))))
} else if cmd == "sfbs" || cmd == "dsfbs" || cmd == "lsbs" || cmd == "bigrams" {
l := *layout
var total float64
var list []FreqPair
if cmd == "sfbs" {
total = 100 * float64(SFBs(l, false)) / l.Total
list = ListSFBs(l, false)
} else if cmd == "dsfbs" {
total = 100 * float64(SFBs(l, true)) / l.Total
list = ListSFBs(l, true)
} else if cmd == "lsbs" {
total = 100 * float64(LSBs(l)) / l.Total
list = ListLSBs(l)
} else if cmd == "bigrams" {
total = 0.0
list = ListWorstBigrams(l)
}
SortFreqList(list)
if count == 0 {
count = Config.Output.Misc.TopNgrams
}
if total != 0.0 {
fmt.Printf("%.2f%%\n", total)
}
PrintFreqList(list, count, true)
} else if cmd == "speed" {
unweighted := FingerSpeed(layout, false)
fmt.Println("Unweighted Speed")
for i, v := range unweighted {
fmt.Printf("\t%s: %.2f\n", FingerNames[i], v)
}
weighted := FingerSpeed(layout, true)
fmt.Println("Weighted Speed")
for i, v := range weighted {
fmt.Printf("\t%s: %.2f\n", FingerNames[i], v)
}
} else if cmd == "ngram" {
total := float64(Data.Total)
ngram := *ngram
if length := len([]rune(ngram)); length == 1 {
fmt.Printf("unigram: %.3f%%\n", 100*float64(Data.Letters[ngram])/total)
} else if length == 2 {
fmt.Printf("bigram: %.3f%%\n", 100*float64(Data.Bigrams[ngram])/total)
fmt.Printf("skipgram: %.3f%%\n", 100*Data.Skipgrams[ngram]/total)
} else if length == 3 {
fmt.Printf("trigram: %.3f%%\n", 100*float64(Data.Trigrams[ngram])/total)
}
}
}
func commandUsage(command *Command) {
var argstr string
if command.Arg == LayoutArg {
argstr = " layout"
} else if command.Arg == NgramArg {
argstr = " ngram"
} else if command.Arg == PathArg {
argstr = " filepath"
}
argstr = gchalk.WithItalic().Sprintf("%s", argstr)
var countstr string
if command.CountArg {
countstr = " (count)"
}
fmt.Printf("%s%s%s | %s\n", command.Names[0], argstr, countstr, command.Description)
}
func main() {
ReadWeights()
flag.BoolVar(&StaggerFlag, "stagger", Config.Weights.Stagger, "if true, calculates distance for ANSI row-stagger form factor")
flag.BoolVar(&ColStaggerFlag, "colstagger", Config.Weights.ColStagger, "if true, calculates distance for col-stagger form factor")
flag.BoolVar(&SlideFlag, "slide", false, "if true, ignores slideable sfbs (made for Oats) (might not work)")
flag.BoolVar(&DynamicFlag, "dynamic", false, "")
flag.Parse()
args := flag.Args()
Data = LoadData(filepath.Join(Config.Paths.Corpora, Config.Corpus) + ".json")
Layouts = make(map[string]Layout)
LoadLayoutDir(Config.Paths.Layouts)
for _, l := range Layouts {
if len(l.Name) > LongestLayoutName {
LongestLayoutName = len(l.Name)
}
}
runCommand(args)
}
func usage() {
fmt.Println("usage: genkey command argument (optional)")
fmt.Println("commands:")
for _, c := range Commands {
fmt.Print(" ")
commandUsage(&c)
}
}