Example 1
Arguments: [
[
"a",
"b",
"c"
],
[
[
"a",
"b"
]
]
]
Expected result: [
"ab",
"c"
]The only rule joins a and b into ab, leaving c.
Tokenization and attention · Medium
Problem 14 of 50 · Python and NumPy
Apply a ranked byte-pair-style merge table to a list of string tokens. merges is ordered from highest priority (rank 0) to lowest, with each rule [left,right].
At each iteration, choose the lowest-rank rule currently present in adjacent tokens. Merge every non-overlapping occurrence of that pair in a left-to-right pass by concatenating its strings. Recompute applicable ranks and repeat until no rule applies. Return the remaining strings. This is a specified token-string model, not a complete pretrained tokenizer.
def ranked_bpe(tokens, merges):Arguments: [
[
"a",
"b",
"c"
],
[
[
"a",
"b"
]
]
]
Expected result: [
"ab",
"c"
]The only rule joins a and b into ab, leaving c.
Arguments: [
[
"a",
"b",
"c"
],
[
[
"b",
"c"
],
[
"a",
"b"
]
]
]
Expected result: [
"a",
"bc"
]The b+c rule has rank 0 and wins even though a+b appears earlier.