> 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 uses dictionaries to look up the hangul readings of hanja.  The
`ko-kr` preset uses the bundled _Loanword spelling examples_ and _Standard
Korean Dictionary_ (標準國語大辭典), and the `ko-kp` preset uses the bundled
_Open Korean Dictionary_ (우리말샘) North Korean category.

## Bundled dictionaries

The bundled dictionary selected by the preset is loaded automatically.  No
extra flags are needed for most Korean text.

To disable every bundled dictionary, for example when you want to rely entirely
on a custom dictionary, pass `--no-bundled-dictionaries`:

```sh
gukhanmun --no-bundled-dictionaries -d my-dict.gukfst input.txt
```

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

```sh
gukhanmun --preset ko-kp input.txt
```

## Loanword spelling examples

The `ko-kr` preset includes the National Institute of Korean Language's
_Loanword spelling examples_ dictionary by default. It supplies Korean
spellings of foreign names written in hanja, such as `香港` → `홍콩`:

```sh
echo '香港' | gukhanmun
# 홍콩
```

Use `--no-loanwords` to disable only loanword examples while keeping the
preset's other dictionaries. The `ko-kp` preset leaves loanword examples off;
pass `--loanwords` to enable them explicitly. Custom dictionaries override
bundled candidates for the same spelling and consumed length. Bundled reference
and loanword sources cooperate under `--source-policy reference-authorized`.
`--loanwords` cannot be combined with `--no-bundled-dictionaries`.

## Custom dictionaries

Supply one or more custom dictionaries with `-d` (or `--dictionary`).  The flag
can be repeated:

```sh
gukhanmun -d legal.gukfst input.txt
gukhanmun -d legal.gukfst -d names.gukcdb input.txt
```

Gukhanmun supports two binary dictionary formats:

| Format | Extension | Lookup                      | Notes                                               |
| ------ | --------- | --------------------------- | --------------------------------------------------- |
| FST    | _.gukfst_ | $O(\text\{key length})$     | Preferred for lattice segmentation; smaller on disk |
| CDB    | _.gukcdb_ | $O(\log N)$ key comparisons | Validated in-memory index; simpler on-disk layout   |

Here $N$ is the number of CDB records. Prefix matching performs a key lookup
for each prefix; CDB loading builds and validates the index before conversion.

Later `-d` arguments take priority over earlier ones and over every bundled
dictionary for matches of the same text and length. With
`--segmentation lattice` (the default), overlapping matches of different
lengths are compared by dictionary coverage, so a longer bundled match can beat
a shorter user match.

## Building a custom dictionary

The _.gukfst_ and _.gukcdb_ files are compiled artifacts, not something you
edit by hand.  You author your entries as a plain text table and compile them
with `gukhanmun-mkdict`.

The `gukhanmun-mkdict` builder is installed together with `gukhanmun` when you
[install from PyPI](https://canary.gukhanmun.org/guide/cli/install.md#from-pypi-pipx-or-uv),
[install from npm](https://canary.gukhanmun.org/guide/cli/install.md#from-npm),
[install via mise](https://canary.gukhanmun.org/guide/cli/install.md#via-mise) or
[download a prebuilt archive](https://canary.gukhanmun.org/guide/cli/install.md#prebuilt-binaries). If you instead
built from crates.io, install the builder the same way:

```sh
cargo install gukhanmun-mkdict
```

Write your entries as a tab-separated file with a `hanja` key column and a
`hangul` reading column:

```tsv
hanja	hangul
北京	베이징
學校	학교
```

Two optional columns control how renderers treat each entry: set
`require_hanja` to `true` to keep the source hanja visible (for homophones that
need disambiguation), and `require_hangul` to `true` to force a hangul gloss in
the original-script rendering mode.

```tsv
hanja	hangul	require_hanja	require_hangul
北京	베이징	false	false
色깔論	색깔론	false	true
```

Compile the table into an FST dictionary (the default format):

```sh
gukhanmun-mkdict --output legal.gukfst legal.tsv
```

Pass `--format cdb` to produce a _.gukcdb_ file instead.  You can supply
several input files, which are merged in order; `--merge` selects how duplicate
keys are resolved (`error`, `first-wins`, or `last-wins`).  Add `--validate` to
reopen the output and confirm every entry round-trips, and `--metadata KEY=VAL`
to embed provenance such as the source or license.

Then load the result like any other custom dictionary:

```sh
gukhanmun -d legal.gukfst input.txt
```

CSV and JSON Lines inputs are also accepted, and a few more advanced options
are available.  See [the internals section](https://canary.gukhanmun.org/internals/dictionary-format.md) for
the full dictionary file format specification.

## Cooperating sources

The default `reference-authorized` 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                                                                          |
| ---------------------- | ---------------------------------------------------------------------------------------- |
| `reference-authorized` | Authorized loanword forms, reference redirect/original forms, then independent loanwords |
| `reference-first`      | Original reference forms before independent loanwords                                    |
| `loanword-first`       | Independent loanwords before original reference forms                                    |
| `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, `--reference-only` 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. Source-aware dictionary matches keep separate annotation
boundaries, including matches next to fallback characters. This can change
parenthetical grouping compared with `ordered`.

Set the policy and eligibility with `--source-policy` and `--reference-only`:

```sh
gukhanmun --source-policy reference-authorized --reference-only input.txt
gukhanmun --source-policy ordered input.txt
```

`-d` always registers an authored override, even when the file embeds a
reference/loanword role. Register additional cooperating dictionaries with
`--reference-dictionary reference.gukfst` or
`--loanword-dictionary names.gukcdb`. Later `-d` values retain priority over
earlier `-d` values.

`gukhanmun-mkdict --evidence source-evidence.json` attaches compiled typed
source evidence to a dictionary. This produces a version 4 container; older
readers reject it. Evidence-free builds retain their existing container format
and have unknown source evidence. The evidence must match the final canonical
keys and complete reading/suffix pairs; stale links fail the build before an
output is created.

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.
