def phrase_extraction(srctext, trgtext, alignment, max_phrase_length=0):
 srctext = srctext.split()
 trgtext = trgtext.split()
 srclen = len(srctext)
 trglen = len(trgtext)
 f_aligned = [j for (_, j) in alignment]
 max_phrase_length = (max_phrase_length or max(srclen, trglen))
 bp = set()
 for e_start in range(srclen):
  max_idx = min(srclen, (e_start + max_phrase_length))
  for e_end in range(e_start, max_idx):
   (f_start, f_end) = ((trglen - 1), (-1))
   for (e, f) in alignment:
    if (e_start <= e <= e_end):
     f_start = min(f, f_start)
     f_end = max(f, f_end)
   phrases = extract(f_start, f_end, e_start, e_end, alignment, f_aligned, srctext, trgtext, srclen, trglen, max_phrase_length)
   if phrases:
    bp.update(phrases)
 return bp

def phrase_extraction ( srctext , trgtext , alignment , max_phrase_length = 0 ) : srctext = srctext . split ( ) trgtext = trgtext . split ( ) srclen = len ( srctext ) trglen = len ( trgtext ) f_aligned = [ j for ( _ , j ) in alignment ] max_phrase_length = ( max_phrase_length or max ( srclen , trglen ) ) bp = set ( ) for e_start in range ( srclen ) : max_idx = min ( srclen , ( e_start + max_phrase_length ) ) for e_end in range ( e_start , max_idx ) : ( f_start , f_end ) = ( ( trglen - 1 ) , ( - 1 ) ) for ( e , f ) in alignment : if ( e_start <= e <= e_end ) : f_start = min ( f , f_start ) f_end = max ( f , f_end ) phrases = extract ( f_start , f_end , e_start , e_end , alignment , f_aligned , srctext , trgtext , srclen , trglen , max_phrase_length ) if phrases : bp . update ( phrases ) return bp
phrase extraction algorithm extracts all consistent phrase pairs from a word-aligned sentence pair .