> For AI agents: the complete documentation index is available at https://canary.gukhanmun.org/llms.txt, the full documentation bundle is available at https://canary.gukhanmun.org/llms-full.txt.

# Dictionaries

Gukhanmun looks up hanja readings from one or more `HanjaDictionary`
implementations.  The `gukhanmun` crate ships with FST and CDB backends and the
bundled _Standard Korean Dictionary_, _Loanword spelling examples_, and the
_Open Korean Dictionary_ (우리말샘) North Korean dictionary used by the `ko-kp`
preset.

## Using the bundled dictionary

The bundled dictionary selected by the preset is included automatically.  To
disable every bundled dictionary and rely only on user dictionaries or fallback
readings:

```rust
let converter = Builder::with_preset(Preset::KoKr)
    .no_bundled_dictionaries()
    .build()?;
```

Use `no_bundled_stdict()` or `no_bundled_opendict()` to disable only one
bundled dictionary family.  To re-enable the _Standard Korean Dictionary_
explicitly after calling `no_bundled_stdict()`:

```rust
builder.bundled_stdict();
```

Use the `ko-kp` preset for North Korean orthography.  It disables the initial
sound law and includes the bundled _Open Korean Dictionary_ (우리말샘) North
Korean (北韓語) dictionary:

```rust
let converter = Builder::with_preset(Preset::KoKp).build()?;
assert_eq!(converter.convert_text_to_string("歷史와 來日")?, "력사와 래일");
```

To keep the `ko-kp` orthographic options but disable its bundled _Open Korean
Dictionary_ data:

```rust
let converter = Builder::with_preset(Preset::KoKp)
    .no_bundled_opendict()
    .build()?;
```

## Loanword spelling examples

The `gukhanmun-loanwords` crate bundles the National Institute of Korean
Language's _Loanword spelling examples_ as an offline FST dictionary. The
`loanwords` feature is enabled by default, and `Preset::KoKr` includes this
dictionary automatically:

```rust
use gukhanmun::{Builder, Preset};

let converter = Builder::with_preset(Preset::KoKr)
    .build()?;
assert_eq!(converter.convert_text_to_string("香港")?, "홍콩");
```

`Preset::KoKp` leaves this dictionary off; call `.bundled_loanwords()` to
enable it explicitly. User dictionaries take priority. Bundled reference and
loanword sources cooperate under the policy described below.
`.no_bundled_loanwords()` disables only this dictionary;
`.no_bundled_dictionaries()` disables it along with the other bundled
dictionaries.

For direct use through `HanjaDictionary`, add the `gukhanmun-loanwords` crate
and call `gukhanmun_loanwords::dictionary()`. With the umbrella crate's
`loanwords` feature, the same function is available as
`gukhanmun::loanwords::dictionary()`.

## Loading a dictionary from a file

Requires the `fst` or `cdb` feature.

```rust
use gukhanmun::FstDictionary;  // or CdbDictionary

let dict = FstDictionary::open("custom.gukfst")?;
let converter = Builder::with_preset(Preset::KoKr)
    .push_dictionary(dict)
    .build()?;
```

Dictionaries added with `push_dictionary` are consulted before the bundled
dictionary. Earlier user dictionaries replace later candidates for the same
spelling and consumed length, including candidates from bundled sources.

## Zero-copy static dictionaries

Embed a dictionary directly in your binary with `include_bytes!` and load it
without any file I/O:

```rust
use gukhanmun::FstDictionary;

static MY_DICT: &[u8] = include_bytes!("../data/custom.gukfst");

let dict = FstDictionary::from_static_bytes(MY_DICT)?;
```

`from_static_bytes` does not copy the data; it creates a zero-copy view backed
by the static slice.

## Loading from owned bytes

Both FST and CDB accept a byte slice through `from_bytes()` and copy it
into their own backing store:

```rust
use gukhanmun::CdbDictionary;

let bytes: Vec<u8> = std::fs::read("custom.gukcdb")?;
let dict = CdbDictionary::from_bytes(&bytes)?;
```

## Chaining multiple dictionaries

`ChainDictionary` lets you compose several dictionaries with explicit priority
ordering. For the same spelling and consumed length, the first dictionary's
candidate set wins. Matches of different lengths remain available to the
segmenter:

```rust
use gukhanmun::{ChainDictionary, FstDictionary, CdbDictionary, HanjaDictionary};

let domain_dict = FstDictionary::open("legal.gukfst")?;
let names_dict = CdbDictionary::open("names.gukcdb")?;
let chain = ChainDictionary::from_iter([
    Box::new(domain_dict) as Box<dyn HanjaDictionary>,
    Box::new(names_dict),
]);

let converter = Builder::with_preset(Preset::KoKr)
    .no_bundled_stdict()
    .push_boxed_dictionary(Box::new(chain))
    .build()?;
```

Alternatively, call `push_dictionary` multiple times; dictionaries are probed
in the order they were pushed, before the bundled dictionary.

## Composing Open Korean Dictionary categories

When the `opendict` feature is enabled, Rust callers can load the _Open Korean
Dictionary_ general, North Korean, dialect, and archaic categories directly.
The `ko-kp` preset includes the North Korean category automatically, but
dialect and archaic data are opt-in:

```rust
use gukhanmun::{Builder, ChainDictionary, HanjaDictionary, Preset};

let chain = ChainDictionary::from_iter([
    Box::new(gukhanmun::opendict::dialect()) as Box<dyn HanjaDictionary>,
    Box::new(gukhanmun::opendict::archaic()),
]);

let converter = Builder::with_preset(Preset::KoKr)
    .push_boxed_dictionary(Box::new(chain))
    .build()?;
```

## Building a custom dictionary

The _.gukfst_ and _.gukcdb_ files loaded above are compiled artifacts, built
from a plain text table with the `gukhanmun-mkdict` tool.  See
[_Building a custom dictionary_](https://canary.gukhanmun.org/guide/cli/dictionary.md#building-a-custom-dictionary)
in the CLI guide for how to author and compile dictionary sources.

## Keeping alternative readings

Use `insert_candidates()` when a key has competing lexical readings. Ordinary
`insert()` and `insert_with_suffix()` still replace the entire key.

```rust
use gukhanmun::{MapDictionary, MatchMark, ReadingCandidate, ReadingCandidates};

let candidates = ReadingCandidates::new(vec![
    ReadingCandidate {
        reading: "동경".into(), suffix_reading: None,
        mark: MatchMark::default(), preference: 0, alias: None,
    },
    ReadingCandidate {
        reading: "도쿄".into(), suffix_reading: None,
        mark: MatchMark::default(), preference: 10, alias: None,
    },
])?;
let mut dictionary = MapDictionary::new();
dictionary.insert_candidates("東京", candidates);
```

The context-free default is `도쿄`. Conversion selects `동경` in `東京을` and
`도쿄` in `東京를`, leaving the particle unchanged. A higher-priority
dictionary replaces the whole set for `東京`. Use `candidate_matches_at()` or
`candidate_entries()` to inspect alternatives; `matches_at()` and `entries()`
expose only the selected default. FST/CDB `lookup().candidates()` exposes the
same sets.

When the readings arrive one per row, `MapDictionary::from_candidates()` groups
repeated keys into the same candidate set, so a table of `(hanja, reading)`
rows needs no manual grouping:

```rust
use gukhanmun::{MapDictionary, MatchMark, ReadingCandidate};

let dictionary = MapDictionary::from_candidates([
    ("東京", ReadingCandidate {
        reading: "동경".into(), suffix_reading: None,
        mark: MatchMark::default(), preference: 0, alias: None,
    }),
    ("東京", ReadingCandidate {
        reading: "도쿄".into(), suffix_reading: None,
        mark: MatchMark::default(), preference: 10, alias: None,
    }),
])?;
```

Preferences are normalised per key exactly as `ReadingCandidates::new()` does,
and an empty hanja key is rejected with `CandidateError::EmptyKey`.

### Particle evidence and limits

The filter runs automatically after dictionary precedence and variant identity
resolution. It keeps the first compatible candidate in preference order;
uncertain context or an empty survivor set uses the default. An override that
leaves a single reading therefore stays authoritative. Candidate marks and
positional spellings travel with the selected reading. Unknown endings remain
eligible. If a custom candidate's initial and suffix spellings disagree, either
compatible or unknown ending keeps that candidate eligible.

Supported particles are `을/를`, `은/는`, `이/가`, `과/와`, and `으로/로`,
including the rieul exception. Supported combinations are `으로는`, `으로도`,
`으로만`, `로는`, `로도`, `로만`, `과는`, `과도`, `과만`, `와는`, `와도`, and
`와만`. A complete suffix must end at whitespace, EOF, a block boundary
reported by the reader, or `.,!?:;)]}。、！？，：；）】」』`. Copulas such as
`이다`, lexical prefixes such as `이름`, and unsupported combinations keep the
default.

Streaming holds four extra source scalars beyond dictionary lookahead.
`finish()` proves EOF; calling `flush()` inside a word may forfeit evidence.
Inline markup, preserved/verbatim transitions, Markdown soft/hard line breaks
and links, HTML `br`, literal entities, and unlisted quotation marks do not
prove a boundary. For example, `[東京을](u)` keeps the default, while a space
after `을` supplies evidence. The engine never connects the particle across
separate inline tokens.

Dictionary-wide homophone detection indexes context-free defaults and queries
the emitted reading. A selected alternate may be marked as a homophone of
another key's default, while unused alternatives alone do not create marks.
Context-local detection compares the readings actually emitted in the active
window.

If the document ends right after a root inline close such as `<b>東京을</b>`,
EOF still supplies evidence. A later token or explicit `flush()` treats that
boundary as uncertain.

## Cooperating sources

The default `SourcePolicy::ReferenceAuthorized` policy keeps reference and
loanword candidate sets separate. A reference candidate authorizes a foreign
form only when the source dictionary explicitly records the relevant synonym or
formal redirect. The offline compiler intersects the permissions of every
contributing sense; a shared hanja key or reading alone grants no permission.
Missing evidence retains the reference form. Thus `東扶餘` reads `동부여`,
while the reference entry for `香港` authorizes `홍콩`.

The resolver ranks complete forms before applying particle evidence once to the
whole pool. An independent loanword can still match a particle even when it is
not the default: `平` reads `평`, but `平를` can select `다이라를`. Dictionary
preferences apply within each source; they are not comparable across sources.
The lattice objective is unchanged.

| Policy                              | Source behavior                                                                          |
| ----------------------------------- | ---------------------------------------------------------------------------------------- |
| `SourcePolicy::ReferenceAuthorized` | Authorized loanword forms, reference redirect/original forms, then independent loanwords |
| `SourcePolicy::ReferenceFirst`      | Original reference forms before independent loanwords                                    |
| `SourcePolicy::LoanwordFirst`       | Independent loanwords before original reference forms                                    |
| `SourcePolicy::Ordered`             | Legacy dictionary order; ignores roles and authorization                                 |

An authored override is exclusive for the same spelling and consumed length
under every cooperating policy. It does not remove a longer dictionary match.
For cooperating policies, `Builder::reference_only(true)` removes independent
loanwords from eligibility but retains reference-authorized forms, including
forms supplied by a loanword dictionary.

Selected forms carry their own lexical identity, alias, positional form and
annotation marks. First-occurrence handling uses the reference anchor when a
reference authorizes a foreign form; homophone handling compares the reading
actually emitted. Cooperative source-policy selections keep their existing
annotation grouping. Plain dictionary views and `SourcePolicy::Ordered` chains
keep legacy grouping, regardless of phrase metadata. In those legacy views,
only an edge selected by a successful phrase proof keeps a separate annotation
boundary; metadata alone does not change grouping. Lattice edge boundaries and
scores remain unchanged.

Use `Builder::source_policy()` to select a policy:

```rust
use gukhanmun::{Builder, Preset, SourcePolicy, SourceRole};

let converter = Builder::with_preset(Preset::KoKr)
    .source_policy(SourcePolicy::ReferenceAuthorized)
    .reference_only(true)
    .build()?;
```

`push_dictionary()` and `push_boxed_dictionary()` always register authored
overrides. To add a cooperating source, use
`push_dictionary_with_role(dictionary, SourceRole::Reference)` or
`SourceRole::Loanword`. Direct `ChainDictionary::new()` and
`ChainDictionary::from_iter()` calls retain `SourcePolicy::Ordered`; opt in
with `with_source_policy()` and `push_with_role()` when composing the chain
yourself.

With lattice segmentation, overlapping paths can remain unresolved beyond the
maximum dictionary word length. Streaming retains their shared uncertainty until
a word boundary or EOF settles the path, while already settled prefixes can
still be emitted. Dictionary roles do not change the lattice scoring objective.

## Source phrase constraints

The bundled _Standard Korean Language Dictionary_ includes source phrases that
constrain readings after segmentation. With that evidence, `無條件降服`,
`無條件 降服` and `무조건 降服` can select `항복` for the existing `降服` edge.
A verified word-to-subheadword relationship can extend the same rule to
`降服하다`. Bare `降服` keeps its legacy reading selection. The resolver does
not add whole phrases to the dictionary or change lattice scores and edge
boundaries.

Each chunk matches its complete hanja spelling or attested hangul reading.
A source caret permits no space or one `U+0020` space between chunks, so two
spaces, a tab or a newline do not match. A dictionary target must cover a whole
selected edge. Literal hangul can span adjacent unchanged text segments, with
outer word boundaries, but cannot consume dictionary or fallback edges.

Rules belong to the dictionary leaf that supplies them and apply only to its
eligible source anchors. User overrides remain authoritative, and nested
chains retain their complete eligibility restrictions for phrase proof. A rule
checks both the original anchor and its attested initial/suffix output pair. A
policy-promoted form must satisfy that pair too. The chosen reading keeps its
complete source identity and marks; readings outside the phrase keep their
usual policy defaults.

Rules from different leaf sources can agree when their directly witnessed
choices have the same complete form, lexical identity, alias and marks. After
intersecting those agreements, the resolver selects only currently eligible
choices directly witnessed by at least one applicable rule. An unwitnessed
duplicate gains no phrase authority from semantic agreement alone.

For `Ordered` and cooperating policies alike, the engine preserves legacy
parent selection over each child's public canonical projection, including
particle evidence. The separate leaf proof pool may be wider or different.
A nested fallback may be an opaque projection at the first slot of the child's
`SourceId` range, with an explicit diagnostic. Such a projection establishes no
leaf identity or `WordId` witness and supplies no phrase authority. Only
eligible complete choices from the ranked leaf pool contribute proof; on
success, the resolver replaces the whole choice atomically. `SelectedReading`
can therefore retain a fallback outside its proof pool.

For a small in-memory dictionary, attach validated `PhraseEvidence` after
inserting its candidates. This example records one phrase in an
application-owned source namespace:

```rust
use gukhanmun::{
    MapDictionary, MatchMark, PhraseCandidateLink, PhraseChunk, PhraseEvidence,
    PhraseRule, PhraseSpelling, ReadingCandidate, ReadingCandidates, RenderMode, WordId,
    convert_plain_text,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut dictionary = MapDictionary::new();
    dictionary.insert("無條件", "무조건");
    let readings = [("강복", 10), ("항복", 0)].map(|(reading, preference)| {
        ReadingCandidate {
            reading: reading.into(), suffix_reading: None,
            mark: MatchMark::default(), preference, alias: None,
        }
    });
    dictionary.insert_candidates("降服", ReadingCandidates::new(readings.to_vec())?);
    let chunk = |key: &str, reading: &str| PhraseChunk::new(
        reading.into(),
        vec![PhraseSpelling::new(
            key.into(),
            vec![PhraseCandidateLink::new(reading.into(), None)],
        )],
    );
    let rule = PhraseRule::new(
        vec![WordId(1)],
        vec![chunk("無條件", "무조건"), chunk("降服", "항복")],
    );
    dictionary.set_phrase_evidence(PhraseEvidence::new(vec![rule]))?;
    assert_eq!(
        convert_plain_text("무조건 降服", &dictionary, RenderMode::HangulOnly),
        "무조건 항복",
    );
    Ok(())
}
```

The setter checks structure and exact links to the dictionary's candidates;
the caller must verify the underlying source relationships. Later candidate
edits invalidate attached phrase evidence. FST/CDB files embed this information
in critical metadata, so their usual loaders require no separate evidence file.

`MapDictionary`, FST and CDB compile an immutable `PhraseIndex` once when
attaching or loading validated evidence. Engines and bundled wrappers borrow
that index through the optional `HanjaDictionary::phrase_index()` capability.
Custom implementations can omit the cache; an engine then builds its own index
once for its lifetime.

See the
[dictionary format](https://canary.gukhanmun.org/internals/dictionary-format.md#critical-phrase-evidence)
for the build contract.

Valid phrase restrictions and particle compatibility are intersected for each
edge. Choices with the same form, lexical identity, alias and marks count as
one result; the resolver keeps the first in rank order. When the intersection
is unresolved or conflicting, or a matcher limit is reached, the resolver
keeps the legacy particle-aware choice. Other edges remain independent.
The low-level engine's
[resolution report](https://canary.gukhanmun.org/guide/rust/streaming.md#inspecting-phrase-decisions) exposes the
rules, remaining choices and chosen metadata at the resolution step. There is
no strict ambiguity-error mode.
