bookworm word finder
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Bookworm Solver
|
||||
|
||||
Small Go app with a browser UI that finds the best words you can build from a set of letters.
|
||||
|
||||
## Run it
|
||||
|
||||
```bash
|
||||
go run .
|
||||
```
|
||||
|
||||
Then open `http://localhost:8080`.
|
||||
|
||||
## How it ranks words
|
||||
|
||||
The solver checks every word in its dictionary, keeps only words that can be built from your letters, and ranks them by:
|
||||
|
||||
1. Total letter score
|
||||
2. Word length
|
||||
3. Alphabetical order
|
||||
|
||||
It also adds a length bonus so longer valid words tend to beat short high-value words.
|
||||
|
||||
## Custom dictionary
|
||||
|
||||
If you want better results, add one word per line to `words/words.txt`. The app will merge that file with the built-in starter dictionary when it starts.
|
||||
@@ -0,0 +1,218 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
//go:embed starter_words.txt
|
||||
var starterWords embed.FS
|
||||
|
||||
var letterScores = map[rune]int{
|
||||
'a': 1,
|
||||
'b': 4,
|
||||
'c': 4,
|
||||
'd': 3,
|
||||
'e': 1,
|
||||
'f': 5,
|
||||
'g': 3,
|
||||
'h': 3,
|
||||
'i': 1,
|
||||
'j': 7,
|
||||
'k': 6,
|
||||
'l': 2,
|
||||
'm': 4,
|
||||
'n': 2,
|
||||
'o': 1,
|
||||
'p': 4,
|
||||
'q': 8,
|
||||
'r': 2,
|
||||
's': 1,
|
||||
't': 2,
|
||||
'u': 2,
|
||||
'v': 5,
|
||||
'w': 5,
|
||||
'x': 7,
|
||||
'y': 4,
|
||||
'z': 8,
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Word string
|
||||
Score int
|
||||
Length int
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
dictionary []string
|
||||
}
|
||||
|
||||
func NewEngine(customDictionaryPath string) (*Engine, error) {
|
||||
words, err := loadWordSet(customDictionaryPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Engine{dictionary: words}, nil
|
||||
}
|
||||
|
||||
func (engine *Engine) FindMatches(letters string, minLength int, limit int) ([]Result, error) {
|
||||
cleanedLetters := normalizeWord(letters)
|
||||
if len(cleanedLetters) < 2 {
|
||||
return nil, fmt.Errorf("enter at least 2 letters")
|
||||
}
|
||||
if minLength < 2 {
|
||||
return nil, fmt.Errorf("minimum length must be at least 2")
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 1
|
||||
}
|
||||
|
||||
available := letterCounts(cleanedLetters)
|
||||
results := make([]Result, 0, limit)
|
||||
|
||||
for _, word := range engine.dictionary {
|
||||
if len(word) < minLength || len(word) > len(cleanedLetters) {
|
||||
continue
|
||||
}
|
||||
if !canBuild(word, available) {
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, Result{
|
||||
Word: word,
|
||||
Score: wordScore(word),
|
||||
Length: len(word),
|
||||
})
|
||||
}
|
||||
|
||||
slices.SortFunc(results, func(left Result, right Result) int {
|
||||
if left.Score != right.Score {
|
||||
return right.Score - left.Score
|
||||
}
|
||||
if left.Length != right.Length {
|
||||
return right.Length - left.Length
|
||||
}
|
||||
return strings.Compare(left.Word, right.Word)
|
||||
})
|
||||
|
||||
if len(results) > limit {
|
||||
results = results[:limit]
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func loadWordSet(customDictionaryPath string) ([]string, error) {
|
||||
entries := map[string]struct{}{}
|
||||
|
||||
if err := loadWordsFromEmbedded(entries); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := loadWordsFromFile(entries, customDictionaryPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
words := make([]string, 0, len(entries))
|
||||
for word := range entries {
|
||||
words = append(words, word)
|
||||
}
|
||||
|
||||
slices.Sort(words)
|
||||
return words, nil
|
||||
}
|
||||
|
||||
func loadWordsFromEmbedded(entries map[string]struct{}) error {
|
||||
file, err := starterWords.Open("starter_words.txt")
|
||||
if err != nil {
|
||||
return fmt.Errorf("open starter dictionary: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
word := normalizeWord(scanner.Text())
|
||||
if len(word) >= 2 {
|
||||
entries[word] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("read starter dictionary: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadWordsFromFile(entries map[string]struct{}, path string) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("open custom dictionary: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
word := normalizeWord(scanner.Text())
|
||||
if len(word) >= 2 {
|
||||
entries[word] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("read custom dictionary: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeWord(value string) string {
|
||||
var builder strings.Builder
|
||||
for _, char := range strings.ToLower(value) {
|
||||
if unicode.IsLetter(char) {
|
||||
builder.WriteRune(char)
|
||||
}
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func letterCounts(value string) map[rune]int {
|
||||
counts := make(map[rune]int, len(value))
|
||||
for _, char := range value {
|
||||
counts[char]++
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
func canBuild(word string, available map[rune]int) bool {
|
||||
used := map[rune]int{}
|
||||
for _, char := range word {
|
||||
used[char]++
|
||||
if used[char] > available[char] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func wordScore(word string) int {
|
||||
base := 0
|
||||
for _, char := range word {
|
||||
base += letterScores[char]
|
||||
}
|
||||
|
||||
lengthBonus := len(word) * len(word)
|
||||
return base + lengthBonus
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"bookworm/internal/solver"
|
||||
)
|
||||
|
||||
//go:embed web/templates/index.html web/static/styles.css
|
||||
var webAssets embed.FS
|
||||
|
||||
type pageData struct {
|
||||
Letters string
|
||||
MinLength int
|
||||
Best *solver.Result
|
||||
Results []solver.Result
|
||||
Error string
|
||||
}
|
||||
|
||||
func main() {
|
||||
engine, err := solver.NewEngine("words/words.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("load dictionary: %v", err)
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFS(webAssets, "web/templates/index.html"))
|
||||
staticFS, err := fs.Sub(webAssets, "web/static")
|
||||
if err != nil {
|
||||
log.Fatalf("load static assets: %v", err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
||||
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
||||
data := pageData{MinLength: 3}
|
||||
|
||||
letters := request.URL.Query().Get("letters")
|
||||
if letters != "" {
|
||||
data.Letters = letters
|
||||
}
|
||||
|
||||
if rawMinLength := request.URL.Query().Get("min"); rawMinLength != "" {
|
||||
minLength, convErr := strconv.Atoi(rawMinLength)
|
||||
if convErr != nil || minLength < 2 || minLength > 12 {
|
||||
data.Error = "Minimum length must be between 2 and 12."
|
||||
} else {
|
||||
data.MinLength = minLength
|
||||
}
|
||||
}
|
||||
|
||||
if data.Letters != "" && data.Error == "" {
|
||||
results, solveErr := engine.FindMatches(data.Letters, data.MinLength, 20)
|
||||
if solveErr != nil {
|
||||
data.Error = solveErr.Error()
|
||||
} else if len(results) > 0 {
|
||||
data.Best = &results[0]
|
||||
data.Results = results[1:]
|
||||
} else {
|
||||
data.Error = "No words matched those letters in the current dictionary."
|
||||
}
|
||||
}
|
||||
|
||||
if err := tmpl.Execute(writer, data); err != nil {
|
||||
http.Error(writer, fmt.Sprintf("render page: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
|
||||
address := ":8080"
|
||||
log.Printf("Bookworm solver running at http://localhost%s", address)
|
||||
if err := http.ListenAndServe(address, mux); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,225 @@
|
||||
:root {
|
||||
--ink: #1f1c1a;
|
||||
--paper: #f4efe7;
|
||||
--panel: rgba(255, 250, 244, 0.88);
|
||||
--accent: #b24c2d;
|
||||
--accent-dark: #7a2e19;
|
||||
--muted: #6d6256;
|
||||
--line: rgba(52, 37, 28, 0.12);
|
||||
--shadow: 0 24px 60px rgba(82, 44, 19, 0.18);
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
font-family: "Aptos", "Segoe UI", sans-serif;
|
||||
color: var(--ink);
|
||||
background:
|
||||
radial-gradient(
|
||||
circle at top left,
|
||||
rgba(255, 212, 166, 0.7),
|
||||
transparent 34%
|
||||
),
|
||||
radial-gradient(
|
||||
circle at bottom right,
|
||||
rgba(184, 102, 70, 0.22),
|
||||
transparent 30%
|
||||
),
|
||||
linear-gradient(135deg, #efe4d3, #f8f5ef 40%, #ecd9c3);
|
||||
}
|
||||
|
||||
.shell {
|
||||
width: min(1080px, calc(100% - 32px));
|
||||
margin: 0 auto;
|
||||
padding: 48px 0 72px;
|
||||
}
|
||||
|
||||
.hero {
|
||||
padding: 24px 4px 28px;
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin: 0 0 10px;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.16em;
|
||||
font-size: 0.78rem;
|
||||
color: var(--accent-dark);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
p {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-family: Georgia, "Times New Roman", serif;
|
||||
font-size: clamp(2.6rem, 7vw, 4.6rem);
|
||||
line-height: 0.96;
|
||||
max-width: 8ch;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.lede {
|
||||
max-width: 54ch;
|
||||
font-size: 1.05rem;
|
||||
line-height: 1.6;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.panel {
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 24px;
|
||||
box-shadow: var(--shadow);
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.search-panel,
|
||||
.message-panel {
|
||||
padding: 24px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.search-form {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1.8fr) minmax(160px, 0.8fr) auto;
|
||||
gap: 14px;
|
||||
align-items: end;
|
||||
}
|
||||
|
||||
.search-form label {
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.search-form span {
|
||||
font-size: 0.92rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.search-form input {
|
||||
width: 100%;
|
||||
border: 1px solid rgba(90, 58, 44, 0.18);
|
||||
border-radius: 16px;
|
||||
padding: 14px 16px;
|
||||
font-size: 1rem;
|
||||
background: rgba(255, 255, 255, 0.72);
|
||||
}
|
||||
|
||||
.search-form button {
|
||||
border: 0;
|
||||
border-radius: 16px;
|
||||
padding: 14px 20px;
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
color: #fff8f3;
|
||||
background: linear-gradient(180deg, var(--accent), var(--accent-dark));
|
||||
cursor: pointer;
|
||||
transition:
|
||||
transform 120ms ease,
|
||||
box-shadow 120ms ease;
|
||||
box-shadow: 0 16px 28px rgba(122, 46, 25, 0.26);
|
||||
}
|
||||
|
||||
.search-form button:hover {
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.hint,
|
||||
.message,
|
||||
.results-header p,
|
||||
.stats {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin: 14px 2px 0;
|
||||
}
|
||||
|
||||
.results-grid {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(280px, 0.9fr) minmax(0, 1.1fr);
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.best-card,
|
||||
.results-card {
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.card-label {
|
||||
margin-bottom: 12px;
|
||||
font-size: 0.82rem;
|
||||
font-weight: 700;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.12em;
|
||||
color: var(--accent-dark);
|
||||
}
|
||||
|
||||
.best-card h2 {
|
||||
margin-bottom: 12px;
|
||||
font-family: Georgia, "Times New Roman", serif;
|
||||
font-size: clamp(2.4rem, 7vw, 4rem);
|
||||
line-height: 0.95;
|
||||
}
|
||||
|
||||
.stats {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.results-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: end;
|
||||
gap: 16px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.results-header h3 {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.results-list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.results-list li {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 14px 16px;
|
||||
border-radius: 16px;
|
||||
background: rgba(255, 255, 255, 0.72);
|
||||
border: 1px solid rgba(90, 58, 44, 0.08);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
@media (max-width: 820px) {
|
||||
.shell {
|
||||
width: min(100% - 24px, 1080px);
|
||||
padding-top: 28px;
|
||||
}
|
||||
|
||||
.search-form,
|
||||
.results-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.results-header {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Bookworm Solver</title>
|
||||
<link rel="stylesheet" href="/static/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<main class="shell">
|
||||
<section class="hero">
|
||||
<p class="eyebrow">Golang word finder</p>
|
||||
<h1>Bookworm-style solver</h1>
|
||||
<p class="lede">
|
||||
Type the letters you have, and the app ranks the strongest words it
|
||||
can build from them.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section class="panel search-panel">
|
||||
<form class="search-form" method="GET" action="/">
|
||||
<label>
|
||||
<span>Letters</span>
|
||||
<input
|
||||
name="letters"
|
||||
type="text"
|
||||
value="{{.Letters}}"
|
||||
placeholder="example: triangle"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Minimum length</span>
|
||||
<input
|
||||
name="min"
|
||||
type="number"
|
||||
min="2"
|
||||
max="12"
|
||||
value="{{.MinLength}}"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<button type="submit">Find best word</button>
|
||||
</form>
|
||||
|
||||
<p class="hint">
|
||||
Scoring favors both valuable letters and longer words. Add more words
|
||||
later with <strong>words/words.txt</strong>.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
{{if .Error}}
|
||||
<section class="panel message-panel">
|
||||
<p class="message">{{.Error}}</p>
|
||||
</section>
|
||||
{{end}} {{if .Best}}
|
||||
<section class="results-grid">
|
||||
<article class="panel best-card">
|
||||
<p class="card-label">Best word</p>
|
||||
<h2>{{.Best.Word}}</h2>
|
||||
<div class="stats">
|
||||
<span>Score {{.Best.Score}}</span>
|
||||
<span>{{.Best.Length}} letters</span>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<article class="panel results-card">
|
||||
<div class="results-header">
|
||||
<h3>Other strong matches</h3>
|
||||
<p>Top ranked alternatives from the same letter set.</p>
|
||||
</div>
|
||||
<ul class="results-list">
|
||||
<li>
|
||||
<span>{{.Best.Word}}</span>
|
||||
<span>{{.Best.Score}}</span>
|
||||
</li>
|
||||
{{range .Results}}
|
||||
<li>
|
||||
<span>{{.Word}}</span>
|
||||
<span>{{.Score}}</span>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</article>
|
||||
</section>
|
||||
{{end}}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
+466550
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user