dictdb

package
v0.0.0-...-bf6e1ee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package dictdb reads dictd-format dictionaries: a sorted .index of headwords and a .dict.dz body compressed for random access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(entry string) string

Clean turns one raw dictionary entry into plain text fit for a terminal. It is safe to run on entries from any of the databases: WordNet and FOLDOC use none of these conventions, so for them it is very nearly a no-op.

func Lemmas

func Lemmas(word string) []string

Lemmas returns the base forms to try for a word, best first, when the word itself has no entry.

This matters more than it might seem. /usr/share/dict/words is a spell checker's list, so it carries every inflection -- "abacuses", "overacts", "smoggiest" -- while a dictionary files definitions under the lemma. Around half of all apparent misses are really an inflected form of a headword that is present, so trying the base forms lifts coverage of the word list from roughly 62% to 90%.

The rules are deliberately generous: a wrong guess costs one failed binary search, and the caller keeps the first candidate that actually resolves.

func Wrap

func Wrap(s string, width int) []string

Wrap breaks text to the given width, keeping the indentation of each line so that numbered senses and quotations stay visually distinct.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is one dictionary: an index of headwords over a dictzip body.

func Open

func Open(name string, index io.Reader, body io.ReaderAt) (*DB, error)

Open builds a DB from the contents of a .index file and a .dict.dz body.

func (*DB) Has

func (d *DB) Has(word string) bool

Has reports whether the headword is present, without decompressing.

func (*DB) Len

func (d *DB) Len() int

Len reports how many headwords the dictionary holds.

func (*DB) Lookup

func (d *DB) Lookup(word string) ([]string, error)

Lookup returns every definition filed under exactly this headword, matched without regard to case. A word can have several, so all are returned.

func (*DB) Name

func (d *DB) Name() string

Name returns the short id of the dictionary.

func (*DB) Title

func (d *DB) Title() string

Title returns its human-readable name.

type Dictionary

type Dictionary interface {
	Name() string
	Title() string
	Lookup(word string) ([]string, error)
	Has(word string) bool
}

Dictionary is one source of definitions. Both a dictd database and the generated gloss file satisfy it.

type Gloss

type Gloss struct {
	// contains filtered or unexported fields
}

Gloss is a dictionary of one-line entries, used for the names and places that a conventional dictionary does not define.

It exists because the word list carries thousands of surnames and towns -- "Hendricks", "Jaipur" -- that GCIDE and WordNet have no entry for. Confirming that such a spelling is a real surname, and roughly how common, is the useful answer for those; a full dictionary entry neither exists nor is wanted.

Only words that nothing else defines are included, so the file stays at tens of kilobytes rather than the tens of megabytes of the sources it is built from.

func ReadGloss

func ReadGloss(name, title string, r io.Reader) (*Gloss, error)

ReadGloss parses the tab-separated, gzipped gloss file: one word and its text per line, sorted by word.

func (*Gloss) Has

func (g *Gloss) Has(word string) bool

Has reports whether the word has a gloss.

func (*Gloss) Len

func (g *Gloss) Len() int

Len reports how many words are glossed.

func (*Gloss) Lookup

func (g *Gloss) Lookup(word string) ([]string, error)

Lookup returns the gloss for a word, if there is one. A word can carry more than one -- a surname that is also a town -- and they arrive as one entry with embedded newlines.

func (*Gloss) Name

func (g *Gloss) Name() string

Name returns the short id of this source.

func (*Gloss) Title

func (g *Gloss) Title() string

Title returns its human-readable name.

type ReaderAtSizer

type ReaderAtSizer interface {
	io.ReaderAt
}

ReaderAtSizer is what a dictionary body must provide: random access and a known size. Both an *os.File and a bytes.Reader over embedded data qualify.

type Result

type Result struct {
	DB    string // which dictionary, e.g. "gcide"
	Title string // its human-readable name
	Word  string // the headword actually matched, which may be a lemma
	Text  string // the definition, cleaned for display
}

Result is one definition found for a word.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is an ordered group of dictionaries, consulted in turn.

Order is the whole point: GCIDE is asked first because its 1913 prose is what makes the tool pleasant, and WordNet follows because GCIDE predates most modern vocabulary. The specialist databases come last and cover what neither general dictionary has -- acronyms, computing terms.

Dictionaries are opened lazily. Parsing a 4MB index costs real time, so a dictionary that is never consulted is never read.

func (*Set) Add

func (s *Set) Add(name string, open func() (Dictionary, error))

Add registers a dictionary under a name, to be opened on first use.

func (*Set) AddDir

func (s *Set) AddDir(dir, name string) error

AddDir registers a dictionary from a directory of dictd files, so an installed dict-gcide or dict-wn is used in preference to the embedded copy.

func (*Set) AddFS

func (s *Set) AddFS(fsys fs.FS, name, indexPath, bodyPath string)

AddFS registers a dictionary held in an fs.FS, which is how the embedded copies are reached. The whole body is read into memory because an fs.File offers no random access; the index is streamed.

func (*Set) Covers

func (s *Set) Covers(word string) (db, headword string, ok bool)

Covers reports whether the word can be defined, and under which headword, without decompressing anything. It exists for measuring coverage over a whole word list, where inflating every entry would be wasteful.

func (*Set) Define

func (s *Set) Define(word string) []Result

Define looks a word up across the set.

Every dictionary is asked for the exact word first. Only if none has it are the base forms tried, so a word that is genuinely present is never shadowed by another dictionary's entry for its stem.

func (*Set) Names

func (s *Set) Names() []string

Names lists the registered dictionaries, in consultation order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL