Text
text
¶
Defines functions that use fugashi and kotobase to tokenize Japanese
sentences and build pydantic models containing relevant dictionary data
enrich(sentence, mode=BundleMode.grammar)
¶
Segments a sentence and enriches each stitched word with dictionary data
Runs one kotobase lookup per stitched word, keyed on the word's lemma.
Stitching first means a compound like 図書館 (library) is looked up as one
word and gets a real dictionary entry, instead of looking up the fragments
図書 and 館 separately (which is both slower and less useful)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence
|
str
|
The Japanese sentence to process |
required |
mode
|
BundleMode
|
How aggressively to group the tokens |
grammar
|
Returns:
| Type | Description |
|---|---|
list[EnrichedJapaneseWord]
|
A list of |
Raises:
| Type | Description |
|---|---|
FugashiError
|
If tokenisation fails |
KotobaseError
|
If a dictionary lookup fails |
ensure_fugashi()
¶
Performs a simple tokenisation operation using fugashi to ensure that
it's functional, raising an exception on any failures
Raises:
| Type | Description |
|---|---|
FugashiError
|
If any error occurs during tokenisation |
ensure_kotobase()
¶
Performs a simple lookup operation using kotobase to ensure that
it's functional, raising an exception on any failures
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If any error occurs during the lookup |
get_audio_clip(literal, clip)
¶
Fetches the raw bytes of one pronunciation clip
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal the clip belongs to |
required |
clip
|
str
|
The clip id from |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, bytes] | None
|
The clip's file name and raw bytes, or |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
get_kanji(literal)
¶
Fetches the full profile of a single Kanji
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal |
required |
Returns:
| Type | Description |
|---|---|
KanjiInfo | None
|
The flat |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
get_kanji_audio(literal)
¶
Lists the pronunciation clips available for a Kanji (Kanji Alive)
Clips
-
Kanji Aliverecords example-word pronunciations per Kanji, so each clip id names one recorded example word -
The clips list is empty when the optional audio pack is not installed or the Kanji has no clips
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal |
required |
Returns:
| Type | Description |
|---|---|
KanjiAudio
|
The |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
get_sentences_with_kanji(literal, limit=10)
¶
Lists example sentences that contain a Kanji
Backs the Kanji view's examples section
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal |
required |
limit
|
int
|
Maximum number of sentences to return |
10
|
Returns:
| Type | Description |
|---|---|
list[Example]
|
The matching sentences with their translations |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
get_stroke_svg(literal)
¶
Fetches a Kanji's stroke-order diagram as a self-contained SVG document
(KanjiVG)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The SVG document, or |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
get_words_with_kanji(literal, limit=50)
¶
Lists dictionary entries whose written form uses a Kanji
Backs the Kanji view's Words With section
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The Kanji literal |
required |
limit
|
int
|
Maximum number of entries to return |
50
|
Returns:
| Type | Description |
|---|---|
list[JMEntry]
|
The matching entries as flat |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
kanji_by_radicals(radicals, match='all')
¶
Finds Kanji that contain one, or every one of the given radicals
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radicals
|
tuple[str, ...]
|
The radical glyphs to require |
required |
match
|
str
|
When |
'all'
|
Returns:
| Type | Description |
|---|---|
list[KanjiInfo]
|
The matching Kanji as flat |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the search fails |
list_radicals()
cached
¶
Lists every search radical with its stroke count (RADKFILE)
Cached for the process lifetime, since the radical inventory is static
Returns:
| Type | Description |
|---|---|
list[RadicalInfo]
|
All search radicals |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the lookup fails |
query_kotobase(query, wildcard=False, include_names=True, sentence_limit=5, entry_limit=None, reading=None, pos=None)
cached
¶
Wraps kotobase.Kotobase.lookup to provide a lru-cache for queries and
flatten the nested lookup DTOs into the KotobaseData model
Readable Tags
The lookup runs with with_labels=True and every tag code (POS,
field, misc, name type) is expanded into its human-readable label
here, so the frontend never handles raw codes
Token-Aware Relevance
-
When the caller knows the token's
readingandpos(from fugashi), entries are re-ranked so kana-matching and part-of-speech-matching entries come first, which also drives themeaningssummary taken from the top entry -
A lookup that finds nothing retries once with the hiragana reading, so tokens whose written form is not a dictionary key still resolve
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
word or wildcard pattern to query |
required |
wildcard
|
bool
|
When |
False
|
include_names
|
bool
|
When |
True
|
sentence_limit
|
int
|
Defines how many |
5
|
entry_limit
|
int | None
|
Defines the maximum number of JMDict entries
to fetch. Fetches all entries when set to |
None
|
reading
|
str | None
|
The token's reading (any kana), when known |
None
|
pos
|
str | None
|
The token's UniDic top-level part of speech, when known |
None
|
Returns:
| Type | Description |
|---|---|
KotobaseData
|
Pydantic model containing all information extracted from |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the |
resolve_reference(ref)
¶
Resolves a sense cross-reference or antonym code into its entries
Lets the UI turn an xref or antonym string (e.g. 見る・1) into the
actual referenced dictionary entries on demand, without resolving every
reference upfront during a lookup
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
The reference code from a sense's |
required |
Returns:
| Type | Description |
|---|---|
list[JMEntry]
|
The referenced entries as flat |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the resolution fails |
search_english(query, limit=50)
¶
Searches dictionary entries by English meaning (reverse lookup)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The English search text |
required |
limit
|
int
|
Maximum number of entries to return |
50
|
Returns:
| Type | Description |
|---|---|
list[JMEntry]
|
The matching entries as flat |
Raises:
| Type | Description |
|---|---|
KotobaseError
|
If the search fails |
segment(sentence, mode=BundleMode.grammar)
¶
Tokenizes and stitches a sentence into useful words (no dictionary lookups)
Usage
-
This is the fast path used to render clickable text
-
Since it skips the (relatively slow) dictionary lookups, it is suited to tokenising whole subtitles/transcripts
-
The dictionary data is fetched later by
enrichor on a word click
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence
|
str
|
The Japanese sentence to segment |
required |
mode
|
BundleMode
|
How aggressively to group the tokens |
grammar
|
Returns:
| Type | Description |
|---|---|
list[JapaneseWord]
|
The stitched |
Raises:
| Type | Description |
|---|---|
FugashiError
|
If tokenisation fails |
segment_batch(sentences, mode=BundleMode.grammar)
¶
Segments many sentences in one call (see segment)
Used to tokenize a whole subtitle file up front in a single request, so the player never tokenizes per-cue mid-playback
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences
|
list[str]
|
The sentences to segment, in order |
required |
mode
|
BundleMode
|
How aggressively to group the tokens |
grammar
|
Returns:
| Type | Description |
|---|---|
list[list[JapaneseWord]]
|
One stitched-word list per input sentence, in the same order |
Raises:
| Type | Description |
|---|---|
FugashiError
|
If tokenisation fails |
stitch(tokens, mode=BundleMode.grammar)
¶
Stitches UniDic short-unit tokens into useful, learner-facing words
The grouping is controlled by mode (see BundleMode)
私は図書館で本を読みました (grammar mode)
Reliability
-
Splitting verbs, auxiliaries and particles follows directly from UniDic's grammatical labels, so it is essentially deterministic
-
Noun compounding is a heuristic. UniDic returns a run of nouns, not whether they form one word or several (that lives in its separate "long unit word" layer, which the short-unit output does not expose), so consecutive nouns are merged by rule and may over- or under-merge
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[Token]
|
Short-unit tokens, in order |
required |
mode
|
BundleMode
|
How aggressively to group the tokens |
grammar
|
Returns:
| Type | Description |
|---|---|
list[JapaneseWord]
|
The stitched |
tokenize(sentence)
¶
Tokenizes a Japanese sentence using fugashi and extracts
all token information into a pydantic model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence
|
str
|
Sentence to tokenize |
required |
Returns:
| Type | Description |
|---|---|
list[Token]
|
list of |
Raises:
| Type | Description |
|---|---|
FugashiError
|
If the tagger can't be initialised or tokenisation fails |