Dtos
dtos
¶
Defines Kotobase's Data-Transfer-Objects
The DTOs are the boundary between the database and the public API.
Repositories return them rather than
ORM rows so that callers get plain, immutable,
serializable values that don't depend on an open session
Serialization
Every DTO is a Pydantic model, serialize it with Pydantic's own
model_dump / model_dump_json, which keep Japanese text verbatim
ORM Mapping
-
ORM-backed DTOs inherit from
SafeORMModeland are built withmodel_validate, reading attributes straight from a loaded ORM row -
A model validator replaces any relationship that wasn't eagerly loaded with
None, so validation never triggers a lazy load, and overlays values passed through the validationcontextfor fields that aren't plain ORM attributes, such as a kanji's radicals -
The
Repositorieseagerly load the relationships each DTO reads
KotobaseDTO = GlossDTO | SenseDTO | KanjiFormDTO | KanaFormDTO | JMDictEntryDTO | NameTranslationDTO | JMNeDictEntryDTO | KanjiDTO | FuriganaDTO | SentenceDTO | JLPTVocabDTO | JLPTKanjiDTO | JLPTGrammarDTO | RadicalDTO | AudioDTO | LookupResult
module-attribute
¶
Represents any one of Kotobase's data transfer objects
AudioDTO
¶
Bases: SafeORMModel
Metadata for a pronunciation audio clip
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
What the clip pronounces, such as |
key |
str
|
Lookup key for the clip |
reading |
str | None
|
The reading the clip pronounces when relevant |
fmt |
str | None
|
Audio container or codec such as |
source |
str
|
Name of the upstream source |
license |
str | None
|
License identifier for the clip |
attribution |
str | None
|
Required attribution text or link |
FuriganaDTO
¶
Bases: SafeORMModel
Furigana segmentation for a spelling and reading pair
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The written spelling |
reading |
str
|
The full kana reading |
segments |
list[dict]
|
Alignment of spelling spans to their readings |
GlossDTO
¶
Bases: SafeORMModel
A single translation of a JMdict sense
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The translated text |
lang |
str
|
ISO 639 language code of the gloss |
gender |
str | None
|
Grammatical gender when given |
gtype |
str | None
|
Gloss type such as |
JLPTGrammarDTO
¶
JLPTKanjiDTO
¶
JLPTVocabDTO
¶
Bases: SafeORMModel
A Tanos JLPT vocabulary item
Attributes:
| Name | Type | Description |
|---|---|---|
level |
int
|
JLPT level from 1 to 5 |
word |
str | None
|
The headword |
reading |
str | None
|
The kana reading |
meaning |
str | None
|
The English meaning |
JMDictEntryDTO
¶
Bases: SafeORMModel
One JMdict dictionary entry
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
The JMdict sequence number |
is_common |
bool
|
True when the entry is marked common |
freq_rank |
int | None
|
Frequency band where a lower value is more frequent |
kanji |
list[KanjiFormDTO]
|
Written forms of the entry |
kana |
list[KanaFormDTO]
|
Reading forms of the entry |
senses |
list[SenseDTO]
|
Meanings of the entry |
headword
property
¶
Returns the primary written form of the entry
Returns:
| Type | Description |
|---|---|
str
|
The first kanji form when present, otherwise the first reading |
key
property
¶
all_pos()
¶
common_kana()
¶
Return only the reading forms flagged common
Returns:
| Type | Description |
|---|---|
list[KanaFormDTO]
|
The kana forms whose |
common_kanji()
¶
Return only the written forms flagged common
Returns:
| Type | Description |
|---|---|
list[KanjiFormDTO]
|
The kanji forms whose |
JMNeDictEntryDTO
¶
Bases: SafeORMModel
One JMnedict proper name entry
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
The JMnedict sequence number |
kanji |
list[str]
|
Written forms of the name |
kana |
list[str]
|
Reading forms of the name |
translations |
list[NameTranslationDTO]
|
Translation blocks |
KanaFormDTO
¶
Bases: SafeORMModel
A reading form of a JMdict entry
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The kana reading |
is_common |
bool
|
True when the reading carries a common priority marker |
no_kanji |
bool
|
True when the reading applies to no kanji form |
restrictions |
list[str]
|
Kanji forms the reading is limited to |
info |
list[str]
|
Reading information tag codes |
priority |
list[str]
|
Priority code list |
KanjiDTO
¶
Bases: SafeORMModel
A kanji with its full KanjiDic2 and KanjiVG profile
Attributes:
| Name | Type | Description |
|---|---|---|
literal |
str
|
The kanji character |
grade |
int | None
|
School grade in which it is taught |
stroke_count |
int | None
|
Accepted stroke count |
freq |
int | None
|
Newspaper frequency rank |
jlpt_old |
int | None
|
Pre 2010 JLPT class from KanjiDic2 |
jlpt_tanos |
int | None
|
JLPT level from the Tanos lists |
onyomi |
list[str]
|
On readings |
kunyomi |
list[str]
|
Kun readings |
nanori |
list[str]
|
Name only readings |
pinyin |
list[str]
|
Mandarin pinyin readings |
korean |
list[str]
|
Korean readings |
meanings |
list[str]
|
English meanings |
radicals |
list[str]
|
Radical components of the kanji |
dic_refs |
dict[str, str]
|
Dictionary references keyed by type, such as
|
query_codes |
dict[str, list[str]]
|
Lookup codes keyed by type, such as
|
codepoints |
dict[str, str]
|
Encoding codepoints keyed by type |
variants |
list[dict[str, Any]]
|
Variant form references with their type and value |
has_stroke_order |
bool
|
True when KanjiVG stroke data is available |
key
property
¶
four_corner_codes()
¶
is_joyo()
¶
Report whether the kanji is in the Joyo set
Returns:
| Type | Description |
|---|---|
bool
|
True when the KanjiDic grade is 1 through 8 |
primary_kunyomi()
¶
Return the first kun reading
Returns:
| Type | Description |
|---|---|
str | None
|
The first kun reading, or None when there are none |
KanjiFormDTO
¶
Keyed
¶
Bases: Protocol
Interface for a DTO that carries its own lookup key
The public API accepts these DTOs anywhere a string key is expected and reads the key straight off the object, so a result from one call can be passed into another without unpacking a field by hand
LookupResult
¶
Bases: BaseModel
The aggregated result of a comprehensive word lookup
Attributes:
| Name | Type | Description |
|---|---|---|
query |
str
|
The query that produced the result |
entries |
list[JMDictEntryDTO]
|
Matching dictionary entries |
names |
list[JMNeDictEntryDTO]
|
Matching proper names |
kanji |
list[KanjiDTO]
|
Details for each kanji in the query |
furigana |
list[FuriganaDTO]
|
Furigana for the matched forms |
jlpt_vocab |
JLPTVocabDTO | None
|
JLPT vocabulary entry for the word |
jlpt_kanji_levels |
dict[str, int]
|
JLPT level per kanji in the query |
jlpt_grammar |
list[JLPTGrammarDTO]
|
JLPT grammar points matching the query |
sentences |
list[SentenceDTO]
|
Example sentences containing the query |
labels |
dict[str, str]
|
Tag code to human description map, populated only when labels are requested |
has_entries()
¶
Report whether the result carries any dictionary entries
Returns:
| Type | Description |
|---|---|
bool
|
True when at least one JMdict entry matched |
has_jlpt()
¶
Report whether the result carries any JLPT information
Returns:
| Type | Description |
|---|---|
bool
|
True when a JLPT vocabulary entry or kanji level is present |
has_kanji()
¶
Report whether the result carries any kanji details
Returns:
| Type | Description |
|---|---|
bool
|
True when at least one kanji profile is present |
NameTranslationDTO
¶
RadicalDTO
¶
Bases: SafeORMModel
A search radical and its stroke count
Attributes:
| Name | Type | Description |
|---|---|---|
radical |
str
|
The radical character |
stroke_count |
int | None
|
Number of strokes in the radical |
SafeORMModel
¶
Bases: BaseModel
Pydantic base model for ORM-backed DTOs that tolerates unloaded relationships
Validates directly from SQLAlchemy instances (from_attributes), but
first replaces any relationship field that wasn't eagerly loaded with
None instead of triggering a lazy load, and overlays any values supplied
through the validation context
check_sqlalchemy_state(data, info)
classmethod
¶
Builds a validation-safe mapping from a SQLAlchemy instance
Reads each field from its ORM attribute (honoring a field's validation
alias), maps unloaded relationships to None so that validation never
triggers a lazy load, then overlays any matching keys from the
validation context. Inputs that aren't SQLAlchemy instances are
returned unchanged
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value being validated (ORM instance or mapping) |
required |
info
|
ValidationInfo
|
Validation context carrying injected fields |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A mapping safe for Pydantic validation, or |
SenseDTO
¶
Bases: SafeORMModel
One meaning of a JMdict entry with its glosses and tags
Attributes:
| Name | Type | Description |
|---|---|---|
glosses |
list[GlossDTO]
|
The translations of the sense |
pos |
list[str]
|
Part of speech tag codes |
field |
list[str]
|
Field of application tag codes |
misc |
list[str]
|
Register tag codes such as |
dialect |
list[str]
|
Dialect tag codes |
info |
list[str]
|
Free text sense notes |
xref |
list[str]
|
Cross references to related entries |
antonym |
list[str]
|
Antonym references |
lsource |
list[dict]
|
Source language records for loanwords |
all_tags()
¶
expand_tags(labels)
¶
Resolve the sense's tag codes to human descriptions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
dict[str, str]
|
A code to description map, such as the one
on |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
One description per tag code, falling back to the raw code when it isn't present in the map |