Compare commits
No commits in common. "397f3448857b7be437de206549685fb69577c4d6" and "5acfd28ccce38d0852e58953a2aaee64b0a26851" have entirely different histories.
397f344885
...
5acfd28ccc
@ -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
282879
internal/dictionary/swiss.go
282879
internal/dictionary/swiss.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
111
internal/gen.go
111
internal/gen.go
@ -3,99 +3,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
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), ""),
|
||||
})
|
||||
}
|
||||
const Path = "/usr/shared/dictionary/words"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Generate")
|
||||
|
||||
entries, err := os.ReadDir(Directory)
|
||||
if err != nil {
|
||||
die(err)
|
||||
}
|
||||
f, err := os.Create("internal/dictionary/words.go")
|
||||
die(err)
|
||||
defer f.Close()
|
||||
|
||||
for _, e := range entries {
|
||||
if !strings.Contains(e.Name(), ".") {
|
||||
generateFile(e.Name())
|
||||
}
|
||||
}
|
||||
packageTemplate.Execute(f, struct {
|
||||
Timestamp time.Time
|
||||
Path string
|
||||
Words []string
|
||||
}{
|
||||
Timestamp: time.Now(),
|
||||
Path: Path,
|
||||
Words: []string{
|
||||
"foo",
|
||||
"bar",
|
||||
"baz",
|
||||
},
|
||||
})
|
||||
|
||||
// generateFile("words")
|
||||
}
|
||||
|
||||
func die(err error) {
|
||||
@ -103,3 +40,17 @@ 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 }}
|
||||
}
|
||||
`))
|
||||
|
Loading…
x
Reference in New Issue
Block a user