Compare commits
4 Commits
5acfd28ccc
...
397f344885
Author | SHA1 | Date | |
---|---|---|---|
|
397f344885 | ||
|
31d3b43df0 | ||
|
22a5b077c5 | ||
|
a0826cd68d |
27
internal/dictionary.go
Normal file
27
internal/dictionary.go
Normal 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
|
||||
}
|
74598
internal/dictionary/american-english.go
Normal file
74598
internal/dictionary/american-english.go
Normal file
File diff suppressed because it is too large
Load Diff
204461
internal/dictionary/brazilian.go
Normal file
204461
internal/dictionary/brazilian.go
Normal file
File diff suppressed because it is too large
Load Diff
74037
internal/dictionary/british-english.go
Normal file
74037
internal/dictionary/british-english.go
Normal file
File diff suppressed because it is too large
Load Diff
51049
internal/dictionary/cracklib-small.go
Normal file
51049
internal/dictionary/cracklib-small.go
Normal file
File diff suppressed because it is too large
Load Diff
200235
internal/dictionary/french.go
Normal file
200235
internal/dictionary/french.go
Normal file
File diff suppressed because it is too large
Load Diff
102464
internal/dictionary/italian.go
Normal file
102464
internal/dictionary/italian.go
Normal file
File diff suppressed because it is too large
Load Diff
278443
internal/dictionary/ngerman.go
Normal file
278443
internal/dictionary/ngerman.go
Normal file
File diff suppressed because it is too large
Load Diff
221256
internal/dictionary/ogerman.go
Normal file
221256
internal/dictionary/ogerman.go
Normal file
File diff suppressed because it is too large
Load Diff
316411
internal/dictionary/portuguese.go
Normal file
316411
internal/dictionary/portuguese.go
Normal file
File diff suppressed because it is too large
Load Diff
68686
internal/dictionary/spanish.go
Normal file
68686
internal/dictionary/spanish.go
Normal file
File diff suppressed because it is too large
Load Diff
282879
internal/dictionary/swiss.go
Normal file
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
111
internal/gen.go
111
internal/gen.go
@ -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 }}
|
||||
}
|
||||
`))
|
||||
|
Loading…
x
Reference in New Issue
Block a user