mdict

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: GPL-3.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mdict wraps the low-level MDX/MDD engine with the behaviour this project needs: recursive discovery, stable dictionary IDs, correct @@@LINK redirection, normalized lookup, and O(1) MDD resource resolution.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a headword or resource is absent.

Functions

func IsAudioRef

func IsAudioRef(ref string) bool

IsAudioRef reports whether a reference points at a pronunciation asset.

func IsSpeexRef

func IsSpeexRef(ref string) bool

IsSpeexRef reports whether a reference needs Speex decoding before macOS can play it.

func MIMEType

func MIMEType(ref string) string

MIMEType maps a resource reference to the content type the resource endpoint should serve. SPX is reported as audio/wav because it is transcoded on the way out.

func NormalizeExactKey

func NormalizeExactKey(word string) string

NormalizeExactKey produces the canonical identity used by exact lookup, redirect cycle detection, cache keys and suggestion deduplication. Unicode canonically equivalent spellings share identity; letter case does not.

func NormalizeResourceKey

func NormalizeResourceKey(ref string) string

NormalizeResourceKey converts any MDD key or entry reference into the single canonical form used by the resource index: lowercase, forward slashes, no leading separator, no scheme.

MDD keys are stored Windows-style (for example "\\synthetic\\uk\\clip.mp3") while entry HTML refers to them with a `sound://` URL, so both forms must fold together.

func ParseLinkTarget

func ParseLinkTarget(content []byte) (string, bool)

ParseLinkTarget reports whether a record is a redirect stub and, if so, the headword it points at.

Real records look like "@@@LINK=Hello!\r\n\x00" — note the trailing NUL, which ordinary whitespace trimming leaves behind.

func ResourceCandidates

func ResourceCandidates(ref string) []string

ResourceCandidates expands a reference into the normalized keys worth trying, most specific first.

Types

type Dictionary

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

Dictionary is one MDX file plus its MDD volume chain.

func (*Dictionary) Close

func (d *Dictionary) Close()

Close releases the underlying file handles.

func (*Dictionary) HasResource

func (d *Dictionary) HasResource(ref string) bool

HasResource reports whether a reference resolves, without decompressing it. The audio pipeline uses this to avoid advertising pronunciations that the user's MDD cannot actually deliver.

func (*Dictionary) ID

func (d *Dictionary) ID() string

ID returns the stable dictionary identifier.

func (*Dictionary) Info

func (d *Dictionary) Info() Info

Info returns a snapshot of the dictionary metadata.

func (*Dictionary) Load

func (d *Dictionary) Load() error

Load opens the MDX and its MDD volumes and builds all indexes. It is safe to call concurrently; only the first call does work.

func (*Dictionary) Lookup

func (d *Dictionary) Lookup(word string) (*LookupResult, error)

Lookup is the single-record convenience API. LookupAll owns all matching, redirect and deduplication semantics so the two paths cannot diverge.

func (*Dictionary) LookupAll

func (d *Dictionary) LookupAll(word string) (*LookupSet, error)

LookupAll resolves a headword and returns every exact record belonging to the selected key. Case resolution happens before duplicate expansion: folded candidates with other spellings are never mixed into the result.

The bundled engine's own redirect handling matches the prefix "@@LINK=" while real MDict files use "@@@LINK=", and it does not strip the trailing NUL that terminates these records, so redirects are resolved here instead.

func (*Dictionary) MDDPaths

func (d *Dictionary) MDDPaths() []string

MDDPaths returns the MDD volume chain.

func (*Dictionary) Prefix

func (d *Dictionary) Prefix(prefix string, limit int) []string

Prefix returns up to limit original headword spellings starting with the prefix. Matching is case-insensitive; returned spelling is never folded.

func (*Dictionary) Resource

func (d *Dictionary) Resource(ref string) ([]byte, error)

Resource resolves an MDD resource reference such as "sound://synthetic/uk/example.mp3" to its raw bytes. Lookup is O(1) against the prebuilt volume index.

func (*Dictionary) ResourceKinds

func (d *Dictionary) ResourceKinds() map[string]int

ResourceKinds counts the MDD resources by file extension.

It is what makes "this dictionary has 100k recordings but no images" a statement the service can actually make, rather than a guess.

func (*Dictionary) SourcePath

func (d *Dictionary) SourcePath() string

SourcePath returns the MDX path. It is used for diagnostics and CLI output only and is never exposed through the HTTP API.

type Health

type Health string

Health describes whether a dictionary is usable.

const (
	HealthOK          Health = "ok"
	HealthUnavailable Health = "unavailable"
)

type Info

type Info struct {
	ID          string   `json:"id"`
	Title       string   `json:"title"`
	Description string   `json:"description,omitempty"`
	EntryCount  int64    `json:"entryCount"`
	Encoding    string   `json:"encoding,omitempty"`
	Version     string   `json:"version,omitempty"`
	CreatedAt   string   `json:"createdAt,omitempty"`
	HasMDD      bool     `json:"hasMDD"`
	MDDVolumes  int      `json:"mddVolumes"`
	Profile     string   `json:"profile"`
	Health      Health   `json:"health"`
	Diagnostics []string `json:"diagnostics,omitempty"`
	// LoadedAt is when the index finished building.
	LoadedAt time.Time `json:"loadedAt,omitzero"`
}

Info is the user-visible description of one dictionary.

type LookupResult

type LookupResult struct {
	// MatchedKey is the dictionary key that actually matched.
	MatchedKey string
	// RedirectedFrom is the original key when redirection was followed.
	RedirectedFrom string
	// HTML is the raw record content.
	HTML []byte
	// RawRecordOrdinal is the one-based source position among exact records
	// with the same MatchedKey. It is stable diagnostic provenance, not the
	// visible ordinal assigned after semantic filtering.
	RawRecordOrdinal int
	// RecordStartOffset is a stable low-level locator useful in debug reports.
	RecordStartOffset int64
}

LookupResult carries a resolved entry plus how it was reached.

type LookupSet

type LookupSet struct {
	MatchedKey string
	Records    []*LookupResult
}

LookupSet is the duplicate-aware result of resolving one query. MatchedKey is chosen once using exact-case, NFC, then deterministic case fallback. Records contains only that key's records (plus records reached through their redirects), in source order and deduplicated by resolved bytes.

type Registry

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

Registry owns every discovered dictionary and the order they are queried in.

func NewRegistry

func NewRegistry(root string) *Registry

NewRegistry creates an empty registry rooted at a dictionary directory.

func (*Registry) All

func (r *Registry) All() []*Dictionary

All returns every dictionary in query order.

func (*Registry) ByID

func (r *Registry) ByID(id string) (*Dictionary, bool)

ByID returns one dictionary.

func (*Registry) Counts

func (r *Registry) Counts() (total int, healthy int)

Counts returns the total and healthy dictionary counts.

func (*Registry) LoadAll

func (r *Registry) LoadAll()

LoadAll eagerly builds every index. Called at startup so the first user lookup is warm.

func (*Registry) Root

func (r *Registry) Root() string

Root returns the dictionary directory being watched.

func (*Registry) Scan

func (r *Registry) Scan() error

Scan walks the dictionary root recursively and rebuilds the registry.

Discovery is per-directory: every .mdx becomes a dictionary, and the .mdd files beside it whose stem matches (allowing a .N volume suffix) become its resource chain. Users therefore only ever copy a folder into place.

func (*Registry) Select

func (r *Registry) Select(ids []string) []*Dictionary

Select resolves a caller-supplied ID list into dictionaries, preserving the caller's order. An empty list means "all, in registry order".

Jump to

Keyboard shortcuts

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