Compare commits

..

No commits in common. "397f3448857b7be437de206549685fb69577c4d6" and "5acfd28ccce38d0852e58953a2aaee64b0a26851" have entirely different histories.

15 changed files with 37 additions and 1949218 deletions

View File

@ -1,27 +0,0 @@
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

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

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

View File

@ -3,99 +3,36 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"os" "os"
"regexp"
"strings"
"text/template" "text/template"
"time" "time"
) )
const Directory = "/usr/share/dict" const Path = "/usr/shared/dictionary/words"
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() { func main() {
fmt.Println("Generate") fmt.Println("Generate")
entries, err := os.ReadDir(Directory) f, err := os.Create("internal/dictionary/words.go")
if err != nil {
die(err) die(err)
} defer f.Close()
for _, e := range entries { packageTemplate.Execute(f, struct {
if !strings.Contains(e.Name(), ".") { Timestamp time.Time
generateFile(e.Name()) Path string
} Words []string
} }{
Timestamp: time.Now(),
Path: Path,
Words: []string{
"foo",
"bar",
"baz",
},
})
// generateFile("words")
} }
func die(err error) { func die(err error) {
@ -103,3 +40,17 @@ func die(err error) {
log.Fatal(err) 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 //go:generate go run internal/gen.go
func main() { func main() {
fmt.Println(dictionary.Brazilian.GetRandomWord()) fmt.Println(dictionary.Foo)
} }