-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize_data.py
More file actions
66 lines (48 loc) · 1.92 KB
/
Copy pathtokenize_data.py
File metadata and controls
66 lines (48 loc) · 1.92 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
sys.path.append('tokenizer/')
import tokenizer
import sentencepiece as spm
import numpy as np
from tqdm import tqdm
import polars as pl
CORPUS_PATH = 'data/training/corpus.parquet'
OUTPUT_DIR = 'data/model_training/'
SPM_MODEL = 'data/spm/baseline_tokenizer.model'
PL_VOCAB = 'tokenizer/vocab.json'
def tokenize_with_pl(texts, output_path):
tokenizer_pl = tokenizer.PolishToKenizer()
tokenizer_pl.from_pretrained(PL_VOCAB)
output_path = output_path + 'corpus_pl.bin'
print("\nEncoding with Polish tokenizer...")
total_tokens = 0
with open(output_path, 'wb') as f:
for text in tqdm(texts, desc="PL"):
chunk = np.array(tokenizer_pl.tokens_to_ids(text), dtype=np.uint16)
chunk.tofile(f)
total_tokens += len(chunk)
print(f"Saved: {output_path} ({total_tokens:,} tokens)")
return total_tokens
def tokenize_with_spm(texts, output_path):
tokenizer_spm = spm.SentencePieceProcessor()
tokenizer_spm.Load(SPM_MODEL)
output_path = output_path + 'corpus_spm.bin'
print("\nEncoding with SentencePiece tokenizer...")
total_tokens = 0
with open(output_path, 'wb') as f:
for text in tqdm(texts, desc="SPM"):
chunk = np.array(tokenizer_spm.EncodeAsIds(text), dtype=np.uint16)
chunk.tofile(f)
total_tokens += len(chunk)
print(f"Saved: {output_path} ({total_tokens:,} tokens)")
return total_tokens
def main():
print("Loading corpus...")
texts = pl.read_parquet(CORPUS_PATH)['text'].to_list()
print(f"Loaded {len(texts):,} documents")
total_chars = sum(len(t) for t in texts)
# tokens_pl = tokenize_with_pl(texts, OUTPUT_DIR)
# print(f"\nCompression ratio PL: {total_chars / tokens_pl:.2f} chars/token")
tokens_spm = tokenize_with_spm(texts, OUTPUT_DIR)
print(f"Compression ratio SPM: {total_chars / tokens_spm:.2f} chars/token")
if __name__ == '__main__':
main()