Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion model/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ def merge_vocab(pair: Tuple[str, str], v_in: Dict[str, int]) -> Dict[str, int]:
p = re.compile(r'(?<!\S)' + bigram + r'(?!\S)')
for word in v_in:
w_out = p.sub(''.join(pair), word)
v_out[w_out] = v_in[word]
# Combine counts if multiple words collapse into the same token
if w_out in v_out:
v_out[w_out] += v_in[word]
else:
v_out[w_out] = v_in[word]

return v_out

Expand Down
6 changes: 6 additions & 0 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_merge_vocab():
assert 'a b' not in merged


def test_merge_vocab_accumulates_counts():
vocab = {'a b c': 2, 'ab c': 1}
merged = merge_vocab(('a', 'b'), vocab)
assert merged['ab c'] == 3


def test_save_load_roundtrip(tmp_path):
freqs = {
'a': 1,
Expand Down