Compare commits

...

4 Commits

Author SHA1 Message Date
Hazel Noack
397f344885 added functionality to dictionaries 2025-07-01 11:10:48 +02:00
Hazel Noack
31d3b43df0 added generated code 2025-06-30 17:23:56 +02:00
Hazel Noack
22a5b077c5 generation works 2025-06-30 17:23:29 +02:00
Hazel Noack
a0826cd68d draft 2025-06-30 15:44:59 +02:00
15 changed files with 1949218 additions and 37 deletions

27
internal/dictionary.go Normal file
View File

@ -0,0 +1,27 @@
package internal
import (
"math/rand/v2"
"sort"
)
type Dictionary []string
func (d Dictionary) GetRandomWord() string {
return d[rand.IntN(len(d))]
}
func (d Dictionary) GetRandomWords(n int) []string {
r := make([]string, n)
for i := range r {
r[i] = d[rand.IntN(len(d))]
}
return r
}
func (d Dictionary) Contains(word string) bool {
index := sort.SearchStrings(d, word)
return index < len(d) && d[index] == word
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

200235
internal/dictionary/french.go Normal file

File diff suppressed because it is too large Load Diff

102464
internal/dictionary/italian.go Normal file

File diff suppressed because it is too large Load Diff

278443
internal/dictionary/ngerman.go Normal file

File diff suppressed because it is too large Load Diff

221256
internal/dictionary/ogerman.go Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

68686
internal/dictionary/spanish.go Normal file

File diff suppressed because it is too large Load Diff

282879
internal/dictionary/swiss.go Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -3,36 +3,99 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"text/template"
"time"
)
const Path = "/usr/shared/dictionary/words"
const Directory = "/usr/share/dict"
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
var wordsTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
// using data from
// {{ .Path }}
package dictionary
import (
"gitea.elara.ws/Hazel/go-words/internal"
)
var {{ .VarName }} = internal.Dictionary{
{{- range .Words }}
{{ printf "%q" . }},
{{- end }}
}
`))
type templateData struct {
Timestamp time.Time
Path string
Words []string
VarName string
}
func generateFile(name string) {
fmt.Println("generating file for dictionary " + name)
lowerName := strings.ToLower(name)
words := []string{}
Path := Directory + "/" + lowerName
file, err := os.Open(Path)
if err != nil {
die(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
w := strings.TrimSpace(scanner.Text())
if regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(w) {
words = append(words, w)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
f, err := os.Create("internal/dictionary/" + lowerName + ".go")
die(err)
defer f.Close()
wordsTemplate.Execute(f, templateData{
Timestamp: time.Now(),
Path: Path,
Words: words,
VarName: nonAlphanumericRegex.ReplaceAllString(strings.Title(name), ""),
})
}
func main() {
fmt.Println("Generate")
f, err := os.Create("internal/dictionary/words.go")
die(err)
defer f.Close()
entries, err := os.ReadDir(Directory)
if err != nil {
die(err)
}
packageTemplate.Execute(f, struct {
Timestamp time.Time
Path string
Words []string
}{
Timestamp: time.Now(),
Path: Path,
Words: []string{
"foo",
"bar",
"baz",
},
})
for _, e := range entries {
if !strings.Contains(e.Name(), ".") {
generateFile(e.Name())
}
}
// generateFile("words")
}
func die(err error) {
@ -40,17 +103,3 @@ func die(err error) {
log.Fatal(err)
}
}
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
// using data from
// {{ .Path }}
package dictionary
var Words = []string{
{{- range .Words }}
{{ printf "%q" . }},
{{- end }}
}
`))

View File

@ -9,5 +9,5 @@ import (
//go:generate go run internal/gen.go
func main() {
fmt.Println(dictionary.Foo)
fmt.Println(dictionary.Brazilian.GetRandomWord())
}