Skip to content

Data Mappers

datatypes

This module defines the kotobase datatypes representing information from the different data sources.

map_jmdict(entry)

Map Raw JMDictEntry database row to a Python DTO.

Parameters:

Name Type Description Default
entry JMDictEntry

SQLAlchemy Table object

required

Returns:

Name Type Description
JMDictEntryDTO JMDictEntryDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def map_jmdict(entry: orm.JMDictEntry) -> JMDictEntryDTO:
    """
    Map Raw JMDictEntry database row to a Python DTO.

    Args:
      entry (JMDictEntry): SQLAlchemy Table object

    Returns:
      JMDictEntryDTO: Python DTO

    """
    return JMDictEntryDTO(
        id=entry.id,
        rank=entry.rank,
        kana=[k.text for k in entry.kana],
        kanji=[k.text for k in entry.kanji],
        senses=[
            {
                "order": s.order,
                "pos":   s.pos,
                "gloss": s.gloss,
            }
            for s in entry.senses
        ],
    )

map_jmnedict(entry)

Map Raw JMNeDictEntry database row to a Python DTO.

Parameters:

Name Type Description Default
entry JMneDictEntry

SQLAlchemy Table object

required

Returns:

Name Type Description
JMNeDictEntryDTO JMNeDictEntryDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
def map_jmnedict(entry: orm.JMnedictEntry) -> JMNeDictEntryDTO:
    """
    Map Raw JMNeDictEntry database row to a Python DTO.

    Args:
      entry (JMneDictEntry): SQLAlchemy Table object

    Returns:
      JMNeDictEntryDTO: Python DTO

    """
    return JMNeDictEntryDTO(
        id=entry.id,
        kana=_split_list(entry.kana,  sep=";"),
        kanji=_split_list(entry.kanji, sep=";"),
        translation_type=entry.translation_type,
        gloss=_split_list(entry.translation, sep=";"),
    )

map_jlpt_vocab(row)

Map Raw JLPT Vocabulary database row to a Python DTO.

Parameters:

Name Type Description Default
entry JlptVocab

SQLAlchemy Table object

required

Returns:

Name Type Description
JLPTVocabDTO JLPTVocabDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def map_jlpt_vocab(row: orm.JlptVocab) -> JLPTVocabDTO:
    """
    Map Raw JLPT Vocabulary database row to a Python DTO.

    Args:
      entry (JlptVocab): SQLAlchemy Table object

    Returns:
      JLPTVocabDTO: Python DTO

    """
    return JLPTVocabDTO(
        id=row.id,
        level=row.level,
        kanji=row.kanji,
        hiragana=row.hiragana,
        english=row.english,
    )

map_jlpt_kanji(row)

Map Raw JLPT Kanji database row to a Python DTO.

Parameters:

Name Type Description Default
entry JlptKanji

SQLAlchemy Table object

required

Returns:

Name Type Description
JLPTKanjiDTO JLPTKanjiDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
def map_jlpt_kanji(row: orm.JlptKanji) -> JLPTKanjiDTO:
    """
    Map Raw JLPT Kanji database row to a Python DTO.

    Args:
      entry (JlptKanji): SQLAlchemy Table object

    Returns:
      JLPTKanjiDTO: Python DTO

    """
    return JLPTKanjiDTO(
        id=row.id,
        level=row.level,
        kanji=row.kanji,
    )

map_jlpt_grammar(row)

Map Raw JLPT Grammar database row to a Python DTO.

Parameters:

Name Type Description Default
entry JlptGrammar

SQLAlchemy Table object

required

Returns:

Name Type Description
JLPTGrammarDTO JLPTGrammarDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
def map_jlpt_grammar(row: orm.JlptGrammar) -> JLPTGrammarDTO:
    """
    Map Raw JLPT Grammar database row to a Python DTO.

    Args:
      entry (JlptGrammar): SQLAlchemy Table object

    Returns:
      JLPTGrammarDTO: Python DTO

    """
    return JLPTGrammarDTO(
        id=row.id,
        level=row.level,
        grammar=row.grammar,
        formation=row.formation,
        examples=_split_list(row.examples, sep="|"),
    )

map_kanjidic(row, *, jlpt_tanos_level=None)

Map Raw KANJIDIC2 database row to a Python DTO.

Parameters:

Name Type Description Default
row Kanjidic

SQLAlchemy Table object

required
jlpt_tanos_level int

JLPT level extracted from Tanos lists if it exists.

None

Returns:

Name Type Description
KanjiDTO KanjiDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
def map_kanjidic(
    row: orm.Kanjidic,
    *,
    jlpt_tanos_level: Optional[int] = None,
) -> KanjiDTO:
    """
    Map Raw KANJIDIC2 database row to a Python DTO.

    Args:
      row (Kanjidic): SQLAlchemy Table object
      jlpt_tanos_level (int, optional): JLPT level extracted from Tanos lists
                                        if it exists.

    Returns:
      KanjiDTO: Python DTO
    """
    return KanjiDTO(
        literal=row.literal,
        grade=row.grade,
        stroke_count=row.stroke_count,
        meanings=_split_list(row.meanings, sep=","),
        onyomi=_split_list(row.on_readings, sep=","),
        kunyomi=_split_list(row.kun_readings, sep=","),
        jlpt_kanjidic=row.jlpt,
        jlpt_tanos=jlpt_tanos_level,
    )

map_sentence(row)

Map Raw Tatoeba exmaple sentences database row to a Python DTO.

Parameters:

Name Type Description Default
entry TatoebaSentence

SQLAlchemy Table object

required

Returns:

Name Type Description
SentenceDTO SentenceDTO

Python DTO

Source code in kotobase/src/kotobase/core/datatypes.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
def map_sentence(row: orm.TatoebaSentence) -> SentenceDTO:
    """
    Map Raw Tatoeba exmaple sentences database row to a Python DTO.

    Args:
      entry (TatoebaSentence): SQLAlchemy Table object

    Returns:
      SentenceDTO: Python DTO
    """
    return SentenceDTO(
        id=row.id,
        text=row.text,
    )

map_many(func, rows)

Apply any single-row mapper across any iterable while keeping return order.

Parameters:

Name Type Description Default
func Callable

The mapper function to apply

required
rows Iterable

The returned database rows

required

Returns:

Name Type Description
list List

List of the DTO objects returned by func

Source code in kotobase/src/kotobase/core/datatypes.py
479
480
481
482
483
484
485
486
487
488
489
490
491
492
def map_many(func: Callable,
             rows: Iterable
             ) -> List:
    """
    Apply any single-row mapper across any iterable while keeping return order.

    Args:
      func (Callable): The mapper function to apply
      rows (Iterable): The returned database rows

    Returns:
      list: List of the DTO objects returned by `func`
    """
    return [func(r) for r in rows]