File size: 14,087 Bytes
cf9a7c2 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
from collections import Counter, defaultdict
from tqdm import tqdm
from transformers import AutoTokenizer
from pathlib import Path
import json
import pickle
import os
import re
from transformers.tokenization_utils_base import BatchEncoding
import torch
class SKMorfoTokenizer:
def __init__(self):
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
self.dictionary = None
self.roots = None
self.vocab_MDBSNK = None
self.important_vocab_MDBSNK = None
self.vocab = None
self.merges = None
self.reverse_vocab = None
self.load_suplementary_files()
def load_suplementary_files(self):
current_dir = os.path.dirname(__file__) # Adresár, kde sa nachádza tento súbor
root_file = os.path.join(current_dir, 'word_root_20231210_sorted')
vocab_file = os.path.join(current_dir, 'slova_MDBSNK')
important_vocab_file = os.path.join(current_dir, 'dolezite_slova_MDBSNK')
dictionary_file = os.path.join(current_dir, 'kodovanie.json')
vocab_json_file = os.path.join(current_dir, 'tokenizers/SKMT_BPE/vocab.json')
merges_txt_file = os.path.join(current_dir, 'tokenizers/SKMT_BPE/merges.txt')
with open(root_file, 'rb') as f:
self.roots = pickle.load(f)
with open(vocab_file, 'rb') as f:
self.vocab_MDBSNK = pickle.load(f)
with open(important_vocab_file, 'rb') as f:
self.important_vocab_MDBSNK = pickle.load(f)
self.important_vocab_MDBSNK = set(self.important_vocab_MDBSNK)
with open(dictionary_file, "r", encoding="utf-8") as f:
self.dictionary = json.load(f)
try:
with open(vocab_json_file, "r", encoding="utf-8") as file:
loaded_vocab = json.load(file)
self.vocab = {prvok: index for prvok, index in loaded_vocab.items()}
self.reverse_vocab = {v: k for k, v in self.vocab.items()}
except FileNotFoundError:
print("Súbor s vocab neexistuje.")
try:
with open(merges_txt_file, "r", encoding="utf-8") as file:
loaded_merges = [tuple(line.split()) for line in file]
self.merges = {pair: pair[0]+pair[1] for pair in loaded_merges}
except FileNotFoundError:
print("Súbor s merges neexistuje.")
def decode(self, token):
for k, v in self.dictionary.items():
if k in token:
token = token.replace(k, v)
return token
def split_word(self, text):
"""Tu sa rozdeluje slovo na znaky a korene, ak korene existujú pre dané slovo"""
pattern = re.compile(r'§{([^}]+)}§|([^§{}]+)')
result = []
for match in pattern.finditer(text):
inside_brackets, outside_brackets = match.groups()
if inside_brackets is not None:
result.append((inside_brackets, 1))
if outside_brackets is not None:
result.append((outside_brackets, 0))
def replace_letters(string):
for key, value in self.dictionary.items():
string = re.sub(re.escape(value), key, string)
return string
result = [(replace_letters(s), n) for s, n in result]
new_list = []
for text, flag in result:
if flag == 0:
new_list.extend((char) for char in text)
elif flag == 1:
new_list.append((text))
return new_list
def valid_word(self, word):
decoded = self.decode(word)
if decoded.startswith("Ġ"):
decoded = decoded[1:]
if decoded[0].lower() in self.vocab_MDBSNK:
if decoded in self.vocab_MDBSNK[decoded[0].lower()]:
return True
return False
def all_words_spaces(self, word_freqs):
def is_valid_word(word):
special_chars = "jžxďqitürpľuknŕemfšřýťhzčäwáécóösyoĺěvôdlňabígú"
pattern = f"^[a-z{special_chars}]+$"
return re.search(pattern, word) is not None
def decode(token):
for k, v in self.dictionary.items():
if k in token:
token = token.replace(k, v)
return token
unified_word_freqs = {}
for word, freq in word_freqs.items():
if word[0] == 'Ġ':
if is_valid_word(decode(word[1:])):
if unified_word_freqs.get(word, 0) == 0:
pokus = word_freqs.get(word[1:], 0)
unified_word_freqs[word] = pokus + freq
else:
unified_word_freqs[word] = freq
else:
if is_valid_word(decode(word)):
if unified_word_freqs.get("Ġ"+word, 0) == 0:
pokus = word_freqs.get("Ġ"+word, 0)
unified_word_freqs["Ġ"+word] = pokus + freq
else:
unified_word_freqs[word] = freq
return unified_word_freqs
def all_words_spaces_tokenize(self, tokenized_text):
def is_valid_word(word):
special_chars = "jžxďqitürpľuknŕemfšřýťhzčäwáécóösyoĺěvôdlňabígú"
pattern = f"^[a-z{special_chars}]+$"
return re.search(pattern, word) is not None
def decode(token):
for k, v in self.dictionary.items():
if k in token:
token = token.replace(k, v)
return token
unified_tokenized_text = []
for word in tokenized_text:
if word[0] == 'Ġ':
unified_tokenized_text.append(word)
else:
if is_valid_word(decode(word)):
unified_tokenized_text.append("Ġ"+word)
else:
unified_tokenized_text.append(word)
return unified_tokenized_text
def tokenize_half(self, text):
pre_tokenize_result = self.tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text)
pre_tokenized_text = [word for word, offset in pre_tokenize_result]
pre_tokenized_text = self.all_words_spaces_tokenize(pre_tokenized_text)
splits = {}
# Use tqdm to create a progress bar for the loop
for word in pre_tokenized_text:
decoded = self.decode(word)
try:
if decoded.startswith("Ġ"):
decoded = decoded[1:]
rooted = self.roots[decoded]
splits[word] = ["Ġ"] + self.split_word(rooted)
else:
rooted = roots[decoded]
splits[word] = self.split_word(rooted)
except:
splits[word] = list(word)
for pair, merge in self.merges.items():
for idx, split in splits.items():
i = 0
while i < len(split) - 1:
if split[i] == pair[0] and split[i + 1] == pair[1]:
split = split[:i] + [merge] + split[i + 2 :]
else:
i += 1
splits[idx] = split
zoznam = []
for slovo in pre_tokenized_text:
if slovo in splits:
zoznam.extend(splits[slovo])
return zoznam
def tokenize_additionally(self, word):
split = list(word)
for pair, merge in self.merges.items():
i = 0
while i < len(split) - 1:
if split[i] == pair[0] and split[i + 1] == pair[1]:
split = split[:i] + [merge] + split[i + 2 :]
else:
i += 1
return split
def tokenize(self, text, max_length=None, return_tensors=None, return_subword=False):
casti = text.lower().split("<mask>", 1)
if len(casti) == 1:
zoznam = self.tokenize_half(text)
else:
zoznam = self.tokenize_half(casti[0].strip()) + ["<mask>"] + self.tokenize_half(casti[1])
# Upraviť input_ids a attention_mask na základe max_length
if max_length == None:
return [prvok if prvok in self.vocab else "<unk>" for prvok in zoznam]
# Ak sa token nenachádza v vocab, tak mu priradíme UNK idčko = 3
input_ids = []
for prvok in zoznam:
if prvok in self.vocab:
input_ids.append(self.vocab[prvok])
else:
try:
prvky_add = self.tokenize_additionally(prvok)
for prvok_add in prvky_add:
if prvok_add in self.vocab:
input_ids.append(self.vocab[prvok_add])
else:
input_ids.append(self.vocab["<unk>"])
except Exception as e:
input_ids.append(self.vocab["<unk>"])
if len(input_ids) >= max_length - 2:
input_ids = input_ids[:max_length - 2]
attention_mask = [1] * (max_length - 2)
input_ids = [self.vocab["<s>"]] + input_ids + [self.vocab["</s>"]]
attention_mask = [1] + attention_mask + [1]
else:
padding_length = max_length - len(input_ids) - 2
input_ids = [self.vocab["<s>"]] + input_ids + [self.vocab["</s>"]]
attention_mask = [1] * len(input_ids)
input_ids += [self.vocab["<pad>"]] * padding_length
attention_mask += [0] * padding_length
# Zmena tu - Zabalíme výsledné tenzory do zoznamu jedného prvku
output = {"input_ids": [input_ids], "attention_mask": [attention_mask]}
if return_tensors == "pt":
output = {key: torch.tensor(val) for key, val in output.items()}
if return_subword:
tokens = [self.reverse_vocab[idx] for idx in input_ids]
return tokens
return BatchEncoding(output)
def tokenizeQA(self, text1, text2, max_length=None, return_tensors=None, return_subword=False):
zoznam1 = self.tokenize_half(text1.lower().strip())
zoznam2 = self.tokenize_half(text2.lower().strip())
# Ak sa token nenachádza v vocab, tak mu priradíme UNK idčko = 3
input_ids1 = []
for prvok in zoznam1:
if prvok in self.vocab:
input_ids1.append(self.vocab[prvok])
else:
# print(f"Nemáme token pre: {prvok}")
try:
prvky_add = self.tokenize_additionally(prvok)
for prvok_add in prvky_add:
if prvok_add in self.vocab:
input_ids1.append(self.vocab[prvok_add])
else:
input_ids1.append(self.vocab["<unk>"])
except Exception as e:
print(f"Chyba pri spracovaní prvku {prvok}: {e}")
input_ids1.append(self.vocab["<unk>"])
# Ak sa token nenachádza v vocab, tak mu priradíme UNK idčko = 3
input_ids2 = []
for prvok in zoznam2:
if prvok in self.vocab:
input_ids2.append(self.vocab[prvok])
else:
# print(f"Nemáme token pre: {prvok}")
try:
prvky_add = self.tokenize_additionally(prvok)
for prvok_add in prvky_add:
if prvok_add in self.vocab:
input_ids2.append(self.vocab[prvok_add])
else:
input_ids2.append(self.vocab["<unk>"])
except Exception as e:
print(f"Chyba pri spracovaní prvku {prvok}: {e}")
input_ids2.append(self.vocab["<unk>"])
total_length = len(input_ids1) + len(input_ids2)
if total_length >= max_length - 4:
excess_length = total_length - (max_length - 4)
while excess_length > 0:
if len(input_ids1) >= len(input_ids2):
input_ids1 = input_ids1[:-1]
else:
input_ids2 = input_ids2[:-1]
excess_length -= 1
input_ids1 = [self.vocab["<s>"]] + input_ids1 + [self.vocab["</s>"]]
input_ids2 = [self.vocab["</s>"]] + input_ids2 + [self.vocab["</s>"]]
input_ids = input_ids1 + input_ids2
if len(input_ids) >= max_length:
input_ids = input_ids[:max_length]
attention_mask = [1] * (max_length)
else:
padding_length = max_length - len(input_ids)
attention_mask = [1] * len(input_ids)
input_ids += [self.vocab["<pad>"]] * padding_length
attention_mask += [0] * padding_length
# Zmena tu - Zabalíme výsledné tenzory do zoznamu jedného prvku
output = {"input_ids": [input_ids], "attention_mask": [attention_mask]}
if return_tensors == "pt":
output = {key: torch.tensor(val) for key, val in output.items()}
if return_subword:
tokens = [self.reverse_vocab[idx] for idx in input_ids]
return tokens
return BatchEncoding(output)
def convert_ids_to_tokens(self, input_id):
return self.decode(self.reverse_vocab[input_id])
def convert_list_ids_to_tokens(self, input_ids):
tokens = []
for input_id in input_ids:
tokens.append(self.decode(self.reverse_vocab[input_id.item() if isinstance(input_id, torch.Tensor) else input_id]))
return tokens
def convert_tokens_to_ids(self, token):
return self.vocab[token]
def convert_list_tokens_to_ids(self, tokens):
ids = []
for token in tokens:
ids.append(self.vocab[token])
return ids
|