def train_bpe(word_counts: dict[str, int],
n_merges: int) -> list[tuple[str, str]]:
words = {tuple(w): c for w, c in word_counts.items()}
merges = []
for _ in range(n_merges):
pairs = count_pairs(words) # weighted pair counts
if not pairs:
break
best = pairs.most_common(1)[0][0] # the most frequent pair
merges.append(best)
words = {merge(s, best): c for s, c in words.items()}
return merges
toy = {"cat": 8, "cats": 4, "hat": 5, "that": 6, "chat": 3}
print(train_bpe(toy, n_merges=4))[('a', 't'), ('h', 'at'), ('c', 'at'), ('t', 'hat')]