Dictionaries
Use runtime entries for application vocabulary, or load compiled FST/CDB
sources. The official dictionaries ship as optional data distributions,
separate from the gukhanmun package. An extra installs the data, and a
function from gukhanmun.dictionaries adds it to a converter. Installing data
never changes a converter on its own, and neither preset loads it.
Use load_dictionary() to parse a source once and share it across converters.
You can mix the returned LoadedDictionary with ordinary dictionary sources.
The handle captures the dictionary's contents and source role. These converters
retain the dictionary even after you drop reference. Load a new handle to pick
up changes to a file or URL.
Install the data
Each dictionary has an extra that installs its data at exactly the same
version as gukhanmun:
The data distributions are platform-independent wheels without dependencies.
They do not import gukhanmun, so gukhanmun installed alone stays free of
official data.
Install the extras from PyPI as shown in
Installation, for example
gukhanmun[stdict,loanwords]. The installation guide has pip, uv and Poetry
commands, plus separate instructions for prereleases and local builds.
Add dictionaries to a converter
gukhanmun.dictionaries has one function per dictionary:
Each returns a FileDictionarySource holding the dictionary bytes, the format
and the source role. The four Open Korean Dictionary categories are separate
dictionaries, so add only the ones you need:
Source roles decide how dictionaries cooperate. The helpers give each source
its role, so under the default SourcePolicy.REFERENCE_AUTHORIZED putting the
loanword source before or after the reference source does not change which one
takes priority; here the reference dictionary attests the loanword reading:
Among several reference sources, such as Open Korean Dictionary categories,
registration order still decides between their competing readings. See the
API reference for
OVERRIDE sources, the other source policies and reference_only.
A converter reads the data once when it is constructed. Reuse the converter rather than calling the functions for every document.
Choose a format
Every function takes a keyword-only format, DictionaryFormat.FST by
default:
Both formats contain the same data and produce the same conversions. FST data is smaller; CDB uses a validated index for logarithmic key lookup. Install the extra of the format you use.
The CDB distributions of the Standard Korean Language Dictionary and the Open Korean Dictionary store their files gzip-compressed. The functions decompress them, so the bytes are always ready for the dictionary loader.
Read the bytes
Each function has a *_bytes() counterpart, such as stdict_bytes(), that
returns the decompressed dictionary bytes alone, for example to cache them or
to pass a different role:
The packaged files are also available through importlib.resources. Each
distribution's import package has the dictionary files and a provenance.json
that records the source snapshot and the hashes of the files:
Read this way, .gz files are still compressed.
Handle missing data
When the data distribution is not installed, the function raises
GukhanmunError with ErrorCode.DICTIONARY_LOAD. The message names the extra
that installs it:
Other formats stay usable when one is missing. If an installed distribution is
damaged, for example a file is missing or no longer matches the size and hash
recorded in provenance.json, the error suggests reinstalling it with
pip install --force-reinstall, and error.chain keeps any underlying cause.
The suggested commands use PyPI. For another package manager or a local build, follow Installation.
Runtime entries
Use MapDictionarySource for application vocabulary. Its entry sequence keeps
repeated hanja forms as competing readings, rather than collapsing them into
one mapping:
A higher preference wins by default; following Korean particles can select
a compatible candidate instead, as 東京을 shows when 을 selects 동경.
suffix_reading supplies the reading used when the word is not word-initial,
for example after a numeral. Entry-level require_hanja and require_hangul
request annotation marks, as do the separate directives.
MapDictionarySource copies its entries into an immutable tuple. Mutating the
original list later does not change a configured source or converter.
Combining source roles
Map sources default to SourceRole.OVERRIDE. A user override replaces
cooperating reference/loanword candidates for the same spelling and consumed
length; earlier override sources take priority. You can combine an override
with official sources explicitly:
The official extras must be installed for this example. The map source still overrides matching cooperative candidates even though it comes last.
source_policy controls cooperation between sources:
Under the three cooperative policies, all except ORDERED,
reference_only=True excludes independent loanword choices while retaining
reference-authorized forms. A source role alone does not create the lexical
evidence needed to authorize a form.
Compiled and remote dictionaries
FileDictionarySource accepts raw bytes or a filesystem path with an explicit
DictionaryFormat.FST or CDB. UrlDictionarySource loads an explicit HTTP(S)
URL during converter construction. A string path is never guessed to be a
URL. Both load once; conversion calls do no dictionary I/O.
Binary source roles come from an explicit role first, then role metadata
embedded in the file, then OVERRIDE. Filenames do not determine roles. See the
API reference for URL
origin restrictions, timeouts and download limits.
To compile a large custom dictionary, follow the dictionary-building guide. The resulting FST/CDB files work with the Python sources above.
Licenses
The data distributions carry the licenses of their data:
- Standard Korean Language Dictionary and Open Korean Dictionary data: Creative Commons Attribution-ShareAlike 2.0 Korea (CC BY-SA 2.0 KR).
- NIKL loanword spelling examples: the National Institute of Korean Language's free-use terms.
Each distribution includes LICENSE and ATTRIBUTION.md with the attribution and code license. Keep them when you redistribute the data.