Skip to content

Commit c65afe4

Browse files
committed
feat: add first version
0 parents  commit c65afe4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+18375
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

cmd/generate-data/main.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/dave/jennifer/jen"
11+
)
12+
13+
func main() {
14+
src := os.Args[1]
15+
16+
fmt.Printf("Reading files from %s...\n", src)
17+
18+
files, err := filepath.Glob(filepath.Join(src, "*.txt"))
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
g, err := generateDefFile()
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
out := filepath.Join("data", "all.go")
29+
30+
fmt.Printf("Generating %s...\n", out)
31+
32+
if err := g.Save(out); err != nil {
33+
panic(err)
34+
}
35+
36+
for _, file := range files {
37+
filename := filepath.Base(file)
38+
lang := strings.TrimSuffix(filename, ".txt")
39+
40+
words, err := readWords(file)
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
g, err := generateLangFile(lang, words)
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
out := filepath.Join("data", lang+".go")
51+
52+
fmt.Printf("Generating %s...\n", out)
53+
54+
if err := g.Save(out); err != nil {
55+
panic(err)
56+
}
57+
}
58+
}
59+
60+
func readWords(file string) ([]string, error) {
61+
f, err := os.Open(file)
62+
if err != nil {
63+
return nil, err
64+
}
65+
defer f.Close()
66+
67+
exists := make(map[string]struct{})
68+
69+
var words []string
70+
71+
scanner := bufio.NewScanner(f)
72+
73+
for scanner.Scan() {
74+
word := scanner.Text()
75+
76+
if _, ok := exists[word]; ok {
77+
continue
78+
}
79+
80+
words = append(words, word)
81+
exists[word] = struct{}{}
82+
}
83+
84+
return words, scanner.Err()
85+
}
86+
87+
func generateLangFile(lang string, words []string) (*jen.File, error) {
88+
f := jen.NewFile("data")
89+
f.HeaderComment("Code generated by generate-data. DO NOT EDIT.")
90+
91+
mapValues := jen.Dict{}
92+
93+
for _, word := range words {
94+
mapValues[jen.Lit(word)] = jen.Values()
95+
}
96+
97+
f.Func().Id("init").Params().Block(
98+
jen.Id("Languages").Index(jen.Lit(lang)).Op("=").Map(jen.String()).Struct().Values(mapValues),
99+
)
100+
101+
return f, nil
102+
}
103+
104+
func generateDefFile() (*jen.File, error) {
105+
f := jen.NewFile("data")
106+
f.HeaderComment("Code generated by generate-data. DO NOT EDIT.")
107+
108+
f.Var().Id("Languages").Op("=").Map(jen.String()).Map(jen.String()).Struct().Values()
109+
110+
return f, nil
111+
}

data/af.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/all.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)