summaryrefslogtreecommitdiff
path: root/text_recognizer/data/transforms.py
blob: f53df64803dbf295a65f5b3beeb1a4b71ef67b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Transforms for PyTorch datasets."""
from pathlib import Path
from typing import Optional, Union, Sequence

from torch import Tensor

from text_recognizer.datasets.mappings import WordPieceMapping


class WordPiece:
    """Converts EMNIST indices to Word Piece indices."""

    def __init__(
        self,
        num_features: int,
        tokens: str,
        lexicon: str,
        data_dir: Optional[Union[str, Path]] = None,
        use_words: bool = False,
        prepend_wordsep: bool = False,
        special_tokens: Sequence[str] = ("<s>", "<e>", "<p>"),
        extra_symbols: Optional[Sequence[str]] = None,
    ) -> None:
        self.mapping = WordPieceMapping(
            num_features,
            tokens,
            lexicon,
            data_dir,
            use_words,
            prepend_wordsep,
            special_tokens,
            extra_symbols,
        )

    def __call__(self, x: Tensor) -> Tensor:
        return self.mapping.emnist_to_wordpiece_indices(x)