0
0
NLPml~10 mins

Context window handling in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to split text into chunks of fixed size for context window processing.

NLP
def split_text(text, chunk_size):
    return [text[i:i+[1]] for i in range(0, len(text), chunk_size)]
Drag options to blanks, or click blank then click option'
Atext
Blen(text)
Cchunk_size
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using the entire text length instead of chunk size.
Using the loop variable i as the slice size.
2fill in blank
medium

Complete the code to create overlapping context windows with a given stride.

NLP
def sliding_windows(text, window_size, stride):
    return [text[i:i+window_size] for i in range(0, len(text) - window_size + 1, [1])]
Drag options to blanks, or click blank then click option'
Awindow_size
Blen(text)
Ci
Dstride
Attempts:
3 left
💡 Hint
Common Mistakes
Using window size as the step, which causes no overlap.
Using the loop variable i as the step size.
3fill in blank
hard

Fix the error in the code that tries to pad a context window to a fixed size.

NLP
def pad_window(window, fixed_size, pad_token):
    if len(window) < fixed_size:
        window += [1] * (fixed_size - len(window))
    return window
Drag options to blanks, or click blank then click option'
Awindow
Bpad_token
Cfixed_size
Dlen(window)
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to add the window to itself instead of padding.
Using fixed_size or length as the padding token.
4fill in blank
hard

Fill both blanks to create a dictionary mapping each chunk to its start index in the text.

NLP
def chunk_indices(text, chunk_size):
    return {text[i:i+[1]]: i for i in range(0, len(text), [2])}
Drag options to blanks, or click blank then click option'
Achunk_size
Blen(text)
Cchunk_size // 2
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using different values for slice length and step size causing errors.
Using half chunk size for step causing overlapping.
5fill in blank
hard

Fill all three blanks to filter chunks longer than min_length and map them to their lengths.

NLP
def filter_chunks(chunks, min_length):
    return {chunk: len(chunk) for chunk in chunks if len(chunk) [1] [2] and chunk [3] ''}
Drag options to blanks, or click blank then click option'
A>
Bmin_length
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != to check for non-empty strings.
Using < instead of > for length comparison.