Corpus counting¶
trimbed.counting
¶
Counting how often each token id occurs across one or more Hugging Face datasets.
CorpusCounts
dataclass
¶
CorpusCounts(
counts: Counter[int] = Counter(),
total_num_tokens: int = 0,
num_documents: int = 0,
samples: list[str] = list(),
)
Token-frequency statistics gathered over a corpus.
Attributes:
| Name | Type | Description |
|---|---|---|
counts |
Counter[int]
|
Occurrences per token id, weighted per dataset, e.g.
|
total_num_tokens |
int
|
Sum of all counts. |
num_documents |
int
|
Number of examples read. |
samples |
list[str]
|
A reservoir sample of raw texts, reused to verify the trimmed tokenizer. |
distinct_tokens
property
¶
Return how many distinct token ids the corpus used at least once.
coverage_of
¶
Return the fraction of corpus occurrences covered by a set of ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
Iterable[int]
|
The ids that survive the trim. |
required |
Returns:
| Type | Description |
|---|---|
float
|
A value in |
ranked_ids
¶
Return token ids sorted by descending frequency, ties broken by id to ensure determinism.
to_dict
¶
Return a JSON-serialisable representation.
The counter's integer keys become strings, since JSON has no integer keys, e.g.
{"counts": {"13": 88317, "409": 91204}, "total_num_tokens": ..., "num_documents": ...,
"samples": [...]}. from_dict converts them back.
from_dict
classmethod
¶
Rebuild counts from to_dict output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
The decoded JSON mapping. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The reconstructed counts. |
save
¶
Write the counts to a JSON file, creating parent directories as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Destination file. |
required |
load_from_file
classmethod
¶
Read counts back from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Source file written by |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The reconstructed counts. |
View source on GitHub: src/trimbed/counting.py lines 108–118
CorpusCounter
¶
CorpusCounter(
tokenizer: PreTrainedTokenizerFast,
config: CorpusConfig,
*,
seed: int = 0,
sample_size: int = 256
)
Tokenizes configured datasets and accumulates per-id frequencies.
Build a counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
PreTrainedTokenizerFast
|
A fast tokenizer used to encode the corpus. |
required |
config
|
CorpusConfig
|
A CorpusConfig which specifies the datasets to read and how. |
required |
seed
|
int
|
Seed for the verification-sample reservoir. |
0
|
sample_size
|
int
|
How many raw texts to retain for later verification, e.g. 256, to verify the tokenizer and the model comparison then uses only the first handful. |
256
|
View source on GitHub: src/trimbed/counting.py lines 124–139
count
¶
count() -> CorpusCounts
Count token occurrences across every configured dataset.
Reads from CorpusConfig.counts_cache
when that file exists, and writes to it otherwise, so an expensive pass can be
reused across trimming runs.
Returns:
| Type | Description |
|---|---|
CorpusCounts
|
The accumulated statistics. |
View source on GitHub: src/trimbed/counting.py lines 141–165