Example 1
Arguments: [
[
1,
2,
1
],
2
]
Expected result: [
2
]The suffix [1] previously continued with 2, so token 2 is blocked.
Decoding and sampling · Medium
Problem 27 of 50 · Python and NumPy
Return the sorted unique token IDs that would create a previously seen n-gram if appended to history. Match the last n−1 tokens against prefixes of complete n-grams already in history.
For n=1, every previously seen token is blocked. If history is too short to supply either a matching suffix or a previous full n-gram, return [].
def blocked_ngram_tokens(history, n):Arguments: [
[
1,
2,
1
],
2
]
Expected result: [
2
]The suffix [1] previously continued with 2, so token 2 is blocked.
Arguments: [
[
3,
1,
3
],
1
]
Expected result: [
1,
3
]Every seen unigram is blocked; deduplication and sorting give [1,3].