def determine_windowsX(sentence, spans, aliases_seen_by_model, maxlen, mincontext, sanity_check=False):
    """
    Truncate <sentence> into windows of <maxlen> tokens each.
    * Returns a list of windows. Each window is a tuple with:
        - The offset and endpos, indicating where it starts and ends in sentence.
        - The first and the last spans that start (but maybe not end) in the window.
        - The list of spans, among those from the above line, that lie within aliases2see.
    * Each window will have exactly <maxlen> tokens unless the sentence itself is shorter than that.
    * Windows may overlap. Conversely, large portions of the sentence may not exist in any window, particularly when
      they don't contain any aliases2see.
    * Windows are determined through a greedy packing appraoch that guarantees that:
        - Every alias in aliases2see is present in at least one window.
        - Every alias in aliases2see is present in exactly one window in which it's marked as "to predict".
        - The alias may share this unique window with other aliases, some of which may be 'aliases2see' as well.
        - In this unique window, the alias is guaranteed to have at least <mincontext> context on its left and right.
        - The exception to the above rule is if the sentence boundaries are closer than <mincontext> words.
        - In that case, more words are taken from the "other" direction (e.g., right) up to <maxlen>, if possible.
        - Given multiple aliases to predict in the same window, the window is centered around its leftmost and
          rightmost aliases, making sure their left and right contexts---respectively---are equal.
        - For all of the above, an alias's position is taken as its first token.
        - Something tells me all of the above just sounds like legalese. I hope it doesn't.
    """
    assert 2*mincontext < maxlen

    windows = []

    alias_idx = 0
    while alias_idx < len(spans):
        if alias_idx not in aliases_seen_by_model:
            alias_idx += 1
            continue

        window_first_alias = alias_idx
        window_last_alias = alias_idx
        max_possible_offset = max(0, spans[alias_idx][0] - mincontext)
        window_aliases2see = [window_first_alias]

        # Expand with more aliases within the same window
        while alias_idx+1 < len(spans):
            # Stop if adding another alias would prevent retaining mincontext to the left of window_first_alias
            if min(spans[alias_idx+1][0] + mincontext, len(sentence)) > max_possible_offset + maxlen:
                break

            alias_idx += 1
            window_last_alias = alias_idx if alias_idx in aliases_seen_by_model else window_last_alias

            if alias_idx in aliases_seen_by_model:
                window_aliases2see.append(alias_idx)

        center = (spans[window_first_alias][0] + spans[window_last_alias][0]) // 2
        window_offset = max(center - (maxlen // 2) + 1, 0)
        window_endpos = center + (maxlen // 2)
        window_endpos += max(maxlen - (window_endpos - window_offset), 0)
        window_endpos = min(window_endpos, len(sentence))
        window_offset -= max(maxlen - (window_endpos - window_offset), 0)
        window_offset = max(window_offset, 0)

        while window_first_alias > 0:
            if spans[window_first_alias-1][0] < window_offset:
                break
            window_first_alias -= 1

        while window_last_alias+1 < len(spans):
            if spans[window_last_alias+1][0] >= window_endpos:
                break
            window_last_alias += 1

        windows.append((window_offset, window_endpos, window_first_alias, window_last_alias+1, window_aliases2see))
        alias_idx += 1

    if sanity_check:
        for alias_idx, (offset, endpos) in enumerate(spans):
            assert 0 <= offset and offset < endpos and endpos <= len(sentence)
            windowX = [(o, e, f, l, A) for o, e, f, l, A in windows if f <= alias_idx and alias_idx < l]
            assert len(windowX) >= int(alias_idx in aliases_seen_by_model)
            window = [(o, e, f, l, A) for o, e, f, l, A in windows if alias_idx in A]
            assert len(window) == int(alias_idx in aliases_seen_by_model)

            if alias_idx in aliases_seen_by_model:
                assert window[0] in windowX
                window_offset, window_endpos, _, _, _ = window[0]
                assert window_offset <= max(offset - mincontext, 0)
                assert min(offset + mincontext, len(sentence)) <= window_endpos+1
                assert window_endpos - window_offset == min(maxlen, len(sentence))

    return windows