bleualign/gale_church.py
def align_blocks(source_sentences, target_sentences, params = LanguageIndependent):
    """Creates the sentence alignment of two blocks of texts (usually paragraphs).

    @param source_sentences: The list of source sentence lengths.
    @param target_sentences: The list of target sentence lengths.
    @param params: the sentence alignment parameters.

    @return: The sentence alignments, a list of index pairs.
    """
    alignment_types = list(params.PRIORS.keys())

    # there are always three rows in the history (with the last of them being filled)
    # and the rows are always |target_text| + 2, so that we never have to do
    # boundary checks
    D = [(len(target_sentences) + 2) * [0] for x in range(2)]

    # for the first sentence, only substitution, insertion or deletion are
    # allowed, and they are all equally likely ( == 1)

    D.append([0, 1])
    try:
      D[-2][1] = 1
      D[-2][2] = 1
    except:
      pass

    backlinks = {}

    for i in range(len(source_sentences)):
        for j in range(len(target_sentences)):
            m = []
            for a in alignment_types:
                k = D[-(1 + a[0])][j + 2 - a[1]]
                if k > 0:
                    p = k * \
                      align_probability(i, j, source_sentences, target_sentences, a, params)
                    m.append((p, a))

            if len(m) > 0:
                v = max(m)
                backlinks[(i, j)] = v[1]
                D[-1].append(v[0])
            else:
                backlinks[(i, j)] = (1, 1)
                D[-1].append(0)

        D.pop(0)
        D.append([0, 0])

    return trace(backlinks, source_sentences, target_sentences)
def align_blocks(source_sentences, target_sentences, params = LanguageIndependent):
    """Creates the sentence alignment of two blocks of texts (usually paragraphs).

    @param source_sentences: The list of source sentence lengths.
    @param target_sentences: The list of target sentence lengths.
    @param params: the sentence alignment parameters.

    @return: The sentence alignments, a list of index pairs.
    """
    alignment_types = list(params.PRIORS.keys())

    # there are always three rows in the history (with the last of them being filled)
    # and the rows are always |target_text| + 2, so that we never have to do
    # boundary checks
    D = [(len(target_sentences) + 2) * [0] for x in range(2)]

    # for the first sentence, only substitution, insertion or deletion are
    # allowed, and they are all equally likely ( == 1)

    D.append([0, 1])
    try:
      D[-2][1] = 1
      D[-2][2] = 1
    except:
      pass

    backlinks = {}

    for i in range(len(source_sentences)):
        for j in range(len(target_sentences)):
            m = []
            for a in alignment_types:
                k = D[-(1 + a[0])][j + 2 - a[1]]
                if k > 0:
                    p = k * \
                      align_probability(i, j, source_sentences, target_sentences, a, params)
                    m.append((p, a))

            if len(m) > 0:
                v = max(m)
                backlinks[(i, j)] = v[1]
                D[-1].append(v[0])
            else:
                backlinks[(i, j)] = (1, 1)
                D[-1].append(0)

        D.pop(0)
        D.append([0, 0])

    return trace(backlinks, source_sentences, target_sentences)
def align_blocks ( source_sentences , target_sentences , params = LanguageIndependent ) : alignment_types = list ( params . PRIORS . keys ( ) ) # there are always three rows in the history (with the last of them being filled) # and the rows are always |target_text| + 2, so that we never have to do # boundary checks D = [ ( len ( target_sentences ) + 2 ) * [ 0 ] for x in range ( 2 ) ] # for the first sentence, only substitution, insertion or deletion are # allowed, and they are all equally likely ( == 1) D . append ( [ 0 , 1 ] ) try : D [ - 2 ] [ 1 ] = 1 D [ - 2 ] [ 2 ] = 1 except : pass backlinks = { } for i in range ( len ( source_sentences ) ) : for j in range ( len ( target_sentences ) ) : m = [ ] for a in alignment_types : k = D [ - ( 1 + a [ 0 ] ) ] [ j + 2 - a [ 1 ] ] if k > 0 : p = k * align_probability ( i , j , source_sentences , target_sentences , a , params ) m . append ( ( p , a ) ) if len ( m ) > 0 : v = max ( m ) backlinks [ ( i , j ) ] = v [ 1 ] D [ - 1 ] . append ( v [ 0 ] ) else : backlinks [ ( i , j ) ] = ( 1 , 1 ) D [ - 1 ] . append ( 0 ) D . pop ( 0 ) D . append ( [ 0 , 0 ] ) return trace ( backlinks , source_sentences , target_sentences )
Creates the sentence alignment of two blocks of texts (usually paragraphs).

    @param source_sentences: The list of source sentence lengths.
    @param target_sentences: The list of target sentence lengths.
    @param params: the sentence alignment parameters.

    @return: The sentence alignments, a list of index pairs.
Creates the sentence alignment of two blocks of texts ( usually paragraphs ) .