Have got a dictionary of words

This commit is contained in:
Leon Mika 2025-01-25 09:45:55 +11:00
parent 5cb44dd17e
commit d9fa154a01
15 changed files with 46459 additions and 39 deletions

72
cmd/prep-words/main.go Normal file
View file

@ -0,0 +1,72 @@
package main
import (
"bufio"
"encoding/json"
"flag"
"log"
"os"
"sort"
)
type wordList struct {
Words map[int][]string `json:"words"`
}
func main() {
dictFile := flag.String("dict", "", "dictionary of word to prep")
flag.Parse()
words := wordList{
Words: make(map[int][]string),
}
if err := scanSuitableWords(*dictFile, func(word string) {
if len(word) >= 4 && len(word) <= 6 {
words.Words[len(word)] = append(words.Words[len(word)], word)
}
}); err != nil {
log.Fatal(err)
}
for _, word := range words.Words {
sort.Strings(word)
}
if err := json.NewEncoder(os.Stdout).Encode(words); err != nil {
log.Fatal(err)
}
}
func scanSuitableWords(dictFile string, withWord func(word string)) error {
f, err := os.Open(dictFile)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
isSuitable := true
breakpoint := len(scanner.Text())
for i, r := range scanner.Text() {
if r == '/' {
breakpoint = i
break
}
if r < 'a' || r > 'z' {
isSuitable = false
break
}
}
if isSuitable {
word := scanner.Text()[:breakpoint]
withWord(word)
} else {
}
}
return scanner.Err()
}