|
6 | 6 |
|
7 | 7 | use Codewithkyrian\Tokenizers\Contracts\ModelInterface; |
8 | 8 |
|
| 9 | +/** |
| 10 | + * A minimal vocabulary-mapping model with no subword algorithm. |
| 11 | + * |
| 12 | + * Used for tokenizers where tokens map 1:1 to characters or bytes |
| 13 | + * without BPE, WordPiece, or Unigram segmentation — typically CTC |
| 14 | + * models such as Wav2Vec2. |
| 15 | + * |
| 16 | + * `tokenize()` is an identity transform; `encode()` and `decode()` |
| 17 | + * are simple dictionary lookups. |
| 18 | + */ |
9 | 19 | class FallbackModel implements ModelInterface |
10 | 20 | { |
11 | 21 | /** |
| 22 | + * Maps token strings to integer IDs. |
| 23 | + * |
| 24 | + * @var array<string, int> |
| 25 | + */ |
| 26 | + protected array $tokenToId = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Maps integer IDs back to token strings. |
| 30 | + * |
12 | 31 | * @var array<int, string> |
13 | 32 | */ |
14 | | - protected array $vocab = []; |
| 33 | + protected array $idToToken = []; |
15 | 34 |
|
16 | 35 | /** |
17 | | - * @var array<string, int> |
| 36 | + * The unknown-token string, used as a fallback when a token |
| 37 | + * or ID cannot be found in the vocabulary. |
18 | 38 | */ |
19 | | - protected array $vocabReversed = []; |
20 | 39 | protected ?string $unkToken; |
21 | 40 |
|
22 | 41 | /** |
23 | | - * @param array<int, string> $vocab the vocabulary |
24 | | - * @param null|string $unkToken the unknown token |
| 42 | + * @param array<string, int> $vocab Token → ID mapping (e.g. `['e' => 5, …]`) |
| 43 | + * @param null|string $unkToken Fallback string for unknown tokens / IDs |
25 | 44 | */ |
26 | | - public function __construct( |
27 | | - array $vocab = [], |
28 | | - ?string $unkToken = null |
29 | | - ) { |
| 45 | + public function __construct(array $vocab = [], ?string $unkToken = null) |
| 46 | + { |
| 47 | + $this->tokenToId = $vocab; |
| 48 | + $this->idToToken = array_flip($this->tokenToId); |
30 | 49 | $this->unkToken = $unkToken; |
31 | | - |
32 | | - // Populate vocab |
33 | | - foreach ($vocab as $token => $id) { |
34 | | - $this->vocab[$token] = $id; |
35 | | - $this->vocabReversed[$id] = $token; |
36 | | - } |
37 | 50 | } |
38 | 51 |
|
39 | 52 | /** |
40 | | - * @param string[] $messages the messages to tokenize |
| 53 | + * Identity transform — returns tokens unchanged. |
| 54 | + * |
| 55 | + * @param string[] $messages Input token strings |
41 | 56 | * |
42 | | - * @return string[] |
| 57 | + * @return string[] Same token strings, unchanged |
43 | 58 | */ |
44 | 59 | public function tokenize(array $messages): array |
45 | 60 | { |
46 | 61 | return $messages; |
47 | 62 | } |
48 | 63 |
|
49 | 64 | /** |
50 | | - * @param string[] $tokens the tokens to encode |
| 65 | + * Convert token strings to their integer IDs. |
51 | 66 | * |
52 | | - * @return int[] |
| 67 | + * Unknown tokens resolve to the unk-token's ID if available, |
| 68 | + * otherwise to 0. |
| 69 | + * |
| 70 | + * @param string[] $tokens Token strings to encode |
| 71 | + * |
| 72 | + * @return int[] Integer token IDs |
53 | 73 | */ |
54 | 74 | public function encode(array $tokens): array |
55 | 75 | { |
56 | | - return array_map(function ($token) { |
57 | | - return $this->vocabReversed[$token] ?? $this->vocabReversed[$this->unkToken] ?? 0; |
58 | | - }, $tokens); |
| 76 | + return array_map( |
| 77 | + fn (string $token): int => $this->tokenToId[$token] |
| 78 | + ?? $this->tokenToId[$this->unkToken] |
| 79 | + ?? 0, |
| 80 | + $tokens, |
| 81 | + ); |
59 | 82 | } |
60 | 83 |
|
61 | 84 | /** |
62 | | - * @param int[] $ids the IDs to decode |
| 85 | + * Convert integer IDs back to their token strings. |
| 86 | + * |
| 87 | + * Unknown IDs resolve to the unk-token string if available, |
| 88 | + * otherwise to an empty string. |
63 | 89 | * |
64 | | - * @return string[] |
| 90 | + * @param int[] $ids Integer token IDs to decode |
| 91 | + * |
| 92 | + * @return string[] Token strings |
65 | 93 | */ |
66 | 94 | public function decode(array $ids): array |
67 | 95 | { |
68 | | - return array_map(fn ($id) => $this->vocab[$id] ?? $this->unkToken ?? '', $ids); |
| 96 | + return array_map( |
| 97 | + fn (int $id): string => $this->idToToken[$id] |
| 98 | + ?? $this->unkToken |
| 99 | + ?? '', |
| 100 | + $ids, |
| 101 | + ); |
69 | 102 | } |
70 | 103 |
|
71 | 104 | /** |
| 105 | + * Return the full token → ID vocabulary. |
| 106 | + * |
72 | 107 | * @return array<int, string> |
73 | 108 | */ |
74 | 109 | public function getVocab(): array |
75 | 110 | { |
76 | | - return $this->vocab; |
| 111 | + return $this->idToToken; |
77 | 112 | } |
78 | 113 |
|
| 114 | + /** |
| 115 | + * Return the number of tokens in the vocabulary. |
| 116 | + */ |
79 | 117 | public function getVocabSize(): int |
80 | 118 | { |
81 | | - return \count($this->vocab); |
| 119 | + return \count($this->idToToken); |
82 | 120 | } |
83 | 121 |
|
84 | 122 | /** |
85 | | - * @param string $token the token to add |
86 | | - * @param int $id the ID of the token |
| 123 | + * Add a token or override an existing one. |
| 124 | + * |
| 125 | + * @param string $token The token string |
| 126 | + * @param int $id The integer ID to assign |
87 | 127 | */ |
88 | 128 | public function addToken(string $token, int $id): void |
89 | 129 | { |
90 | | - $this->vocab[$id] = $token; |
91 | | - $this->vocabReversed[$token] = $id; |
| 130 | + $this->tokenToId[$token] = $id; |
| 131 | + $this->idToToken[$id] = $token; |
92 | 132 | } |
93 | 133 |
|
| 134 | + /** |
| 135 | + * Return configuration, a single config key, or a default value. |
| 136 | + * |
| 137 | + * @return ($key is null ? array<string, mixed> : mixed) |
| 138 | + */ |
94 | 139 | public function getConfig(?string $key = null, mixed $default = null): mixed |
95 | 140 | { |
96 | 141 | if (null !== $key) { |
97 | 142 | return match ($key) { |
98 | | - 'vocab' => $this->vocab, |
| 143 | + 'vocab' => $this->tokenToId, |
99 | 144 | 'unk_token' => $this->unkToken, |
100 | 145 | default => $default, |
101 | 146 | }; |
102 | 147 | } |
103 | 148 |
|
104 | | - // 2. Full Config Reconstruction |
105 | 149 | return [ |
106 | | - 'vocab' => $this->vocab, |
| 150 | + 'vocab' => $this->tokenToId, |
107 | 151 | 'unk_token' => $this->unkToken, |
108 | 152 | ]; |
109 | 153 | } |
|
0 commit comments