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 ¶
- Variables
- func IsAudioRef(ref string) bool
- func IsSpeexRef(ref string) bool
- func MIMEType(ref string) string
- func NormalizeExactKey(word string) string
- func NormalizeResourceKey(ref string) string
- func ParseLinkTarget(content []byte) (string, bool)
- func ResourceCandidates(ref string) []string
- type Dictionary
- func (d *Dictionary) Close()
- func (d *Dictionary) HasResource(ref string) bool
- func (d *Dictionary) ID() string
- func (d *Dictionary) Info() Info
- func (d *Dictionary) Load() error
- func (d *Dictionary) Lookup(word string) (*LookupResult, error)
- func (d *Dictionary) LookupAll(word string) (*LookupSet, error)
- func (d *Dictionary) MDDPaths() []string
- func (d *Dictionary) Prefix(prefix string, limit int) []string
- func (d *Dictionary) Resource(ref string) ([]byte, error)
- func (d *Dictionary) ResourceKinds() map[string]int
- func (d *Dictionary) SourcePath() string
- type Health
- type Info
- type LookupResult
- type LookupSet
- type Registry
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a headword or resource is absent.
Functions ¶
func IsAudioRef ¶
IsAudioRef reports whether a reference points at a pronunciation asset.
func IsSpeexRef ¶
IsSpeexRef reports whether a reference needs Speex decoding before macOS can play it.
func MIMEType ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) 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" )
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 ¶
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) LoadAll ¶
func (r *Registry) LoadAll()
LoadAll eagerly builds every index. Called at startup so the first user lookup is warm.
func (*Registry) Scan ¶
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".