|
from datasets import load_dataset, concatenate_datasets |
|
from tokenizers import trainers, Tokenizer, normalizers, ByteLevelBPETokenizer |
|
|
|
model_dir = "./scandinavian" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
danish_dataset = load_dataset("mc4", "da") |
|
norwegian_dataset = load_dataset("mc4", "no") |
|
swedish_dataset = load_dataset("mc4", "sv") |
|
|
|
|
|
all_datasets = concatenate_datasets([danish_dataset, norwegian_dataset, swedish_dataset]) |
|
all_datasets = all_datasets.shuffle() |
|
|
|
|
|
tokenizer = ByteLevelBPETokenizer() |
|
|
|
|
|
def batch_iterator(batch_size=1000): |
|
for i in range(0, len(all_datasets), batch_size): |
|
yield all_datasets[i : i + batch_size]["text"] |
|
|
|
|
|
|
|
tokenizer.train_from_iterator( |
|
batch_iterator(), |
|
vocab_size=50265, |
|
min_frequency=2, |
|
special_tokens=[ |
|
"<s>", |
|
"<pad>", |
|
"</s>", |
|
"<unk>", |
|
"<mask>", |
|
], |
|
) |
|
|
|
|
|
tokenizer.save(f"{model_dir}/tokenizer.json") |
|
|