def ghd(ref, hyp, ins_cost=2.0, del_cost=2.0, shift_cost_coeff=1.0, boundary='1'):
 ref_idx = [i for (i, val) in enumerate(ref) if (val == boundary)]
 hyp_idx = [i for (i, val) in enumerate(hyp) if (val == boundary)]
 nref_bound = len(ref_idx)
 nhyp_bound = len(hyp_idx)
 if ((nref_bound == 0) and (nhyp_bound == 0)):
  return 0.0
 elif ((nref_bound > 0) and (nhyp_bound == 0)):
  return (nref_bound * ins_cost)
 elif ((nref_bound == 0) and (nhyp_bound > 0)):
  return (nhyp_bound * del_cost)
 mat = _init_mat((nhyp_bound + 1), (nref_bound + 1), ins_cost, del_cost)
 _ghd_aux(mat, hyp_idx, ref_idx, ins_cost, del_cost, shift_cost_coeff)
 return mat[((-1), (-1))]

def ghd ( ref , hyp , ins_cost = 2.0 , del_cost = 2.0 , shift_cost_coeff = 1.0 , boundary = '1' ) : ref_idx = [ i for ( i , val ) in enumerate ( ref ) if ( val == boundary ) ] hyp_idx = [ i for ( i , val ) in enumerate ( hyp ) if ( val == boundary ) ] nref_bound = len ( ref_idx ) nhyp_bound = len ( hyp_idx ) if ( ( nref_bound == 0 ) and ( nhyp_bound == 0 ) ) : return 0.0 elif ( ( nref_bound > 0 ) and ( nhyp_bound == 0 ) ) : return ( nref_bound * ins_cost ) elif ( ( nref_bound == 0 ) and ( nhyp_bound > 0 ) ) : return ( nhyp_bound * del_cost ) mat = _init_mat ( ( nhyp_bound + 1 ) , ( nref_bound + 1 ) , ins_cost , del_cost ) _ghd_aux ( mat , hyp_idx , ref_idx , ins_cost , del_cost , shift_cost_coeff ) return mat [ ( ( - 1 ) , ( - 1 ) ) ]
compute the generalized hamming distance for a reference and a hypothetical segmentation .