Editor: document tone, right-click word lookup, expanded stats

Four enhancements to make the editor fit real school usage:

- Per-document tone (academic/professional/casual/humorous/creative/
  persuasive/general): new documents.tone column (migration 0002), threaded
  through the docs API, a bilingual ToneSelect dropdown on the title row, and
  injected into the grammar-checkpoint LLM prompt so advice fits the register.
  The voice pass stays tone-agnostic.

- Right-click word lookup: a new offline `lexicon` package serves definitions
  (Wordset, modern ESL-friendly glosses) and synonyms (WordNet synsets first,
  then frequency+stopword-ranked Moby for breadth) from gzipped embedded data,
  behind /api/word/{word} with light morphology. The WordCard popover shows the
  definition and tappable synonym pills that swap the word in place.

- Expanded writing stats: clicking the word count opens a StatsPanel with page
  count, sentences, paragraphs, reading time, average word length, word variety,
  and Flesch-Kincaid reading level — all computed client-side.

Claude-Session: https://claude.ai/code/session_016Yr6jELuRc7hyzYLccQKZd
This commit is contained in:
prosolis
2026-06-25 23:22:55 -07:00
parent 95123e8c49
commit 4c288834c0
23 changed files with 1021 additions and 32 deletions

21
internal/lexicon/data.go Normal file
View File

@@ -0,0 +1,21 @@
// Package lexicon serves offline word lookups — a definition and a list of
// synonyms for a single word — from two public-domain datasets compiled into the
// binary. Definitions come from the Wordset dictionary (modern, concise glosses
// with a part of speech and example, which read kindly for an ESL writer);
// synonyms come from the Moby Thesaurus. Both are gzipped JSON, decompressed
// lazily on first use so a writer who never right-clicks a word pays nothing.
package lexicon
import _ "embed"
// definitionsGz is the gzipped Wordset definitions map: word → [[pos, def,
// example], …], lowercase keys. Built by the data-prep step (see commit notes).
//
//go:embed data/definitions.json.gz
var definitionsGz []byte
// synonymsGz is the gzipped Moby thesaurus map: headword → [synonym, …],
// lowercase keys, capped per word to keep the popover (and the binary) small.
//
//go:embed data/synonyms.json.gz
var synonymsGz []byte

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,50 @@
package lexicon
import (
"encoding/json"
"net/http"
"net/url"
"github.com/go-chi/chi/v5"
)
// Handler serves the word-lookup endpoint backed by a single shared Lexicon.
type Handler struct {
Lex *Lexicon
}
// New constructs a Handler with a fresh (lazily-loaded) Lexicon.
func NewHandler() *Handler { return &Handler{Lex: New()} }
// Routes returns the router mounted at /api/word. The word is a path segment so
// "/api/word/happy" reads naturally; it's URL-decoded to tolerate the rare
// punctuated token.
func (h *Handler) Routes() chi.Router {
r := chi.NewRouter()
r.Get("/{word}", h.lookup)
return r
}
// lookup returns the definition + synonyms for one word. A word found in neither
// dataset still returns 200 with empty lists, so the popover can show a friendly
// "nothing found" rather than an error state.
func (h *Handler) lookup(w http.ResponseWriter, r *http.Request) {
word := chi.URLParam(r, "word")
if decoded, err := url.PathUnescape(word); err == nil {
word = decoded
}
res, err := h.Lex.Lookup(word)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
// Word lookups are static for the life of the build; let the browser cache
// them so repeated right-clicks on the same word are instant.
w.Header().Set("Cache-Control", "public, max-age=86400")
_ = json.NewEncoder(w).Encode(res)
}

205
internal/lexicon/lexicon.go Normal file
View File

@@ -0,0 +1,205 @@
package lexicon
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
)
// Meaning is one sense of a word: its part of speech, the gloss, and an optional
// usage example. The frontend renders a few of these in the word popover.
type Meaning struct {
PartOfSpeech string `json:"part_of_speech"`
Definition string `json:"definition"`
Example string `json:"example,omitempty"`
}
// Result is the full lookup for one word. Either list may be empty (the word
// isn't a headword in that dataset); the frontend handles a partial or empty
// result gracefully.
type Result struct {
Word string `json:"word"`
Definitions []Meaning `json:"definitions"`
Synonyms []string `json:"synonyms"`
}
// maxSynonyms caps how many synonyms we hand the popover, even though the dataset
// stores up to ~50 per word — a long flat wall of words overwhelms more than it
// helps, especially for an ESL reader scanning for the right fit.
const maxSynonyms = 16
// maxDefinitions caps the senses shown so the popover stays a glance, not an
// essay.
const maxDefinitions = 4
// Lexicon holds the lazily-loaded datasets. The maps are populated once, on the
// first Lookup, behind a sync.Once so startup stays instant and a load error is
// remembered rather than retried on every request.
type Lexicon struct {
once sync.Once
loadErr error
defs map[string][][]string // word → [[pos, def, example], …]
synonyms map[string][]string // word → [synonym, …]
}
// New returns a Lexicon. The datasets aren't read until the first Lookup.
func New() *Lexicon { return &Lexicon{} }
func (l *Lexicon) load() {
l.once.Do(func() {
if err := gunzipJSON(definitionsGz, &l.defs); err != nil {
l.loadErr = fmt.Errorf("load definitions: %w", err)
return
}
if err := gunzipJSON(synonymsGz, &l.synonyms); err != nil {
l.loadErr = fmt.Errorf("load synonyms: %w", err)
return
}
})
}
// Lookup returns the definition senses and synonyms for word. It first tries the
// word as written (lowercased), then a few simple morphological reductions
// (plurals, -ed/-ing/-ly) so "running" or "happily" still resolve. A word found
// in neither dataset yields a Result with empty lists (not an error).
func (l *Lexicon) Lookup(word string) (Result, error) {
l.load()
if l.loadErr != nil {
return Result{}, l.loadErr
}
norm := strings.ToLower(strings.TrimSpace(word))
res := Result{Word: word, Definitions: []Meaning{}, Synonyms: []string{}}
if norm == "" {
return res, nil
}
if raw := lookupDefs(l.defs, norm); raw != nil {
for _, m := range raw {
res.Definitions = append(res.Definitions, toMeaning(m))
if len(res.Definitions) >= maxDefinitions {
break
}
}
}
if syns := lookupSyns(l.synonyms, norm); syns != nil {
if len(syns) > maxSynonyms {
syns = syns[:maxSynonyms]
}
res.Synonyms = append(res.Synonyms, syns...)
}
return res, nil
}
// lookupDefs / lookupSyns walk the candidate forms of a word and return the
// first dataset hit. They're separate (rather than a generic helper) only
// because the two maps have different value types.
func lookupDefs(m map[string][][]string, word string) [][]string {
for _, c := range candidates(word) {
if v, ok := m[c]; ok {
return v
}
}
return nil
}
func lookupSyns(m map[string][]string, word string) []string {
for _, c := range candidates(word) {
if v, ok := m[c]; ok {
return v
}
}
return nil
}
// candidates returns the lemma forms to try, in priority order: the word itself,
// then conservative de-inflections. This is deliberately lightweight — a full
// stemmer would over-reduce ("business" → "busy") and surface wrong entries; a
// handful of common English suffix rules covers the everyday cases without a
// dependency. Duplicates are fine (map lookup is cheap); order is what matters.
func candidates(word string) []string {
out := []string{word}
add := func(s string) {
if len(s) >= 2 && s != word {
out = append(out, s)
}
}
switch {
case strings.HasSuffix(word, "ies"): // studies → study
add(word[:len(word)-3] + "y")
case strings.HasSuffix(word, "es"): // boxes → box, wishes → wish
add(word[:len(word)-2])
add(word[:len(word)-1])
case strings.HasSuffix(word, "s"): // cats → cat
add(word[:len(word)-1])
}
if strings.HasSuffix(word, "ing") { // running → run, making → make
stem := word[:len(word)-3]
add(stem)
add(stem + "e")
add(undouble(stem))
}
if strings.HasSuffix(word, "ed") { // hoped → hope, stopped → stop
stem := word[:len(word)-2]
add(stem)
add(word[:len(word)-1])
add(undouble(stem))
}
if strings.HasSuffix(word, "ly") { // happily handled above via ies path? no — quickly → quick
add(word[:len(word)-2])
}
if strings.HasSuffix(word, "ily") { // happily → happy
add(word[:len(word)-3] + "y")
}
return out
}
// undouble collapses a doubled final consonant (stopp → stop, runn → run) so the
// -ed/-ing stems of doubled-consonant verbs resolve to their base form.
func undouble(stem string) string {
n := len(stem)
if n >= 2 && stem[n-1] == stem[n-2] {
return stem[:n-1]
}
return stem
}
// toMeaning maps a compact [pos, def, example] triple from the dataset onto the
// JSON-friendly Meaning. The dataset always stores three elements, but we guard
// the length so a malformed row can't panic.
func toMeaning(m []string) Meaning {
var out Meaning
if len(m) > 0 {
out.PartOfSpeech = m[0]
}
if len(m) > 1 {
out.Definition = m[1]
}
if len(m) > 2 {
out.Example = m[2]
}
return out
}
// gunzipJSON decompresses gz and decodes the JSON into v.
func gunzipJSON(gz []byte, v any) error {
r, err := gzip.NewReader(bytes.NewReader(gz))
if err != nil {
return err
}
defer r.Close()
data, err := io.ReadAll(r)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}

View File

@@ -0,0 +1,76 @@
package lexicon
import "testing"
func TestLookupKnownWord(t *testing.T) {
l := New()
res, err := l.Lookup("happy")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
if len(res.Definitions) == 0 {
t.Errorf("expected definitions for %q, got none", "happy")
}
if len(res.Synonyms) == 0 {
t.Errorf("expected synonyms for %q, got none", "happy")
}
if len(res.Synonyms) > maxSynonyms {
t.Errorf("synonyms not capped: got %d, want <= %d", len(res.Synonyms), maxSynonyms)
}
if len(res.Definitions) > maxDefinitions {
t.Errorf("definitions not capped: got %d, want <= %d", len(res.Definitions), maxDefinitions)
}
}
func TestLookupMorphology(t *testing.T) {
l := New()
// Inflected forms should resolve to their base entry via candidates().
for _, w := range []string{"running", "studies", "boxes", "quickly", "stopped"} {
res, err := l.Lookup(w)
if err != nil {
t.Fatalf("Lookup(%q): %v", w, err)
}
if len(res.Definitions) == 0 && len(res.Synonyms) == 0 {
t.Errorf("expected some result for inflected %q, got nothing", w)
}
}
}
func TestLookupUnknownWord(t *testing.T) {
l := New()
res, err := l.Lookup("zzzxqqq")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
if len(res.Definitions) != 0 || len(res.Synonyms) != 0 {
t.Errorf("expected empty result for nonsense word, got %+v", res)
}
// Empty result must still serialize as [] not null for the frontend.
if res.Definitions == nil || res.Synonyms == nil {
t.Errorf("empty slices must be non-nil for JSON []: %+v", res)
}
}
func TestCandidates(t *testing.T) {
cases := map[string]string{
"cats": "cat",
"studies": "study",
"running": "run",
"hoped": "hope",
"quickly": "quick",
"happily": "happy",
}
for word, want := range cases {
got := candidates(word)
found := false
for _, c := range got {
if c == want {
found = true
break
}
}
if !found {
t.Errorf("candidates(%q) = %v, missing expected base %q", word, got, want)
}
}
}