Spaces:
Running on Zero
Running on Zero
Add inference source
Browse files- backend/data/vocab.py +20 -0
backend/data/vocab.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Training-split token vocabulary construction without pretrained embeddings."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from collections.abc import Iterable, Sequence
|
| 6 |
+
from typing import Final
|
| 7 |
+
|
| 8 |
+
# These IDs are a local batching convention, not a requirement imposed by File 00.
|
| 9 |
+
PAD_TOKEN: Final[str] = "<PAD>"
|
| 10 |
+
UNK_TOKEN: Final[str] = "<UNK>"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def build_vocab(train_tokens: Iterable[Sequence[str]]) -> dict[str, int]:
|
| 14 |
+
"""Assign token IDs from the training split only, preserving first-seen order."""
|
| 15 |
+
vocabulary: dict[str, int] = {PAD_TOKEN: 0, UNK_TOKEN: 1}
|
| 16 |
+
for sentence in train_tokens:
|
| 17 |
+
for token in sentence:
|
| 18 |
+
if token not in vocabulary:
|
| 19 |
+
vocabulary[token] = len(vocabulary)
|
| 20 |
+
return vocabulary
|