#NEWLINE# def pk( ref , hyp , k = None , boundary = '1' ) : #NEWLINE# #INDENT# '\n    Compute the Pk metric for a pair of segmentations A segmentation\n    is any sequence over a vocabulary of two items (e.g. "0", "1"),\n    where the specified boundary value is used to mark the edge of a\n    segmentation.\n\n    >>> \'%.2f\' % pk(\'0100\'*100, \'1\'*400, 2)\n    \'0.50\'\n    >>> \'%.2f\' % pk(\'0100\'*100, \'0\'*400, 2)\n    \'0.50\'\n    >>> \'%.2f\' % pk(\'0100\'*100, \'0100\'*100, 2)\n    \'0.00\'\n\n    :param ref: the reference segmentation\n    :type ref: str or list\n    :param hyp: the segmentation to evaluate\n    :type hyp: str or list\n    :param k: window size, if None, set to half of the average reference segment length\n    :type boundary: str or int or bool\n    :param boundary: boundary value\n    :type boundary: str or int or bool\n    :rtype: float\n    ' #NEWLINE# if ( k is None ) : #NEWLINE# #INDENT# k = int ( round ( ( len ( ref ) / ( ref . count ( boundary ) * 2.0 ) ) ) ) #NEWLINE# #UNINDENT# err = 0 #NEWLINE# for i in xrange ( ( ( len ( ref ) - k ) + 1 ) ) : #NEWLINE# #INDENT# r = ( ref [ i : ( i + k ) ] . count ( boundary ) > 0 ) #NEWLINE# h = ( hyp [ i : ( i + k ) ] . count ( boundary ) > 0 ) #NEWLINE# if ( r != h ) : #NEWLINE# #INDENT# err += 1 #NEWLINE# #UNINDENT# #UNINDENT# return ( err / ( ( len ( ref ) - k ) + 1.0 ) )