Demo entry 6338642

code

   

Submitted by anonymous on Dec 15, 2016 at 21:59
Language: Python 3. Code size: 543 Bytes.

def contextwin(l, win):
    '''
    win :: int corresponding to the size of the window
    given a list of indexes composing a sentence

    l :: array containing the word indexes

    it will return a list of list of indexes corresponding
    to context windows surrounding each word in the sentence
    '''
    assert (win % 2) == 1
    assert win >= 1
    l = list(l)

    lpadded = win // 2 * [-1] + l + win // 2 * [-1]
    out = [lpadded[i:(i + win)] for i in range(len(l))]

    assert len(out) == len(l)
    return out

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).