def _fix_out_of_sentence_links( alignments, sent_start, sent_end ):
    ''' Fixes out-of-the-sentence links in the given sentence.
        The sentence is a sublist of *alignments*, starting from 
        *sent_start* and ending one token before *sent_end*;
    ''' 
    sent_len = sent_end - sent_start
    j = sent_start
    while j < sent_start + sent_len:
        for rel_id, rel in enumerate( alignments[j][PARSER_OUT] ):
            if int( rel[1] ) >= sent_len:
                # If the link points out-of-the-sentence, fix
                # the link so that it points inside the sentence
                # boundaries:
                wid = j - sent_start
                if sent_len == 1:
                    # a single word becomes a root
                    rel[1] = -1
                elif wid-1 > -1:
                    # word at the middle/end is linked to the previous
                    rel[1] = wid - 1
                elif wid-1 == -1:
                    # word at the beginning is linked to the next
                    rel[1] = wid + 1
                alignments[j][PARSER_OUT][rel_id] = rel
        j += 1