signature:
def sim_matrix ( src , tar , mat = None , mismatch_cost = 0 , match_cost = 1 , symmetric = True , alphabet = None , ) :
    """Return the matrix similarity of two strings .
 With the default parameters , this is identical to sim _ ident .
 It is possible for sim _ matrix to return values outside of the range
 : math : ` [ 0 , 1 ] ` , if values outside that range are present in mat ,
 mismatch _ cost , or match _ cost .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 mat : dict
 A dict mapping tuples to costs ; the tuples are ( src , tar ) pairs of
 symbols from the alphabet parameter
 mismatch _ cost : float
 The value returned if ( src , tar ) is absent from mat when src does
 not equal tar
 match _ cost : float
 The value returned if ( src , tar ) is absent from mat when src equals
 tar
 symmetric : bool
 True if the cost of src not matching tar is identical to the cost
 of tar not matching src ; in this case , the values in mat need only
 contain ( src , tar ) or ( tar , src ) , not both
 alphabet : str
 A collection of tokens from which src and tar are drawn ; if this is
 defined a ValueError is raised if either tar or src is not found in
 alphabet
 Returns
 float
 Matrix similarity
 Raises
 ValueError
 src value not in alphabet
 ValueError
 tar value not in alphabet
 Examples
 > > > NeedlemanWunsch . sim _ matrix ( ' cat ' , ' hat ' )
 > > > NeedlemanWunsch . sim _ matrix ( ' hat ' , ' hat ' )"""

implementation:
if alphabet :
    alphabet = tuple ( alphabet )
    for i in src :
        if i not in alphabet :
            raise ValueError ( 'src value not in alphabet' )
    for i in tar :
        if i not in alphabet :
            raise ValueError ( 'tar value not in alphabet' )
if src == tar :
    if mat and ( src , src ) in mat :
        return mat [ ( src , src ) ]
    return match_cost
if mat and ( src , tar ) in mat :
    return mat [ ( src , tar ) ]
elif symmetric and mat and ( tar , src ) in mat :
    return mat [ ( tar , src ) ]
return mismatch_cost


signature:
def encode ( self , word , max_length = 14 ) :
    """Return the IBM Alpha Search Inquiry System code for a word .
 A collection is necessary as the return type since there can be
 multiple values for a single word . But the collection must be ordered
 since the first value is the primary coding .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to 14)
 Returns
 tuple
 The Alpha - SIS value
 Examples
 > > > pe = AlphaSIS ( )
 > > > pe . encode ( ' Christopher ' )
 ( ' 064018400000 ' , ' 0704018400000 ' , ' 04018400000 ' )
 > > > pe . encode ( ' Niall ' )
 ( ' 02500000 ' , )
 > > > pe . encode ( ' Smith ' )
 ( ' 03100000 ' , )
 > > > pe . encode ( ' Schmidt ' )
 ( ' 063100000 ' , )"""

implementation:
alpha = [ '' ]
pos = 0
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = word . replace ( 'ß' , 'SS' )
word = '' . join ( c for c in word if c in self . _uc_set )
# Clamp max _ length to [ 4 , 64]
if max_length != - 1 :
    max_length = min ( max ( 4 , max_length ) , 64 )
else :
    max_length = 64
# Do special processing for initial substrings
for k in self . _alpha_sis_initials_order :
    if word . startswith ( k ) :
        alpha [ 0 ] += self . _alpha_sis_initials [ k ]
        pos += len ( k )
        break
# Add a ' 0 ' if alpha is still empty
if not alpha [ 0 ] :
    alpha [ 0 ] += '0'
# Whether or not any special initial codes were encoded , iterate
# through the length of the word in the main encoding loop
while pos < len ( word ) :
    orig_pos = pos
    for k in self . _alpha_sis_basic_order :
        if word [ pos : ] . startswith ( k ) :
            if isinstance ( self . _alpha_sis_basic [ k ] , tuple ) :
                newalpha = [ ]
                for i in range ( len ( self . _alpha_sis_basic [ k ] ) ) :
                    newalpha += [ _ + self . _alpha_sis_basic [ k ] [ i ] for _ in alpha ]
                alpha = newalpha
            else :
                alpha = [ _ + self . _alpha_sis_basic [ k ] for _ in alpha ]
            pos += len ( k )
            break
    if pos == orig_pos :
        alpha = [ _ + '_' for _ in alpha ]
        pos += 1
# Trim doublets and placeholders
for i in range ( len ( alpha ) ) :
    pos = 1
    while pos < len ( alpha [ i ] ) :
        if alpha [ i ] [ pos ] == alpha [ i ] [ pos - 1 ] :
            alpha [ i ] = alpha [ i ] [ : pos ] + alpha [ i ] [ pos + 1 : ]
        pos += 1
alpha = ( _ . replace ( '_' , '' ) for _ in alpha )
# Trim codes and return tuple
alpha = ( ( _ + ( '0' * max_length ) ) [ : max_length ] for _ in alpha )
return tuple ( alpha )


signature:
def encode ( self , word , max_length = - 1 ) :
    """Return the PhoneticSpanish coding of word .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to unlimited )
 Returns
 str
 The PhoneticSpanish code
 Examples
 > > > pe = PhoneticSpanish ( )
 > > > pe . encode ( ' Perez ' )
 '094'
 > > > pe . encode ( ' Martinez ' )
 '69364'
 > > > pe . encode ( ' Gutierrez ' )
 '83994'
 > > > pe . encode ( ' Santiago ' )
 '4638'
 > > > pe . encode ( ' Nicolás ' )
 '6454'"""

implementation:
# uppercase , normalize , and decompose , filter to A - Z minus vowels & W
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = '' . join ( c for c in word if c in self . _uc_set )
# merge repeated Ls & Rs
word = word . replace ( 'LL' , 'L' )
word = word . replace ( 'R' , 'R' )
# apply the Soundex algorithm
sdx = word . translate ( self . _trans )
if max_length > 0 :
    sdx = ( sdx + ( '0' * max_length ) ) [ : max_length ]
return sdx


signature:
def qgram_fingerprint ( phrase , qval = 2 , start_stop = '' , joiner = '' ) :
    """Return Q - Gram fingerprint .
 This is a wrapper for : py : meth : ` QGram . fingerprint ` .
 Parameters
 phrase : str
 The string from which to calculate the q - gram fingerprint
 qval : int
 The length of each q - gram ( by default 2)
 start _ stop : str
 The start & stop symbol ( s ) to concatenate on either end of the phrase ,
 as defined in : py : class : ` tokenizer . QGrams `
 joiner : str
 The string that will be placed between each word
 Returns
 str
 The q - gram fingerprint of the phrase
 Examples
 > > > qgram _ fingerprint ( ' The quick brown fox jumped over the lazy dog . ' )
 ' azbrckdoedeleqerfoheicjukblampnfogovowoxpequrortthuiumvewnxjydzy '
 > > > qgram _ fingerprint ( ' Christopher ' )
 ' cherhehrisopphristto '
 > > > qgram _ fingerprint ( ' Niall ' )
 ' aliallni '"""

implementation: return QGram ( ) . fingerprint ( phrase , qval , start_stop , joiner )


signature:
def fingerprint ( self , phrase , qval = 2 , start_stop = '' , joiner = '' ) :
    """Return Q - Gram fingerprint .
 Parameters
 phrase : str
 The string from which to calculate the q - gram fingerprint
 qval : int
 The length of each q - gram ( by default 2)
 start _ stop : str
 The start & stop symbol ( s ) to concatenate on either end of the
 phrase , as defined in : py : class : ` tokenizer . QGrams `
 joiner : str
 The string that will be placed between each word
 Returns
 str
 The q - gram fingerprint of the phrase
 Examples
 > > > qf = QGram ( )
 > > > qf . fingerprint ( ' The quick brown fox jumped over the lazy dog . ' )
 ' azbrckdoedeleqerfoheicjukblampnfogovowoxpequrortthuiumvewnxjydzy '
 > > > qf . fingerprint ( ' Christopher ' )
 ' cherhehrisopphristto '
 > > > qf . fingerprint ( ' Niall ' )
 ' aliallni '"""

implementation:
phrase = unicode_normalize ( 'NFKD' , text_type ( phrase . strip ( ) . lower ( ) ) )
phrase = '' . join ( c for c in phrase if c . isalnum ( ) )
phrase = QGrams ( phrase , qval , start_stop )
phrase = joiner . join ( sorted ( phrase ) )
return phrase


signature:
def dist ( self , src , tar ) :
    """Return the NCD between two strings using BWT plus RLE .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 float
 Compression distance
 Examples
 > > > cmp = NCDbwtrle ( )
 > > > cmp . dist ( ' cat ' , ' hat ' )
 0.75
 > > > cmp . dist ( ' Niall ' , ' Neil ' )
 0.8333334
 > > > cmp . dist ( ' aluminum ' , ' Catalan ' )
 1.0
 > > > cmp . dist ( ' ATCG ' , ' TAGC ' )
 0.8"""

implementation:
if src == tar :
    return 0.0
src_comp = self . _rle . encode ( self . _bwt . encode ( src ) )
tar_comp = self . _rle . encode ( self . _bwt . encode ( tar ) )
concat_comp = self . _rle . encode ( self . _bwt . encode ( src + tar ) )
concat_comp2 = self . _rle . encode ( self . _bwt . encode ( tar + src ) )
return ( min ( len ( concat_comp ) , len ( concat_comp2 ) ) - min ( len ( src_comp ) , len ( tar_comp ) ) ) / max ( len ( src_comp ) , len ( tar_comp ) )


signature:
def dm_soundex ( word , max_length = 6 , zero_pad = True ) :
    """Return the Daitch - Mokotoff Soundex code for a word .
 This is a wrapper for : py : meth : ` DaitchMokotoff . encode ` .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to 6 ; must be between 6 and
 64)
 zero _ pad : bool
 Pad the end of the return value with 0s to achieve a max _ length string
 Returns
 str
 The Daitch - Mokotoff Soundex value
 Examples
 > > > sorted ( dm _ soundex ( ' Christopher ' ) )
 [ ' 494379 ' , ' 594379 ' ]
 > > > dm _ soundex ( ' Niall ' )
 { ' 680000 ' }
 > > > dm _ soundex ( ' Smith ' )
 { ' 463000 ' }
 > > > dm _ soundex ( ' Schmidt ' )
 { ' 463000 ' }
 > > > sorted ( dm _ soundex ( ' The quick brown fox ' , max _ length = 20,
 . . . zero _ pad = False ) )
 [ ' 35457976754 ' , ' 3557976754 ' ]"""

implementation: return DaitchMokotoff ( ) . encode ( word , max_length , zero_pad )


signature:
def encode ( self , word , max_length = 6 , zero_pad = True ) :
    """Return the Daitch - Mokotoff Soundex code for a word .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to 6 ; must be between 6
 and 64)
 zero _ pad : bool
 Pad the end of the return value with 0s to achieve a max _ length
 string
 Returns
 str
 The Daitch - Mokotoff Soundex value
 Examples
 > > > pe = DaitchMokotoff ( )
 > > > sorted ( pe . encode ( ' Christopher ' ) )
 [ ' 494379 ' , ' 594379 ' ]
 > > > pe . encode ( ' Niall ' )
 { ' 680000 ' }
 > > > pe . encode ( ' Smith ' )
 { ' 463000 ' }
 > > > pe . encode ( ' Schmidt ' )
 { ' 463000 ' }
 > > > sorted ( pe . encode ( ' The quick brown fox ' , max _ length = 20,
 . . . zero _ pad = False ) )
 [ ' 35457976754 ' , ' 3557976754 ' ]"""

implementation:
dms = [ '' ]
# initialize empty code list
# Require a max _ length of at least 6 and not more than 64
if max_length != - 1 :
    max_length = min ( max ( 6 , max_length ) , 64 )
else :
    max_length = 64
# uppercase , normalize , decompose , and filter non - A - Z
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = word . replace ( 'ß' , 'SS' )
word = '' . join ( c for c in word if c in self . _uc_set )
# Nothing to convert , return base case
if not word :
    if zero_pad :
        return { '0' * max_length }
    return { '0' }
pos = 0
while pos < len ( word ) : # Iterate through _ dms _ order , which specifies the possible
# substrings for which codes exist in the Daitch - Mokotoff coding
    for sstr in self . _dms_order [ word [ pos ] ] : # pragma : no branch
        if word [ pos : ] . startswith ( sstr ) : # Having determined a valid substring start , retrieve the
        # code
            dm_val = self . _dms_table [ sstr ]
            # Having retried the code ( triple ) , determine the correct
            # positional variant ( first , pre - vocalic , elsewhere )
            if pos == 0 :
                dm_val = dm_val [ 0 ]
            elif ( pos + len ( sstr ) < len ( word ) and word [ pos + len ( sstr ) ] in self . _uc_v_set ) :
                dm_val = dm_val [ 1 ]
            else :
                dm_val = dm_val [ 2 ]
            # Build the code strings
            if isinstance ( dm_val , tuple ) :
                dms = [ _ + text_type ( dm_val [ 0 ] ) for _ in dms ] + [ _ + text_type ( dm_val [ 1 ] ) for _ in dms ]
            else :
                dms = [ _ + text_type ( dm_val ) for _ in dms ]
            pos += len ( sstr )
            break
# Filter out double letters and _ placeholders
dms = ( '' . join ( c for c in self . _delete_consecutive_repeats ( _ ) if c != '_' ) for _ in dms )
# Trim codes and return set
if zero_pad :
    dms = ( ( _ + ( '0' * max_length ) ) [ : max_length ] for _ in dms )
else :
    dms = ( _ [ : max_length ] for _ in dms )
return set ( dms )


signature:
def encode ( self , word ) :
    """Return the Norphone code .
 Parameters
 word : str
 The word to transform
 Returns
 str
 The Norphone code
 Examples
 > > > pe = Norphone ( )
 > > > pe . encode ( ' Hansen ' )
 ' HNSN '
 > > > pe . encode ( ' Larsen ' )
 ' LRSN '
 > > > pe . encode ( ' Aagaard ' )
 ' ÅKRT '
 > > > pe . encode ( ' Braaten ' )
 ' BRTN '
 > > > pe . encode ( ' Sandvik ' )
 ' SNVK '"""

implementation:
word = word . upper ( )
code = ''
skip = 0
if word [ 0 : 2 ] == 'AA' :
    code = 'Å'
    skip = 2
elif word [ 0 : 2 ] == 'GI' :
    code = 'J'
    skip = 2
elif word [ 0 : 3 ] == 'SKY' :
    code = 'X'
    skip = 3
elif word [ 0 : 2 ] == 'EI' :
    code = 'Æ'
    skip = 2
elif word [ 0 : 2 ] == 'KY' :
    code = 'X'
    skip = 2
elif word [ : 1 ] == 'C' :
    code = 'K'
    skip = 1
elif word [ : 1 ] == 'Ä' :
    code = 'Æ'
    skip = 1
elif word [ : 1 ] == 'Ö' :
    code = 'Ø'
    skip = 1
if word [ - 2 : ] == 'DT' :
    word = word [ : - 2 ] + 'T'
# Though the rules indicate this rule applies in all positions , the
# reference implementation indicates it applies only in final position .
elif word [ - 2 : - 1 ] in self . _uc_v_set and word [ - 1 : ] == 'D' :
    word = word [ : - 2 ]
for pos , char in enumerate ( word ) :
    if skip :
        skip -= 1
    else :
        for length in sorted ( self . _replacements , reverse = True ) :
            if word [ pos : pos + length ] in self . _replacements [ length ] :
                code += self . _replacements [ length ] [ word [ pos : pos + length ] ]
                skip = length - 1
                break
        else :
            if not pos or char not in self . _uc_v_set :
                code += char
code = self . _delete_consecutive_repeats ( code )
return code


signature:
def to_tuple ( self ) :
    """Cast to tuple .
 Returns
 tuple
 The confusion table as a 4 - tuple ( tp , tn , fp , fn )
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . to _ tuple ( )
 (120 , 60 , 20 , 30)"""

implementation: return self . _tp , self . _tn , self . _fp , self . _fn


signature:
def to_dict ( self ) :
    """Cast to dict .
 Returns
 dict
 The confusion table as a dict
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > import pprint
 > > > pprint . pprint ( ct . to _ dict ( ) )
 { ' fn ' : 30 , ' fp ' : 20 , ' tn ' : 60 , ' tp ' : 120}"""

implementation: return { 'tp' : self . _tp , 'tn' : self . _tn , 'fp' : self . _fp , 'fn' : self . _fn }


signature:
def population ( self ) :
    """Return population , N .
 Returns
 int
 The population ( N ) of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . population ( )
 230"""

implementation: return self . _tp + self . _tn + self . _fp + self . _fn


signature:
def precision ( self ) :
    r"""Return precision .
 Precision is defined as : math : ` \ frac { tp } { tp + fp } `
 AKA positive predictive value ( PPV )
 Cf . https : / / en . wikipedia . org / wiki / Precision _ and _ recall
 Cf . https : / / en . wikipedia . org / wiki / Information _ retrieval # Precision
 Returns
 float
 The precision of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . precision ( )
 0.8571428571428571"""

implementation:
if self . _tp + self . _fp == 0 :
    return float ( 'NaN' )
return self . _tp / ( self . _tp + self . _fp )


signature:
def precision_gain ( self ) :
    r"""Return gain in precision .
 The gain in precision is defined as :
 : math : ` G ( precision ) = \ frac { precision } { random ~ precision } `
 Cf . https : / / en . wikipedia . org / wiki / Gain _ ( information _ retrieval )
 Returns
 float
 The gain in precision of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . precision _ gain ( )
 1.3142857142857143"""

implementation:
if self . population ( ) == 0 :
    return float ( 'NaN' )
random_precision = self . cond_pos_pop ( ) / self . population ( )
return self . precision ( ) / random_precision


signature:
def recall ( self ) :
    r"""Return recall .
 Recall is defined as : math : ` \ frac { tp } { tp + fn } `
 AKA sensitivity
 AKA true positive rate ( TPR )
 Cf . https : / / en . wikipedia . org / wiki / Precision _ and _ recall
 Cf . https : / / en . wikipedia . org / wiki / Sensitivity _ ( test )
 Cf . https : / / en . wikipedia . org / wiki / Information _ retrieval # Recall
 Returns
 float
 The recall of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . recall ( )
 0.8"""

implementation:
if self . _tp + self . _fn == 0 :
    return float ( 'NaN' )
return self . _tp / ( self . _tp + self . _fn )


signature:
def specificity ( self ) :
    r"""Return specificity .
 Specificity is defined as : math : ` \ frac { tn } { tn + fp } `
 AKA true negative rate ( TNR )
 Cf . https : / / en . wikipedia . org / wiki / Specificity _ ( tests )
 Returns
 float
 The specificity of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . specificity ( )
 0.75"""

implementation:
if self . _tn + self . _fp == 0 :
    return float ( 'NaN' )
return self . _tn / ( self . _tn + self . _fp )


signature:
def npv ( self ) :
    r"""Return negative predictive value ( NPV ) .
 NPV is defined as : math : ` \ frac { tn } { tn + fn } `
 Cf . https : / / en . wikipedia . org / wiki / Negative _ predictive _ value
 Returns
 float
 The negative predictive value of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . npv ( )
 0.66666"""

implementation:
if self . _tn + self . _fn == 0 :
    return float ( 'NaN' )
return self . _tn / ( self . _tn + self . _fn )


signature:
def fallout ( self ) :
    r"""Return fall - out .
 Fall - out is defined as : math : ` \ frac { fp } { fp + tn } `
 AKA false positive rate ( FPR )
 Cf . https : / / en . wikipedia . org / wiki / Information _ retrieval # Fall - out
 Returns
 float
 The fall - out of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . fallout ( )
 0.25"""

implementation:
if self . _fp + self . _tn == 0 :
    return float ( 'NaN' )
return self . _fp / ( self . _fp + self . _tn )


signature:
def fdr ( self ) :
    r"""Return false discovery rate ( FDR ) .
 False discovery rate is defined as : math : ` \ frac { fp } { fp + tp } `
 Cf . https : / / en . wikipedia . org / wiki / False _ discovery _ rate
 Returns
 float
 The false discovery rate of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . fdr ( )
 0.14285714285714285"""

implementation:
if self . _fp + self . _tp == 0 :
    return float ( 'NaN' )
return self . _fp / ( self . _fp + self . _tp )


signature:
def accuracy ( self ) :
    r"""Return accuracy .
 Accuracy is defined as : math : ` \ frac { tp + tn } { population } `
 Cf . https : / / en . wikipedia . org / wiki / Accuracy
 Returns
 float
 The accuracy of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . accuracy ( )
 0.782608695652174"""

implementation:
if self . population ( ) == 0 :
    return float ( 'NaN' )
return ( self . _tp + self . _tn ) / self . population ( )


signature:
def accuracy_gain ( self ) :
    r"""Return gain in accuracy .
 The gain in accuracy is defined as :
 : math : ` G ( accuracy ) = \ frac { accuracy } { random ~ accuracy } `
 Cf . https : / / en . wikipedia . org / wiki / Gain _ ( information _ retrieval )
 Returns
 float
 The gain in accuracy of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . accuracy _ gain ( )
 1.4325259515570934"""

implementation:
if self . population ( ) == 0 :
    return float ( 'NaN' )
random_accuracy = ( self . cond_pos_pop ( ) / self . population ( ) ) ** 2 + ( self . cond_neg_pop ( ) / self . population ( ) ) ** 2
return self . accuracy ( ) / random_accuracy


signature:
def pr_lmean ( self ) :
    r"""Return logarithmic mean of precision & recall .
 The logarithmic mean is :
 0 if either precision or recall is 0,
 the precision if they are equal ,
 otherwise : math : ` \ frac { precision - recall }
 { ln ( precision ) - ln ( recall ) } `
 Cf . https : / / en . wikipedia . org / wiki / Logarithmic _ mean
 Returns
 float
 The logarithmic mean of the confusion table ' s precision & recall
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . pr _ lmean ( )
 0.8282429171492667"""

implementation:
precision = self . precision ( )
recall = self . recall ( )
if not precision or not recall :
    return 0.0
elif precision == recall :
    return precision
return ( precision - recall ) / ( math . log ( precision ) - math . log ( recall ) )


signature:
def fbeta_score ( self , beta = 1.0 ) :
    r"""Return : math : ` F _ { \ beta } ` score .
 : math : ` F _ { \ beta } ` for a positive real value : math : ` \ beta ` " measures
 the effectiveness of retrieval with respect to a user who
 attaches : math : ` \ beta ` times as much importance to recall as
 precision " ( van Rijsbergen 1979)
 : math : ` F _ { \ beta } ` score is defined as :
 : math : ` ( 1 + \ beta ^ 2 ) \ cdot \ frac { precision \ cdot recall }
 { ( ( \ beta ^ 2 \ cdot precision ) + recall ) } `
 Cf . https : / / en . wikipedia . org / wiki / F1 _ score
 Parameters
 beta : float
 The : math : ` \ beta ` parameter in the above formula
 Returns
 float
 The : math : ` F _ { \ beta } ` of the confusion table
 Raises
 AttributeError
 Beta must be a positive real value
 Examples
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . fbeta _ score ( )
 0.8275862068965518
 > > > ct . fbeta _ score ( beta = 0.1)
 0.8565371024734982"""

implementation:
if beta <= 0 :
    raise AttributeError ( 'Beta must be a positive real value.' )
precision = self . precision ( )
recall = self . recall ( )
return ( ( 1 + beta ** 2 ) * precision * recall / ( ( beta ** 2 * precision ) + recall ) )


signature:
def mcc ( self ) :
    r"""Return Matthews correlation coefficient ( MCC ) .
 The Matthews correlation coefficient is defined in
 : cite : ` Matthews : 1975 ` as :
 : math : ` \ frac { ( tp \ cdot tn ) - ( fp \ cdot fn ) }
 { \ sqrt { ( tp + fp ) ( tp + fn ) ( tn + fp ) ( tn + fn ) } } `
 This is equivalent to the geometric mean of informedness and
 markedness , defined above .
 Cf . https : / / en . wikipedia . org / wiki / Matthews _ correlation _ coefficient
 Returns
 float
 The Matthews correlation coefficient of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . mcc ( )
 0.5367450401216932"""

implementation:
if ( ( ( self . _tp + self . _fp ) * ( self . _tp + self . _fn ) * ( self . _tn + self . _fp ) * ( self . _tn + self . _fn ) ) ) == 0 :
    return float ( 'NaN' )
return ( ( self . _tp * self . _tn ) - ( self . _fp * self . _fn ) ) / math . sqrt ( ( self . _tp + self . _fp ) * ( self . _tp + self . _fn ) * ( self . _tn + self . _fp ) * ( self . _tn + self . _fn ) )


signature:
def significance ( self ) :
    r"""Return the significance , : math : ` \ chi ^ { 2 } ` .
 Significance is defined as :
 : math : ` \ chi ^ { 2 } =
 \ frac { ( tp \ cdot tn - fp \ cdot fn ) ^ { 2 } ( tp + tn + fp + fn ) }
 { ( ( tp + fp ) ( tp + fn ) ( tn + fp ) ( tn + fn ) } `
 Also : : math : ` \ chi ^ { 2 } = MCC ^ { 2 } \ cdot n `
 Cf . https : / / en . wikipedia . org / wiki / Pearson % 27s _ chi - square _ test
 Returns
 float
 The significance of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . significance ( )
 66.26190476190476"""

implementation:
if ( ( ( self . _tp + self . _fp ) * ( self . _tp + self . _fn ) * ( self . _tn + self . _fp ) * ( self . _tn + self . _fn ) ) ) == 0 :
    return float ( 'NaN' )
return ( ( self . _tp * self . _tn - self . _fp * self . _fn ) ** 2 * ( self . _tp + self . _tn + self . _fp + self . _fn ) ) / ( ( self . _tp + self . _fp ) * ( self . _tp + self . _fn ) * ( self . _tn + self . _fp ) * ( self . _tn + self . _fn ) )


signature:
def kappa_statistic ( self ) :
    r"""Return κ statistic .
 The κ statistic is defined as :
 : math : ` \ kappa = \ frac { accuracy - random ~ accuracy }
 {1 - random ~ accuracy } `
 The κ statistic compares the performance of the classifier relative to
 the performance of a random classifier . : math : ` \ kappa ` = 0 indicates
 performance identical to random . : math : ` \ kappa ` = 1 indicates perfect
 predictive success . : math : ` \ kappa ` = - 1 indicates perfect predictive
 failure .
 Returns
 float
 The κ statistic of the confusion table
 Example
 > > > ct = ConfusionTable ( 120 , 60 , 20 , 30)
 > > > ct . kappa _ statistic ( )
 0.5344129554655871"""

implementation:
if self . population ( ) == 0 :
    return float ( 'NaN' )
random_accuracy = ( ( self . _tn + self . _fp ) * ( self . _tn + self . _fn ) + ( self . _fn + self . _tp ) * ( self . _fp + self . _tp ) ) / self . population ( ) ** 2
return ( self . accuracy ( ) - random_accuracy ) / ( 1 - random_accuracy )


signature:
def stem ( self , word ) :
    """Return CLEF German stem .
 Parameters
 word : str
 The word to stem
 Returns
 str
 Word stem
 Examples
 > > > stmr = CLEFGerman ( )
 > > > stmr . stem ( ' lesen ' )
 ' lese '
 > > > stmr . stem ( ' graues ' )
 ' grau '
 > > > stmr . stem ( ' buchstabieren ' )
 ' buchstabier '"""

implementation:
# lowercase , normalize , and compose
word = normalize ( 'NFC' , text_type ( word . lower ( ) ) )
# remove umlauts
word = word . translate ( self . _umlauts )
# remove plurals
wlen = len ( word ) - 1
if wlen > 3 :
    if wlen > 5 :
        if word [ - 3 : ] == 'nen' :
            return word [ : - 3 ]
    if wlen > 4 :
        if word [ - 2 : ] in { 'en' , 'se' , 'es' , 'er' } :
            return word [ : - 2 ]
    if word [ - 1 ] in { 'e' , 'n' , 'r' , 's' } :
        return word [ : - 1 ]
return word


signature:
def stem ( self , word ) :
    """Return Snowball Danish stem .
 Parameters
 word : str
 The word to stem
 Returns
 str
 Word stem
 Examples
 > > > stmr = SnowballDanish ( )
 > > > stmr . stem ( ' underviser ' )
 ' undervis '
 > > > stmr . stem ( ' suspension ' )
 ' suspension '
 > > > stmr . stem ( ' sikkerhed ' )
 ' sikker '"""

implementation:
# lowercase , normalize , and compose
word = normalize ( 'NFC' , text_type ( word . lower ( ) ) )
r1_start = min ( max ( 3 , self . _sb_r1 ( word ) ) , len ( word ) )
# Step 1
_r1 = word [ r1_start : ]
if _r1 [ - 7 : ] == 'erendes' :
    word = word [ : - 7 ]
elif _r1 [ - 6 : ] in { 'erende' , 'hedens' } :
    word = word [ : - 6 ]
elif _r1 [ - 5 : ] in { 'ethed' , 'erede' , 'heden' , 'heder' , 'endes' , 'ernes' , 'erens' , 'erets' , } :
    word = word [ : - 5 ]
elif _r1 [ - 4 : ] in { 'ered' , 'ende' , 'erne' , 'eren' , 'erer' , 'heds' , 'enes' , 'eres' , 'eret' , } :
    word = word [ : - 4 ]
elif _r1 [ - 3 : ] in { 'hed' , 'ene' , 'ere' , 'ens' , 'ers' , 'ets' } :
    word = word [ : - 3 ]
elif _r1 [ - 2 : ] in { 'en' , 'er' , 'es' , 'et' } :
    word = word [ : - 2 ]
elif _r1 [ - 1 : ] == 'e' :
    word = word [ : - 1 ]
elif _r1 [ - 1 : ] == 's' :
    if len ( word ) > 1 and word [ - 2 ] in self . _s_endings :
        word = word [ : - 1 ]
# Step 2
if word [ r1_start : ] [ - 2 : ] in { 'gd' , 'dt' , 'gt' , 'kt' } :
    word = word [ : - 1 ]
# Step 3
if word [ - 4 : ] == 'igst' :
    word = word [ : - 2 ]
_r1 = word [ r1_start : ]
repeat_step2 = False
if _r1 [ - 4 : ] == 'elig' :
    word = word [ : - 4 ]
    repeat_step2 = True
elif _r1 [ - 4 : ] == 'løst' :
    word = word [ : - 1 ]
elif _r1 [ - 3 : ] in { 'lig' , 'els' } :
    word = word [ : - 3 ]
    repeat_step2 = True
elif _r1 [ - 2 : ] == 'ig' :
    word = word [ : - 2 ]
    repeat_step2 = True
if repeat_step2 :
    if word [ r1_start : ] [ - 2 : ] in { 'gd' , 'dt' , 'gt' , 'kt' } :
        word = word [ : - 1 ]
# Step 4
if ( len ( word [ r1_start : ] ) >= 1 and len ( word ) >= 2 and word [ - 1 ] == word [ - 2 ] and word [ - 1 ] not in self . _vowels ) :
    word = word [ : - 1 ]
return word


signature:
def stem ( self , word , alternate_vowels = False ) :
    """Return Snowball German stem .
 Parameters
 word : str
 The word to stem
 alternate _ vowels : bool
 Composes ae as ä , oe as ö , and ue as ü before running the algorithm
 Returns
 str
 Word stem
 Examples
 > > > stmr = SnowballGerman ( )
 > > > stmr . stem ( ' lesen ' )
 ' les '
 > > > stmr . stem ( ' graues ' )
 ' grau '
 > > > stmr . stem ( ' buchstabieren ' )
 ' buchstabi '"""

implementation:
# lowercase , normalize , and compose
word = normalize ( 'NFC' , word . lower ( ) )
word = word . replace ( 'ß' , 'ss' )
if len ( word ) > 2 :
    for i in range ( 2 , len ( word ) ) :
        if word [ i ] in self . _vowels and word [ i - 2 ] in self . _vowels :
            if word [ i - 1 ] == 'u' :
                word = word [ : i - 1 ] + 'U' + word [ i : ]
            elif word [ i - 1 ] == 'y' :
                word = word [ : i - 1 ] + 'Y' + word [ i : ]
if alternate_vowels :
    word = word . replace ( 'ae' , 'ä' )
    word = word . replace ( 'oe' , 'ö' )
    word = word . replace ( 'que' , 'Q' )
    word = word . replace ( 'ue' , 'ü' )
    word = word . replace ( 'Q' , 'que' )
r1_start = max ( 3 , self . _sb_r1 ( word ) )
r2_start = self . _sb_r2 ( word )
# Step 1
niss_flag = False
if word [ - 3 : ] == 'ern' :
    if len ( word [ r1_start : ] ) >= 3 :
        word = word [ : - 3 ]
elif word [ - 2 : ] == 'em' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
elif word [ - 2 : ] == 'er' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
elif word [ - 2 : ] == 'en' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
        niss_flag = True
elif word [ - 2 : ] == 'es' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
        niss_flag = True
elif word [ - 1 : ] == 'e' :
    if len ( word [ r1_start : ] ) >= 1 :
        word = word [ : - 1 ]
        niss_flag = True
elif word [ - 1 : ] == 's' :
    if ( len ( word [ r1_start : ] ) >= 1 and len ( word ) >= 2 and word [ - 2 ] in self . _s_endings ) :
        word = word [ : - 1 ]
if niss_flag and word [ - 4 : ] == 'niss' :
    word = word [ : - 1 ]
# Step 2
if word [ - 3 : ] == 'est' :
    if len ( word [ r1_start : ] ) >= 3 :
        word = word [ : - 3 ]
elif word [ - 2 : ] == 'en' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
elif word [ - 2 : ] == 'er' :
    if len ( word [ r1_start : ] ) >= 2 :
        word = word [ : - 2 ]
elif word [ - 2 : ] == 'st' :
    if ( len ( word [ r1_start : ] ) >= 2 and len ( word ) >= 6 and word [ - 3 ] in self . _st_endings ) :
        word = word [ : - 2 ]
# Step 3
if word [ - 4 : ] == 'isch' :
    if len ( word [ r2_start : ] ) >= 4 and word [ - 5 ] != 'e' :
        word = word [ : - 4 ]
elif word [ - 4 : ] in { 'lich' , 'heit' } :
    if len ( word [ r2_start : ] ) >= 4 :
        word = word [ : - 4 ]
        if word [ - 2 : ] in { 'er' , 'en' } and len ( word [ r1_start : ] ) >= 2 :
            word = word [ : - 2 ]
elif word [ - 4 : ] == 'keit' :
    if len ( word [ r2_start : ] ) >= 4 :
        word = word [ : - 4 ]
        if word [ - 4 : ] == 'lich' and len ( word [ r2_start : ] ) >= 4 :
            word = word [ : - 4 ]
        elif word [ - 2 : ] == 'ig' and len ( word [ r2_start : ] ) >= 2 :
            word = word [ : - 2 ]
elif word [ - 3 : ] in { 'end' , 'ung' } :
    if len ( word [ r2_start : ] ) >= 3 :
        word = word [ : - 3 ]
        if ( word [ - 2 : ] == 'ig' and len ( word [ r2_start : ] ) >= 2 and word [ - 3 ] != 'e' ) :
            word = word [ : - 2 ]
elif word [ - 2 : ] in { 'ig' , 'ik' } :
    if len ( word [ r2_start : ] ) >= 2 and word [ - 3 ] != 'e' :
        word = word [ : - 2 ]
# Change ' Y ' and ' U ' back to lowercase if survived stemming
for i in range ( 0 , len ( word ) ) :
    if word [ i ] == 'Y' :
        word = word [ : i ] + 'y' + word [ i + 1 : ]
    elif word [ i ] == 'U' :
        word = word [ : i ] + 'u' + word [ i + 1 : ]
# Remove umlauts
_umlauts = dict ( zip ( ( ord ( _ ) for _ in 'äöü' ) , 'aou' ) )
word = word . translate ( _umlauts )
return word


signature:
def dist_abs ( self , src , tar , max_offset = 5 ) :
    """Return the " simplest " Sift4 distance between two terms .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 max _ offset : int
 The number of characters to search for matching letters
 Returns
 int
 The Sift4 distance according to the simplest formula
 Examples
 > > > cmp = Sift4Simplest ( )
 > > > cmp . dist _ abs ( ' cat ' , ' hat ' )
 > > > cmp . dist _ abs ( ' Niall ' , ' Neil ' )
 > > > cmp . dist _ abs ( ' Colin ' , ' Cuilen ' )
 > > > cmp . dist _ abs ( ' ATCG ' , ' TAGC ' )"""

implementation:
if not src :
    return len ( tar )
if not tar :
    return len ( src )
src_len = len ( src )
tar_len = len ( tar )
src_cur = 0
tar_cur = 0
lcss = 0
local_cs = 0
while ( src_cur < src_len ) and ( tar_cur < tar_len ) :
    if src [ src_cur ] == tar [ tar_cur ] :
        local_cs += 1
    else :
        lcss += local_cs
        local_cs = 0
        if src_cur != tar_cur :
            src_cur = tar_cur = max ( src_cur , tar_cur )
        for i in range ( max_offset ) :
            if not ( ( src_cur + i < src_len ) or ( tar_cur + i < tar_len ) ) :
                break
            if ( src_cur + i < src_len ) and ( src [ src_cur + i ] == tar [ tar_cur ] ) :
                src_cur += i
                local_cs += 1
                break
            if ( tar_cur + i < tar_len ) and ( src [ src_cur ] == tar [ tar_cur + i ] ) :
                tar_cur += i
                local_cs += 1
                break
    src_cur += 1
    tar_cur += 1
lcss += local_cs
return round ( max ( src_len , tar_len ) - lcss )


signature:
def typo ( src , tar , metric = 'euclidean' , cost = ( 1 , 1 , 0.5 , 0.5 ) , layout = 'QWERTY' ) :
    """Return the typo distance between two strings .
 This is a wrapper for : py : meth : ` Typo . typo ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 metric : str
 Supported values include : ` ` euclidean ` ` , ` ` manhattan ` ` ,
 ` ` log - euclidean ` ` , and ` ` log - manhattan ` `
 cost : tuple
 A 4 - tuple representing the cost of the four possible edits : inserts ,
 deletes , substitutions , and shift , respectively ( by default :
 (1 , 1 , 0.5 , 0.5 ) ) The substitution & shift costs should be
 significantly less than the cost of an insertion & deletion unless a
 log metric is used .
 layout : str
 Name of the keyboard layout to use ( Currently supported :
 ` ` QWERTY ` ` , ` ` Dvorak ` ` , ` ` AZERTY ` ` , ` ` QWERTZ ` ` )
 Returns
 float
 Typo distance
 Examples
 > > > typo ( ' cat ' , ' hat ' )
 1.5811388
 > > > typo ( ' Niall ' , ' Neil ' )
 2.8251407
 > > > typo ( ' Colin ' , ' Cuilen ' )
 3.4142137
 > > > typo ( ' ATCG ' , ' TAGC ' )
 2.5
 > > > typo ( ' cat ' , ' hat ' , metric = ' manhattan ' )
 2.0
 > > > typo ( ' Niall ' , ' Neil ' , metric = ' manhattan ' )
 3.0
 > > > typo ( ' Colin ' , ' Cuilen ' , metric = ' manhattan ' )
 3.5
 > > > typo ( ' ATCG ' , ' TAGC ' , metric = ' manhattan ' )
 2.5
 > > > typo ( ' cat ' , ' hat ' , metric = ' log - manhattan ' )
 0.804719
 > > > typo ( ' Niall ' , ' Neil ' , metric = ' log - manhattan ' )
 2.2424533
 > > > typo ( ' Colin ' , ' Cuilen ' , metric = ' log - manhattan ' )
 2.2424533
 > > > typo ( ' ATCG ' , ' TAGC ' , metric = ' log - manhattan ' )
 2.3465736"""

implementation: return Typo ( ) . dist_abs ( src , tar , metric , cost , layout )


signature:
def dist_typo ( src , tar , metric = 'euclidean' , cost = ( 1 , 1 , 0.5 , 0.5 ) , layout = 'QWERTY' ) :
    """Return the normalized typo distance between two strings .
 This is a wrapper for : py : meth : ` Typo . dist ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 metric : str
 Supported values include : ` ` euclidean ` ` , ` ` manhattan ` ` ,
 ` ` log - euclidean ` ` , and ` ` log - manhattan ` `
 cost : tuple
 A 4 - tuple representing the cost of the four possible edits : inserts ,
 deletes , substitutions , and shift , respectively ( by default :
 (1 , 1 , 0.5 , 0.5 ) ) The substitution & shift costs should be
 significantly less than the cost of an insertion & deletion unless a
 log metric is used .
 layout : str
 Name of the keyboard layout to use ( Currently supported :
 ` ` QWERTY ` ` , ` ` Dvorak ` ` , ` ` AZERTY ` ` , ` ` QWERTZ ` ` )
 Returns
 float
 Normalized typo distance
 Examples
 > > > round ( dist _ typo ( ' cat ' , ' hat ' ) , 12)
 0.527046283086
 > > > round ( dist _ typo ( ' Niall ' , ' Neil ' ) , 12)
 0.565028142929
 > > > round ( dist _ typo ( ' Colin ' , ' Cuilen ' ) , 12)
 0.569035609563
 > > > dist _ typo ( ' ATCG ' , ' TAGC ' )
 0.625"""

implementation: return Typo ( ) . dist ( src , tar , metric , cost , layout )


signature:
def sim_typo ( src , tar , metric = 'euclidean' , cost = ( 1 , 1 , 0.5 , 0.5 ) , layout = 'QWERTY' ) :
    """Return the normalized typo similarity between two strings .
 This is a wrapper for : py : meth : ` Typo . sim ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 metric : str
 Supported values include : ` ` euclidean ` ` , ` ` manhattan ` ` ,
 ` ` log - euclidean ` ` , and ` ` log - manhattan ` `
 cost : tuple
 A 4 - tuple representing the cost of the four possible edits : inserts ,
 deletes , substitutions , and shift , respectively ( by default :
 (1 , 1 , 0.5 , 0.5 ) ) The substitution & shift costs should be
 significantly less than the cost of an insertion & deletion unless a
 log metric is used .
 layout : str
 Name of the keyboard layout to use ( Currently supported :
 ` ` QWERTY ` ` , ` ` Dvorak ` ` , ` ` AZERTY ` ` , ` ` QWERTZ ` ` )
 Returns
 float
 Normalized typo similarity
 Examples
 > > > round ( sim _ typo ( ' cat ' , ' hat ' ) , 12)
 0.472953716914
 > > > round ( sim _ typo ( ' Niall ' , ' Neil ' ) , 12)
 0.434971857071
 > > > round ( sim _ typo ( ' Colin ' , ' Cuilen ' ) , 12)
 0.430964390437
 > > > sim _ typo ( ' ATCG ' , ' TAGC ' )
 0.375"""

implementation: return Typo ( ) . sim ( src , tar , metric , cost , layout )


signature:
def dist ( self , src , tar , metric = 'euclidean' , cost = ( 1 , 1 , 0.5 , 0.5 ) , layout = 'QWERTY' , ) :
    """Return the normalized typo distance between two strings .
 This is typo distance , normalized to [ 0 , 1 ] .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 metric : str
 Supported values include : ` ` euclidean ` ` , ` ` manhattan ` ` ,
 ` ` log - euclidean ` ` , and ` ` log - manhattan ` `
 cost : tuple
 A 4 - tuple representing the cost of the four possible edits :
 inserts , deletes , substitutions , and shift , respectively ( by
 default : ( 1 , 1 , 0.5 , 0.5 ) ) The substitution & shift costs should be
 significantly less than the cost of an insertion & deletion unless
 a log metric is used .
 layout : str
 Name of the keyboard layout to use ( Currently supported :
 ` ` QWERTY ` ` , ` ` Dvorak ` ` , ` ` AZERTY ` ` , ` ` QWERTZ ` ` )
 Returns
 float
 Normalized typo distance
 Examples
 > > > cmp = Typo ( )
 > > > round ( cmp . dist ( ' cat ' , ' hat ' ) , 12)
 0.527046283086
 > > > round ( cmp . dist ( ' Niall ' , ' Neil ' ) , 12)
 0.565028142929
 > > > round ( cmp . dist ( ' Colin ' , ' Cuilen ' ) , 12)
 0.569035609563
 > > > cmp . dist ( ' ATCG ' , ' TAGC ' )
 0.625"""

implementation:
if src == tar :
    return 0.0
ins_cost , del_cost = cost [ : 2 ]
return self . dist_abs ( src , tar , metric , cost , layout ) / ( max ( len ( src ) * del_cost , len ( tar ) * ins_cost ) )


signature:
def manhattan ( src , tar , qval = 2 , normalized = False , alphabet = None ) :
    """Return the Manhattan distance between two strings .
 This is a wrapper for : py : meth : ` Manhattan . dist _ abs ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 normalized : bool
 Normalizes to [ 0 , 1 ] if True
 alphabet : collection or int
 The values or size of the alphabet
 Returns
 float
 The Manhattan distance
 Examples
 > > > manhattan ( ' cat ' , ' hat ' )
 4.0
 > > > manhattan ( ' Niall ' , ' Neil ' )
 7.0
 > > > manhattan ( ' Colin ' , ' Cuilen ' )
 9.0
 > > > manhattan ( ' ATCG ' , ' TAGC ' )
 10.0"""

implementation: return Manhattan ( ) . dist_abs ( src , tar , qval , normalized , alphabet )


signature:
def dist_manhattan ( src , tar , qval = 2 , alphabet = None ) :
    """Return the normalized Manhattan distance between two strings .
 This is a wrapper for : py : meth : ` Manhattan . dist ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 alphabet : collection or int
 The values or size of the alphabet
 Returns
 float
 The normalized Manhattan distance
 Examples
 > > > dist _ manhattan ( ' cat ' , ' hat ' )
 0.5
 > > > round ( dist _ manhattan ( ' Niall ' , ' Neil ' ) , 12)
 0.636363636364
 > > > round ( dist _ manhattan ( ' Colin ' , ' Cuilen ' ) , 12)
 0.692307692308
 > > > dist _ manhattan ( ' ATCG ' , ' TAGC ' )
 1.0"""

implementation: return Manhattan ( ) . dist ( src , tar , qval , alphabet )


signature:
def sim_manhattan ( src , tar , qval = 2 , alphabet = None ) :
    """Return the normalized Manhattan similarity of two strings .
 This is a wrapper for : py : meth : ` Manhattan . sim ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 alphabet : collection or int
 The values or size of the alphabet
 Returns
 float
 The normalized Manhattan similarity
 Examples
 > > > sim _ manhattan ( ' cat ' , ' hat ' )
 0.5
 > > > round ( sim _ manhattan ( ' Niall ' , ' Neil ' ) , 12)
 0.363636363636
 > > > round ( sim _ manhattan ( ' Colin ' , ' Cuilen ' ) , 12)
 0.307692307692
 > > > sim _ manhattan ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation: return Manhattan ( ) . sim ( src , tar , qval , alphabet )


signature:
def sim_jaro_winkler ( src , tar , qval = 1 , mode = 'winkler' , long_strings = False , boost_threshold = 0.7 , scaling_factor = 0.1 , ) :
    """Return the Jaro or Jaro - Winkler similarity of two strings .
 This is a wrapper for : py : meth : ` JaroWinkler . sim ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 qval : int
 The length of each q - gram ( defaults to 1 : character - wise matching )
 mode : str
 Indicates which variant of this distance metric to compute :
 - ` ` winkler ` ` - - computes the Jaro - Winkler distance ( default ) which
 increases the score for matches near the start of the word
 - ` ` jaro ` ` - - computes the Jaro distance
 long _ strings : bool
 Set to True to " Increase the probability of a match when the number of
 matched characters is large . This option allows for a little more
 tolerance when the strings are large . It is not an appropriate test
 when comparing fixedlength fields such as phone and social security
 numbers . " ( Used in ' winkler ' mode only . )
 boost _ threshold : float
 A value between 0 and 1 , below which the Winkler boost is not applied
 ( defaults to 0.7 ) . ( Used in ' winkler ' mode only . )
 scaling _ factor : float
 A value between 0 and 0.25 , indicating by how much to boost scores for
 matching prefixes ( defaults to 0.1 ) . ( Used in ' winkler ' mode only . )
 Returns
 float
 Jaro or Jaro - Winkler similarity
 Examples
 > > > round ( sim _ jaro _ winkler ( ' cat ' , ' hat ' ) , 12)
 0.777778
 > > > round ( sim _ jaro _ winkler ( ' Niall ' , ' Neil ' ) , 12)
 0.805
 > > > round ( sim _ jaro _ winkler ( ' aluminum ' , ' Catalan ' ) , 12)
 0.60119047619
 > > > round ( sim _ jaro _ winkler ( ' ATCG ' , ' TAGC ' ) , 12)
 0.833333
 > > > round ( sim _ jaro _ winkler ( ' cat ' , ' hat ' , mode = ' jaro ' ) , 12)
 0.777778
 > > > round ( sim _ jaro _ winkler ( ' Niall ' , ' Neil ' , mode = ' jaro ' ) , 12)
 0.7833333
 > > > round ( sim _ jaro _ winkler ( ' aluminum ' , ' Catalan ' , mode = ' jaro ' ) , 12)
 0.60119047619
 > > > round ( sim _ jaro _ winkler ( ' ATCG ' , ' TAGC ' , mode = ' jaro ' ) , 12)
 0.833333"""

implementation: return JaroWinkler ( ) . sim ( src , tar , qval , mode , long_strings , boost_threshold , scaling_factor )


signature:
def dist_jaro_winkler ( src , tar , qval = 1 , mode = 'winkler' , long_strings = False , boost_threshold = 0.7 , scaling_factor = 0.1 , ) :
    """Return the Jaro or Jaro - Winkler distance between two strings .
 This is a wrapper for : py : meth : ` JaroWinkler . dist ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 qval : int
 The length of each q - gram ( defaults to 1 : character - wise matching )
 mode : str
 Indicates which variant of this distance metric to compute :
 - ` ` winkler ` ` - - computes the Jaro - Winkler distance ( default ) which
 increases the score for matches near the start of the word
 - ` ` jaro ` ` - - computes the Jaro distance
 long _ strings : bool
 Set to True to " Increase the probability of a match when the number of
 matched characters is large . This option allows for a little more
 tolerance when the strings are large . It is not an appropriate test
 when comparing fixedlength fields such as phone and social security
 numbers . " ( Used in ' winkler ' mode only . )
 boost _ threshold : float
 A value between 0 and 1 , below which the Winkler boost is not applied
 ( defaults to 0.7 ) . ( Used in ' winkler ' mode only . )
 scaling _ factor : float
 A value between 0 and 0.25 , indicating by how much to boost scores for
 matching prefixes ( defaults to 0.1 ) . ( Used in ' winkler ' mode only . )
 Returns
 float
 Jaro or Jaro - Winkler distance
 Examples
 > > > round ( dist _ jaro _ winkler ( ' cat ' , ' hat ' ) , 12)
 0.22222
 > > > round ( dist _ jaro _ winkler ( ' Niall ' , ' Neil ' ) , 12)
 0.195
 > > > round ( dist _ jaro _ winkler ( ' aluminum ' , ' Catalan ' ) , 12)
 0.39880952381
 > > > round ( dist _ jaro _ winkler ( ' ATCG ' , ' TAGC ' ) , 12)
 0.1666667
 > > > round ( dist _ jaro _ winkler ( ' cat ' , ' hat ' , mode = ' jaro ' ) , 12)
 0.22222
 > > > round ( dist _ jaro _ winkler ( ' Niall ' , ' Neil ' , mode = ' jaro ' ) , 12)
 0.21666667
 > > > round ( dist _ jaro _ winkler ( ' aluminum ' , ' Catalan ' , mode = ' jaro ' ) , 12)
 0.39880952381
 > > > round ( dist _ jaro _ winkler ( ' ATCG ' , ' TAGC ' , mode = ' jaro ' ) , 12)
 0.1666667"""

implementation: return JaroWinkler ( ) . dist ( src , tar , qval , mode , long_strings , boost_threshold , scaling_factor )


signature:
def sim ( self , src , tar , qval = 1 , mode = 'winkler' , long_strings = False , boost_threshold = 0.7 , scaling_factor = 0.1 , ) :
    """Return the Jaro or Jaro - Winkler similarity of two strings .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 qval : int
 The length of each q - gram ( defaults to 1 : character - wise matching )
 mode : str
 Indicates which variant of this distance metric to compute :
 - ` ` winkler ` ` - - computes the Jaro - Winkler distance ( default )
 which increases the score for matches near the start of the
 word
 - ` ` jaro ` ` - - computes the Jaro distance
 long _ strings : bool
 Set to True to " Increase the probability of a match when the number
 of matched characters is large . This option allows for a little
 more tolerance when the strings are large . It is not an appropriate
 test when comparing fixed length fields such as phone and social
 security numbers . " ( Used in ' winkler ' mode only . )
 boost _ threshold : float
 A value between 0 and 1 , below which the Winkler boost is not
 applied ( defaults to 0.7 ) . ( Used in ' winkler ' mode only . )
 scaling _ factor : float
 A value between 0 and 0.25 , indicating by how much to boost scores
 for matching prefixes ( defaults to 0.1 ) . ( Used in ' winkler ' mode
 only . )
 Returns
 float
 Jaro or Jaro - Winkler similarity
 Raises
 ValueError
 Unsupported boost _ threshold assignment ; boost _ threshold must be
 between 0 and 1.
 ValueError
 Unsupported scaling _ factor assignment ; scaling _ factor must be
 between 0 and 0.25 . '
 Examples
 > > > round ( sim _ jaro _ winkler ( ' cat ' , ' hat ' ) , 12)
 0.777778
 > > > round ( sim _ jaro _ winkler ( ' Niall ' , ' Neil ' ) , 12)
 0.805
 > > > round ( sim _ jaro _ winkler ( ' aluminum ' , ' Catalan ' ) , 12)
 0.60119047619
 > > > round ( sim _ jaro _ winkler ( ' ATCG ' , ' TAGC ' ) , 12)
 0.833333
 > > > round ( sim _ jaro _ winkler ( ' cat ' , ' hat ' , mode = ' jaro ' ) , 12)
 0.777778
 > > > round ( sim _ jaro _ winkler ( ' Niall ' , ' Neil ' , mode = ' jaro ' ) , 12)
 0.7833333
 > > > round ( sim _ jaro _ winkler ( ' aluminum ' , ' Catalan ' , mode = ' jaro ' ) , 12)
 0.60119047619
 > > > round ( sim _ jaro _ winkler ( ' ATCG ' , ' TAGC ' , mode = ' jaro ' ) , 12)
 0.833333"""

implementation:
if mode == 'winkler' :
    if boost_threshold > 1 or boost_threshold < 0 :
        raise ValueError ( 'Unsupported boost_threshold assignment; ' + 'boost_threshold must be between 0 and 1.' )
    if scaling_factor > 0.25 or scaling_factor < 0 :
        raise ValueError ( 'Unsupported scaling_factor assignment; ' + 'scaling_factor must be between 0 and 0.25.' )
if src == tar :
    return 1.0
src = QGrams ( src . strip ( ) , qval ) . _ordered_list
tar = QGrams ( tar . strip ( ) , qval ) . _ordered_list
lens = len ( src )
lent = len ( tar )
# If either string is blank - return - added in Version 2
if lens == 0 or lent == 0 :
    return 0.0
if lens > lent :
    search_range = lens
    minv = lent
else :
    search_range = lent
    minv = lens
# Zero out the flags
src_flag = [ 0 ] * search_range
tar_flag = [ 0 ] * search_range
search_range = max ( 0 , search_range // 2 - 1 )
# Looking only within the search range ,
# count and flag the matched pairs .
num_com = 0
yl1 = lent - 1
for i in range ( lens ) :
    low_lim = ( i - search_range ) if ( i >= search_range ) else 0
    hi_lim = ( i + search_range ) if ( ( i + search_range ) <= yl1 ) else yl1
    for j in range ( low_lim , hi_lim + 1 ) :
        if ( tar_flag [ j ] == 0 ) and ( tar [ j ] == src [ i ] ) :
            tar_flag [ j ] = 1
            src_flag [ i ] = 1
            num_com += 1
            break
# If no characters in common - return
if num_com == 0 :
    return 0.0
# Count the number of transpositions
k = n_trans = 0
for i in range ( lens ) :
    if src_flag [ i ] != 0 :
        j = 0
        for j in range ( k , lent ) : # pragma : no branch
            if tar_flag [ j ] != 0 :
                k = j + 1
                break
        if src [ i ] != tar [ j ] :
            n_trans += 1
n_trans //= 2
# Main weight computation for Jaro distance
weight = ( num_com / lens + num_com / lent + ( num_com - n_trans ) / num_com )
weight /= 3.0
# Continue to boost the weight if the strings are similar
# This is the Winkler portion of Jaro - Winkler distance
if mode == 'winkler' and weight > boost_threshold : # Adjust for having up to the first 4 characters in common
    j = 4 if ( minv >= 4 ) else minv
    i = 0
    while ( i < j ) and ( src [ i ] == tar [ i ] ) :
        i += 1
    weight += i * scaling_factor * ( 1.0 - weight )
    # Optionally adjust for long strings .
    # After agreeing beginning chars , at least two more must agree and
    # the agreeing characters must be > . 5 of remaining characters .
    if ( long_strings and ( minv > 4 ) and ( num_com > i + 1 ) and ( 2 * num_com >= minv + i ) ) :
        weight += ( 1.0 - weight ) * ( ( num_com - i - 1 ) / ( lens + lent - i * 2 + 2 ) )
return weight


signature:
def hamming ( src , tar , diff_lens = True ) :
    """Return the Hamming distance between two strings .
 This is a wrapper for : py : meth : ` Hamming . dist _ abs ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 diff _ lens : bool
 If True ( default ) , this returns the Hamming distance for those
 characters that have a matching character in both strings plus the
 difference in the strings ' lengths . This is equivalent to extending the
 shorter string with obligatorily non - matching characters . If False , an
 exception is raised in the case of strings of unequal lengths .
 Returns
 int
 The Hamming distance between src & tar
 Examples
 > > > hamming ( ' cat ' , ' hat ' )
 > > > hamming ( ' Niall ' , ' Neil ' )
 > > > hamming ( ' aluminum ' , ' Catalan ' )
 > > > hamming ( ' ATCG ' , ' TAGC ' )"""

implementation: return Hamming ( ) . dist_abs ( src , tar , diff_lens )


signature:
def dist_hamming ( src , tar , diff_lens = True ) :
    """Return the normalized Hamming distance between two strings .
 This is a wrapper for : py : meth : ` Hamming . dist ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 diff _ lens : bool
 If True ( default ) , this returns the Hamming distance for those
 characters that have a matching character in both strings plus the
 difference in the strings ' lengths . This is equivalent to extending the
 shorter string with obligatorily non - matching characters . If False , an
 exception is raised in the case of strings of unequal lengths .
 Returns
 float
 The normalized Hamming distance
 Examples
 > > > round ( dist _ hamming ( ' cat ' , ' hat ' ) , 12)
 0.33333
 > > > dist _ hamming ( ' Niall ' , ' Neil ' )
 0.6
 > > > dist _ hamming ( ' aluminum ' , ' Catalan ' )
 1.0
 > > > dist _ hamming ( ' ATCG ' , ' TAGC ' )
 1.0"""

implementation: return Hamming ( ) . dist ( src , tar , diff_lens )


signature:
def sim_hamming ( src , tar , diff_lens = True ) :
    """Return the normalized Hamming similarity of two strings .
 This is a wrapper for : py : meth : ` Hamming . sim ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 diff _ lens : bool
 If True ( default ) , this returns the Hamming distance for those
 characters that have a matching character in both strings plus the
 difference in the strings ' lengths . This is equivalent to extending the
 shorter string with obligatorily non - matching characters . If False , an
 exception is raised in the case of strings of unequal lengths .
 Returns
 float
 The normalized Hamming similarity
 Examples
 > > > round ( sim _ hamming ( ' cat ' , ' hat ' ) , 12)
 0.666667
 > > > sim _ hamming ( ' Niall ' , ' Neil ' )
 0.4
 > > > sim _ hamming ( ' aluminum ' , ' Catalan ' )
 0.0
 > > > sim _ hamming ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation: return Hamming ( ) . sim ( src , tar , diff_lens )


signature:
def dist_abs ( self , src , tar , diff_lens = True ) :
    """Return the Hamming distance between two strings .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 diff _ lens : bool
 If True ( default ) , this returns the Hamming distance for those
 characters that have a matching character in both strings plus the
 difference in the strings ' lengths . This is equivalent to extending
 the shorter string with obligatorily non - matching characters . If
 False , an exception is raised in the case of strings of unequal
 lengths .
 Returns
 int
 The Hamming distance between src & tar
 Raises
 ValueError
 Undefined for sequences of unequal length ; set diff _ lens to True
 for Hamming distance between strings of unequal lengths .
 Examples
 > > > cmp = Hamming ( )
 > > > cmp . dist _ abs ( ' cat ' , ' hat ' )
 > > > cmp . dist _ abs ( ' Niall ' , ' Neil ' )
 > > > cmp . dist _ abs ( ' aluminum ' , ' Catalan ' )
 > > > cmp . dist _ abs ( ' ATCG ' , ' TAGC ' )"""

implementation:
if not diff_lens and len ( src ) != len ( tar ) :
    raise ValueError ( 'Undefined for sequences of unequal length; set diff_lens ' + 'to True for Hamming distance between strings of unequal ' + 'lengths.' )
hdist = 0
if diff_lens :
    hdist += abs ( len ( src ) - len ( tar ) )
hdist += sum ( c1 != c2 for c1 , c2 in zip ( src , tar ) )
return hdist


signature:
def dist ( self , src , tar , diff_lens = True ) :
    """Return the normalized Hamming distance between two strings .
 Hamming distance normalized to the interval [ 0 , 1 ] .
 The Hamming distance is normalized by dividing it
 by the greater of the number of characters in src & tar ( unless
 diff _ lens is set to False , in which case an exception is raised ) .
 The arguments are identical to those of the hamming ( ) function .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 diff _ lens : bool
 If True ( default ) , this returns the Hamming distance for those
 characters that have a matching character in both strings plus the
 difference in the strings ' lengths . This is equivalent to extending
 the shorter string with obligatorily non - matching characters . If
 False , an exception is raised in the case of strings of unequal
 lengths .
 Returns
 float
 Normalized Hamming distance
 Examples
 > > > cmp = Hamming ( )
 > > > round ( cmp . dist ( ' cat ' , ' hat ' ) , 12)
 0.33333
 > > > cmp . dist ( ' Niall ' , ' Neil ' )
 0.6
 > > > cmp . dist ( ' aluminum ' , ' Catalan ' )
 1.0
 > > > cmp . dist ( ' ATCG ' , ' TAGC ' )
 1.0"""

implementation:
if src == tar :
    return 0.0
return self . dist_abs ( src , tar , diff_lens ) / max ( len ( src ) , len ( tar ) )


signature:
def encode ( self , word , max_length = - 1 ) :
    """Return the Metaphone code for a word .
 Based on Lawrence Philips ' Pick BASIC code from 1990
 : cite : ` Philips : 1990 ` , as described in : cite : ` Philips : 1990b ` .
 This incorporates some corrections to the above code , particularly
 some of those suggested by Michael Kuhn in : cite : ` Kuhn : 1995 ` .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The maximum length of the returned Metaphone code ( defaults to 64,
 but in Philips ' original implementation this was 4)
 Returns
 str
 The Metaphone value
 Examples
 > > > pe = Metaphone ( )
 > > > pe . encode ( ' Christopher ' )
 ' KRSTFR '
 > > > pe . encode ( ' Niall ' )
 ' NL '
 > > > pe . encode ( ' Smith ' )
 ' SM0'
 > > > pe . encode ( ' Schmidt ' )
 ' SKMTT '"""

implementation:
# Require a max _ length of at least 4
if max_length != - 1 :
    max_length = max ( 4 , max_length )
else :
    max_length = 64
# As in variable sound - - those modified by adding an " h "
ename = '' . join ( c for c in word . upper ( ) if c . isalnum ( ) )
ename = ename . replace ( 'ß' , 'SS' )
# Delete non - alphanumeric characters and make all caps
if not ename :
    return ''
if ename [ 0 : 2 ] in { 'PN' , 'AE' , 'KN' , 'GN' , 'WR' } :
    ename = ename [ 1 : ]
elif ename [ 0 ] == 'X' :
    ename = 'S' + ename [ 1 : ]
elif ename [ 0 : 2 ] == 'WH' :
    ename = 'W' + ename [ 2 : ]
# Convert to metaphone
elen = len ( ename ) - 1
metaph = ''
for i in range ( len ( ename ) ) :
    if len ( metaph ) >= max_length :
        break
    if ( ename [ i ] not in { 'G' , 'T' } and i > 0 and ename [ i - 1 ] == ename [ i ] ) :
        continue
    if ename [ i ] in self . _uc_v_set and i == 0 :
        metaph = ename [ i ]
    elif ename [ i ] == 'B' :
        if i != elen or ename [ i - 1 ] != 'M' :
            metaph += ename [ i ]
    elif ename [ i ] == 'C' :
        if not ( i > 0 and ename [ i - 1 ] == 'S' and ename [ i + 1 : i + 2 ] in self . _frontv ) :
            if ename [ i + 1 : i + 3 ] == 'IA' :
                metaph += 'X'
            elif ename [ i + 1 : i + 2 ] in self . _frontv :
                metaph += 'S'
            elif i > 0 and ename [ i - 1 : i + 2 ] == 'SCH' :
                metaph += 'K'
            elif ename [ i + 1 : i + 2 ] == 'H' :
                if ( i == 0 and i + 1 < elen and ename [ i + 2 : i + 3 ] not in self . _uc_v_set ) :
                    metaph += 'K'
                else :
                    metaph += 'X'
            else :
                metaph += 'K'
    elif ename [ i ] == 'D' :
        if ( ename [ i + 1 : i + 2 ] == 'G' and ename [ i + 2 : i + 3 ] in self . _frontv ) :
            metaph += 'J'
        else :
            metaph += 'T'
    elif ename [ i ] == 'G' :
        if ename [ i + 1 : i + 2 ] == 'H' and not ( i + 1 == elen or ename [ i + 2 : i + 3 ] not in self . _uc_v_set ) :
            continue
        elif i > 0 and ( ( i + 1 == elen and ename [ i + 1 ] == 'N' ) or ( i + 3 == elen and ename [ i + 1 : i + 4 ] == 'NED' ) ) :
            continue
        elif ( i - 1 > 0 and i + 1 <= elen and ename [ i - 1 ] == 'D' and ename [ i + 1 ] in self . _frontv ) :
            continue
        elif ename [ i + 1 : i + 2 ] == 'G' :
            continue
        elif ename [ i + 1 : i + 2 ] in self . _frontv :
            if i == 0 or ename [ i - 1 ] != 'G' :
                metaph += 'J'
            else :
                metaph += 'K'
        else :
            metaph += 'K'
    elif ename [ i ] == 'H' :
        if ( i > 0 and ename [ i - 1 ] in self . _uc_v_set and ename [ i + 1 : i + 2 ] not in self . _uc_v_set ) :
            continue
        elif i > 0 and ename [ i - 1 ] in self . _varson :
            continue
        else :
            metaph += 'H'
    elif ename [ i ] in { 'F' , 'J' , 'L' , 'M' , 'N' , 'R' } :
        metaph += ename [ i ]
    elif ename [ i ] == 'K' :
        if i > 0 and ename [ i - 1 ] == 'C' :
            continue
        else :
            metaph += 'K'
    elif ename [ i ] == 'P' :
        if ename [ i + 1 : i + 2 ] == 'H' :
            metaph += 'F'
        else :
            metaph += 'P'
    elif ename [ i ] == 'Q' :
        metaph += 'K'
    elif ename [ i ] == 'S' :
        if ( i > 0 and i + 2 <= elen and ename [ i + 1 ] == 'I' and ename [ i + 2 ] in 'OA' ) :
            metaph += 'X'
        elif ename [ i + 1 : i + 2 ] == 'H' :
            metaph += 'X'
        else :
            metaph += 'S'
    elif ename [ i ] == 'T' :
        if ( i > 0 and i + 2 <= elen and ename [ i + 1 ] == 'I' and ename [ i + 2 ] in { 'A' , 'O' } ) :
            metaph += 'X'
        elif ename [ i + 1 : i + 2 ] == 'H' :
            metaph += '0'
        elif ename [ i + 1 : i + 3 ] != 'CH' :
            if ename [ i - 1 : i ] != 'T' :
                metaph += 'T'
    elif ename [ i ] == 'V' :
        metaph += 'F'
    elif ename [ i ] in 'WY' :
        if ename [ i + 1 : i + 2 ] in self . _uc_v_set :
            metaph += ename [ i ]
    elif ename [ i ] == 'X' :
        metaph += 'KS'
    elif ename [ i ] == 'Z' :
        metaph += 'S'
return metaph


signature:
def dolby ( word , max_length = - 1 , keep_vowels = False , vowel_char = '*' ) :
    r"""Return the Dolby Code of a name .
 This is a wrapper for : py : meth : ` Dolby . encode ` .
 Parameters
 word : str
 The word to transform
 max _ length : int
 Maximum length of the returned Dolby code - - this also activates the
 fixed - length code mode if it is greater than 0
 keep _ vowels : bool
 If True , retains all vowel markers
 vowel _ char : str
 The vowel marker character ( default to \ * )
 Returns
 str
 The Dolby Code
 Examples
 > > > dolby ( ' Hansen ' )
 ' H * NSN '
 > > > dolby ( ' Larsen ' )
 ' L * RSN '
 > > > dolby ( ' Aagaard ' )
 ' * GR '
 > > > dolby ( ' Braaten ' )
 ' BR * DN '
 > > > dolby ( ' Sandvik ' )
 ' S * NVK '
 > > > dolby ( ' Hansen ' , max _ length = 6)
 ' H * NS * N '
 > > > dolby ( ' Larsen ' , max _ length = 6)
 ' L * RS * N '
 > > > dolby ( ' Aagaard ' , max _ length = 6)
 ' * G * R '
 > > > dolby ( ' Braaten ' , max _ length = 6)
 ' BR * D * N '
 > > > dolby ( ' Sandvik ' , max _ length = 6)
 ' S * NF * K '
 > > > dolby ( ' Smith ' )
 ' SM * D '
 > > > dolby ( ' Waters ' )
 ' W * DRS '
 > > > dolby ( ' James ' )
 ' J * MS '
 > > > dolby ( ' Schmidt ' )
 ' SM * D '
 > > > dolby ( ' Ashcroft ' )
 ' * SKRFD '
 > > > dolby ( ' Smith ' , max _ length = 6)
 ' SM * D '
 > > > dolby ( ' Waters ' , max _ length = 6)
 ' W * D * RS '
 > > > dolby ( ' James ' , max _ length = 6)
 ' J * M * S '
 > > > dolby ( ' Schmidt ' , max _ length = 6)
 ' SM * D '
 > > > dolby ( ' Ashcroft ' , max _ length = 6)
 ' * SKRFD '"""

implementation: return Dolby ( ) . encode ( word , max_length , keep_vowels , vowel_char )


signature:
def encode ( self , word , max_length = - 1 , keep_vowels = False , vowel_char = '*' ) :
    r"""Return the Dolby Code of a name .
 Parameters
 word : str
 The word to transform
 max _ length : int
 Maximum length of the returned Dolby code - - this also activates
 the fixed - length code mode if it is greater than 0
 keep _ vowels : bool
 If True , retains all vowel markers
 vowel _ char : str
 The vowel marker character ( default to \ * )
 Returns
 str
 The Dolby Code
 Examples
 > > > pe = Dolby ( )
 > > > pe . encode ( ' Hansen ' )
 ' H * NSN '
 > > > pe . encode ( ' Larsen ' )
 ' L * RSN '
 > > > pe . encode ( ' Aagaard ' )
 ' * GR '
 > > > pe . encode ( ' Braaten ' )
 ' BR * DN '
 > > > pe . encode ( ' Sandvik ' )
 ' S * NVK '
 > > > pe . encode ( ' Hansen ' , max _ length = 6)
 ' H * NS * N '
 > > > pe . encode ( ' Larsen ' , max _ length = 6)
 ' L * RS * N '
 > > > pe . encode ( ' Aagaard ' , max _ length = 6)
 ' * G * R '
 > > > pe . encode ( ' Braaten ' , max _ length = 6)
 ' BR * D * N '
 > > > pe . encode ( ' Sandvik ' , max _ length = 6)
 ' S * NF * K '
 > > > pe . encode ( ' Smith ' )
 ' SM * D '
 > > > pe . encode ( ' Waters ' )
 ' W * DRS '
 > > > pe . encode ( ' James ' )
 ' J * MS '
 > > > pe . encode ( ' Schmidt ' )
 ' SM * D '
 > > > pe . encode ( ' Ashcroft ' )
 ' * SKRFD '
 > > > pe . encode ( ' Smith ' , max _ length = 6)
 ' SM * D '
 > > > pe . encode ( ' Waters ' , max _ length = 6)
 ' W * D * RS '
 > > > pe . encode ( ' James ' , max _ length = 6)
 ' J * M * S '
 > > > pe . encode ( ' Schmidt ' , max _ length = 6)
 ' SM * D '
 > > > pe . encode ( ' Ashcroft ' , max _ length = 6)
 ' * SKRFD '"""

implementation:
# uppercase , normalize , decompose , and filter non - A - Z out
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = word . replace ( 'ß' , 'SS' )
word = '' . join ( c for c in word if c in self . _uc_set )
# Rule 1 ( FL2)
if word [ : 3 ] in { 'MCG' , 'MAG' , 'MAC' } :
    word = 'MK' + word [ 3 : ]
elif word [ : 2 ] == 'MC' :
    word = 'MK' + word [ 2 : ]
# Rule 2 ( FL3)
pos = len ( word ) - 2
while pos > - 1 :
    if word [ pos : pos + 2 ] in { 'DT' , 'LD' , 'ND' , 'NT' , 'RC' , 'RD' , 'RT' , 'SC' , 'SK' , 'ST' , } :
        word = word [ : pos + 1 ] + word [ pos + 2 : ]
        pos += 1
    pos -= 1
# Rule 3 ( FL4)
# Although the rule indicates " after the first letter " , the test cases
# make it clear that these apply to the first letter also .
word = word . replace ( 'X' , 'KS' )
word = word . replace ( 'CE' , 'SE' )
word = word . replace ( 'CI' , 'SI' )
word = word . replace ( 'CY' , 'SI' )
# not in the rule set , but they seem to have intended it
word = word . replace ( 'TCH' , 'CH' )
pos = word . find ( 'CH' , 1 )
while pos != - 1 :
    if word [ pos - 1 : pos ] not in self . _uc_vy_set :
        word = word [ : pos ] + 'S' + word [ pos + 1 : ]
    pos = word . find ( 'CH' , pos + 1 )
word = word . replace ( 'C' , 'K' )
word = word . replace ( 'Z' , 'S' )
word = word . replace ( 'WR' , 'R' )
word = word . replace ( 'DG' , 'G' )
word = word . replace ( 'QU' , 'K' )
word = word . replace ( 'T' , 'D' )
word = word . replace ( 'PH' , 'F' )
# Rule 4 ( FL5)
# Although the rule indicates " after the first letter " , the test cases
# make it clear that these apply to the first letter also .
pos = word . find ( 'K' , 0 )
while pos != - 1 :
    if pos > 1 and word [ pos - 1 : pos ] not in self . _uc_vy_set | { 'L' , 'N' , 'R' , } :
        word = word [ : pos - 1 ] + word [ pos : ]
        pos -= 1
    pos = word . find ( 'K' , pos + 1 )
# Rule FL6
if max_length > 0 and word [ - 1 : ] == 'E' :
    word = word [ : - 1 ]
# Rule 5 ( FL7)
word = self . _delete_consecutive_repeats ( word )
# Rule 6 ( FL8)
if word [ : 2 ] == 'PF' :
    word = word [ 1 : ]
if word [ - 2 : ] == 'PF' :
    word = word [ : - 1 ]
elif word [ - 2 : ] == 'GH' :
    if word [ - 3 : - 2 ] in self . _uc_vy_set :
        word = word [ : - 2 ] + 'F'
    else :
        word = word [ : - 2 ] + 'G'
word = word . replace ( 'GH' , '' )
# Rule FL9
if max_length > 0 :
    word = word . replace ( 'V' , 'F' )
# Rules 7-9 ( FL10 - FL12)
first = 1 + ( 1 if max_length > 0 else 0 )
code = ''
for pos , char in enumerate ( word ) :
    if char in self . _uc_vy_set :
        if first or keep_vowels :
            code += vowel_char
            first -= 1
    elif pos > 0 and char in { 'W' , 'H' } :
        continue
    else :
        code += char
if max_length > 0 : # Rule FL13
    if len ( code ) > max_length and code [ - 1 : ] == 'S' :
        code = code [ : - 1 ]
    if keep_vowels :
        code = code [ : max_length ]
    else : # Rule FL14
        code = code [ : max_length + 2 ]
        # Rule FL15
        while len ( code ) > max_length :
            vowels = len ( code ) - max_length
            excess = vowels - 1
            word = code
            code = ''
            for char in word :
                if char == vowel_char :
                    if vowels :
                        code += char
                        vowels -= 1
                else :
                    code += char
            code = code [ : max_length + excess ]
    # Rule FL16
    code += ' ' * ( max_length - len ( code ) )
return code


signature:
def pshp_soundex_last ( lname , max_length = 4 , german = False ) :
    """Calculate the PSHP Soundex / Viewex Coding of a last name .
 This is a wrapper for : py : meth : ` PSHPSoundexLast . encode ` .
 Parameters
 lname : str
 The last name to encode
 max _ length : int
 The length of the code returned ( defaults to 4)
 german : bool
 Set to True if the name is German ( different rules apply )
 Returns
 str
 The PSHP Soundex / Viewex Coding
 Examples
 > > > pshp _ soundex _ last ( ' Smith ' )
 ' S530'
 > > > pshp _ soundex _ last ( ' Waters ' )
 ' W350'
 > > > pshp _ soundex _ last ( ' James ' )
 ' J500'
 > > > pshp _ soundex _ last ( ' Schmidt ' )
 ' S530'
 > > > pshp _ soundex _ last ( ' Ashcroft ' )
 ' A225'"""

implementation: return PSHPSoundexLast ( ) . encode ( lname , max_length , german )


signature:
def encode ( self , lname , max_length = 4 , german = False ) :
    """Calculate the PSHP Soundex / Viewex Coding of a last name .
 Parameters
 lname : str
 The last name to encode
 max _ length : int
 The length of the code returned ( defaults to 4)
 german : bool
 Set to True if the name is German ( different rules apply )
 Returns
 str
 The PSHP Soundex / Viewex Coding
 Examples
 > > > pe = PSHPSoundexLast ( )
 > > > pe . encode ( ' Smith ' )
 ' S530'
 > > > pe . encode ( ' Waters ' )
 ' W350'
 > > > pe . encode ( ' James ' )
 ' J500'
 > > > pe . encode ( ' Schmidt ' )
 ' S530'
 > > > pe . encode ( ' Ashcroft ' )
 ' A225'"""

implementation:
lname = unicode_normalize ( 'NFKD' , text_type ( lname . upper ( ) ) )
lname = lname . replace ( 'ß' , 'SS' )
lname = '' . join ( c for c in lname if c in self . _uc_set )
# A . Prefix treatment
if lname [ : 3 ] == 'VON' or lname [ : 3 ] == 'VAN' :
    lname = lname [ 3 : ] . strip ( )
# The rule implemented below says " MC , MAC become 1 " . I believe it
# meant to say they become M except in German data ( where superscripted
# 1 indicates " except in German data " ) . It doesn ' t make sense for them
# to become 1 ( BPFV - > 1 ) or to apply outside German . Unfortunately ,
# both articles have this error ( ? ) .
if not german :
    if lname [ : 3 ] == 'MAC' :
        lname = 'M' + lname [ 3 : ]
    elif lname [ : 2 ] == 'MC' :
        lname = 'M' + lname [ 2 : ]
# The non - German - only rule to strip ' is unnecessary due to filtering
if lname [ : 1 ] in { 'E' , 'I' , 'O' , 'U' } :
    lname = 'A' + lname [ 1 : ]
elif lname [ : 2 ] in { 'GE' , 'GI' , 'GY' } :
    lname = 'J' + lname [ 1 : ]
elif lname [ : 2 ] in { 'CE' , 'CI' , 'CY' } :
    lname = 'S' + lname [ 1 : ]
elif lname [ : 3 ] == 'CHR' :
    lname = 'K' + lname [ 1 : ]
elif lname [ : 1 ] == 'C' and lname [ : 2 ] != 'CH' :
    lname = 'K' + lname [ 1 : ]
if lname [ : 2 ] == 'KN' :
    lname = 'N' + lname [ 1 : ]
elif lname [ : 2 ] == 'PH' :
    lname = 'F' + lname [ 1 : ]
elif lname [ : 3 ] in { 'WIE' , 'WEI' } :
    lname = 'V' + lname [ 1 : ]
if german and lname [ : 1 ] in { 'W' , 'M' , 'Y' , 'Z' } :
    lname = { 'W' : 'V' , 'M' : 'N' , 'Y' : 'J' , 'Z' : 'S' } [ lname [ 0 ] ] + lname [ 1 : ]
code = lname [ : 1 ]
# B . Postfix treatment
if german : # moved from end of postfix treatment due to blocking
    if lname [ - 3 : ] == 'TES' :
        lname = lname [ : - 3 ]
    elif lname [ - 2 : ] == 'TS' :
        lname = lname [ : - 2 ]
    if lname [ - 3 : ] == 'TZE' :
        lname = lname [ : - 3 ]
    elif lname [ - 2 : ] == 'ZE' :
        lname = lname [ : - 2 ]
    if lname [ - 1 : ] == 'Z' :
        lname = lname [ : - 1 ]
    elif lname [ - 2 : ] == 'TE' :
        lname = lname [ : - 2 ]
if lname [ - 1 : ] == 'R' :
    lname = lname [ : - 1 ] + 'N'
elif lname [ - 2 : ] in { 'SE' , 'CE' } :
    lname = lname [ : - 2 ]
if lname [ - 2 : ] == 'SS' :
    lname = lname [ : - 2 ]
elif lname [ - 1 : ] == 'S' :
    lname = lname [ : - 1 ]
if not german :
    l5_repl = { 'STOWN' : 'SAWON' , 'MPSON' : 'MASON' }
    l4_repl = { 'NSEN' : 'ASEN' , 'MSON' : 'ASON' , 'STEN' : 'SAEN' , 'STON' : 'SAON' , }
    if lname [ - 5 : ] in l5_repl :
        lname = lname [ : - 5 ] + l5_repl [ lname [ - 5 : ] ]
    elif lname [ - 4 : ] in l4_repl :
        lname = lname [ : - 4 ] + l4_repl [ lname [ - 4 : ] ]
if lname [ - 2 : ] in { 'NG' , 'ND' } :
    lname = lname [ : - 1 ]
if not german and lname [ - 3 : ] in { 'GAN' , 'GEN' } :
    lname = lname [ : - 3 ] + 'A' + lname [ - 2 : ]
# C . Infix Treatment
lname = lname . replace ( 'CK' , 'C' )
lname = lname . replace ( 'SCH' , 'S' )
lname = lname . replace ( 'DT' , 'T' )
lname = lname . replace ( 'ND' , 'N' )
lname = lname . replace ( 'NG' , 'N' )
lname = lname . replace ( 'LM' , 'M' )
lname = lname . replace ( 'MN' , 'M' )
lname = lname . replace ( 'WIE' , 'VIE' )
lname = lname . replace ( 'WEI' , 'VEI' )
# D . Soundexing
# code for X & Y are unspecified , but presumably are 2 & 0
lname = lname . translate ( self . _trans )
lname = self . _delete_consecutive_repeats ( lname )
code += lname [ 1 : ]
code = code . replace ( '0' , '' )
# rule 1
if max_length != - 1 :
    if len ( code ) < max_length :
        code += '0' * ( max_length - len ( code ) )
    else :
        code = code [ : max_length ]
return code


signature:
def fingerprint ( self , word ) :
    """Return the skeleton key .
 Parameters
 word : str
 The word to transform into its skeleton key
 Returns
 str
 The skeleton key
 Examples
 > > > sk = SkeletonKey ( )
 > > > sk . fingerprint ( ' The quick brown fox jumped over the lazy dog . ' )
 ' THQCKBRWNFXJMPDVLZYGEUIOA '
 > > > sk . fingerprint ( ' Christopher ' )
 ' CHRSTPIOE '
 > > > sk . fingerprint ( ' Niall ' )
 ' NLIA '"""

implementation:
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = '' . join ( c for c in word if c in self . _letters )
start = word [ 0 : 1 ]
consonant_part = ''
vowel_part = ''
# add consonants & vowels to to separate strings
# ( omitting the first char & duplicates )
for char in word [ 1 : ] :
    if char != start :
        if char in self . _vowels :
            if char not in vowel_part :
                vowel_part += char
        elif char not in consonant_part :
            consonant_part += char
# return the first char followed by consonants followed by vowels
return start + consonant_part + vowel_part


signature:
def nysiis ( word , max_length = 6 , modified = False ) :
    """Return the NYSIIS code for a word .
 This is a wrapper for : py : meth : ` NYSIIS . encode ` .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The maximum length ( default 6 ) of the code to return
 modified : bool
 Indicates whether to use USDA modified NYSIIS
 Returns
 str
 The NYSIIS value
 Examples
 > > > nysiis ( ' Christopher ' )
 ' CRASTA '
 > > > nysiis ( ' Niall ' )
 ' NAL '
 > > > nysiis ( ' Smith ' )
 ' SNAT '
 > > > nysiis ( ' Schmidt ' )
 ' SNAD '
 > > > nysiis ( ' Christopher ' , max _ length = - 1)
 ' CRASTAFAR '
 > > > nysiis ( ' Christopher ' , max _ length = 8 , modified = True )
 ' CRASTAFA '
 > > > nysiis ( ' Niall ' , max _ length = 8 , modified = True )
 ' NAL '
 > > > nysiis ( ' Smith ' , max _ length = 8 , modified = True )
 ' SNAT '
 > > > nysiis ( ' Schmidt ' , max _ length = 8 , modified = True )
 ' SNAD '"""

implementation: return NYSIIS ( ) . encode ( word , max_length , modified )


signature:
def encode ( self , word , max_length = 6 , modified = False ) :
    """Return the NYSIIS code for a word .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The maximum length ( default 6 ) of the code to return
 modified : bool
 Indicates whether to use USDA modified NYSIIS
 Returns
 str
 The NYSIIS value
 Examples
 > > > pe = NYSIIS ( )
 > > > pe . encode ( ' Christopher ' )
 ' CRASTA '
 > > > pe . encode ( ' Niall ' )
 ' NAL '
 > > > pe . encode ( ' Smith ' )
 ' SNAT '
 > > > pe . encode ( ' Schmidt ' )
 ' SNAD '
 > > > pe . encode ( ' Christopher ' , max _ length = - 1)
 ' CRASTAFAR '
 > > > pe . encode ( ' Christopher ' , max _ length = 8 , modified = True )
 ' CRASTAFA '
 > > > pe . encode ( ' Niall ' , max _ length = 8 , modified = True )
 ' NAL '
 > > > pe . encode ( ' Smith ' , max _ length = 8 , modified = True )
 ' SNAT '
 > > > pe . encode ( ' Schmidt ' , max _ length = 8 , modified = True )
 ' SNAD '"""

implementation:
# Require a max _ length of at least 6
if max_length > - 1 :
    max_length = max ( 6 , max_length )
word = '' . join ( c for c in word . upper ( ) if c . isalpha ( ) )
word = word . replace ( 'ß' , 'SS' )
# exit early if there are no alphas
if not word :
    return ''
original_first_char = word [ 0 ]
if word [ : 3 ] == 'MAC' :
    word = 'MCC' + word [ 3 : ]
elif word [ : 2 ] == 'KN' :
    word = 'NN' + word [ 2 : ]
elif word [ : 1 ] == 'K' :
    word = 'C' + word [ 1 : ]
elif word [ : 2 ] in { 'PH' , 'PF' } :
    word = 'FF' + word [ 2 : ]
elif word [ : 3 ] == 'SCH' :
    word = 'SSS' + word [ 3 : ]
elif modified :
    if word [ : 2 ] == 'WR' :
        word = 'RR' + word [ 2 : ]
    elif word [ : 2 ] == 'RH' :
        word = 'RR' + word [ 2 : ]
    elif word [ : 2 ] == 'DG' :
        word = 'GG' + word [ 2 : ]
    elif word [ : 1 ] in self . _uc_v_set :
        word = 'A' + word [ 1 : ]
if modified and word [ - 1 : ] in { 'S' , 'Z' } :
    word = word [ : - 1 ]
if ( word [ - 2 : ] == 'EE' or word [ - 2 : ] == 'IE' or ( modified and word [ - 2 : ] == 'YE' ) ) :
    word = word [ : - 2 ] + 'Y'
elif word [ - 2 : ] in { 'DT' , 'RT' , 'RD' } :
    word = word [ : - 2 ] + 'D'
elif word [ - 2 : ] in { 'NT' , 'ND' } :
    word = word [ : - 2 ] + ( 'N' if modified else 'D' )
elif modified :
    if word [ - 2 : ] == 'IX' :
        word = word [ : - 2 ] + 'ICK'
    elif word [ - 2 : ] == 'EX' :
        word = word [ : - 2 ] + 'ECK'
    elif word [ - 2 : ] in { 'JR' , 'SR' } :
        return 'ERROR'
key = word [ : 1 ]
skip = 0
for i in range ( 1 , len ( word ) ) :
    if i >= len ( word ) :
        continue
    elif skip :
        skip -= 1
        continue
    elif word [ i : i + 2 ] == 'EV' :
        word = word [ : i ] + 'AF' + word [ i + 2 : ]
        skip = 1
    elif word [ i ] in self . _uc_v_set :
        word = word [ : i ] + 'A' + word [ i + 1 : ]
    elif modified and i != len ( word ) - 1 and word [ i ] == 'Y' :
        word = word [ : i ] + 'A' + word [ i + 1 : ]
    elif word [ i ] == 'Q' :
        word = word [ : i ] + 'G' + word [ i + 1 : ]
    elif word [ i ] == 'Z' :
        word = word [ : i ] + 'S' + word [ i + 1 : ]
    elif word [ i ] == 'M' :
        word = word [ : i ] + 'N' + word [ i + 1 : ]
    elif word [ i : i + 2 ] == 'KN' :
        word = word [ : i ] + 'N' + word [ i + 2 : ]
    elif word [ i ] == 'K' :
        word = word [ : i ] + 'C' + word [ i + 1 : ]
    elif modified and i == len ( word ) - 3 and word [ i : i + 3 ] == 'SCH' :
        word = word [ : i ] + 'SSA'
        skip = 2
    elif word [ i : i + 3 ] == 'SCH' :
        word = word [ : i ] + 'SSS' + word [ i + 3 : ]
        skip = 2
    elif modified and i == len ( word ) - 2 and word [ i : i + 2 ] == 'SH' :
        word = word [ : i ] + 'SA'
        skip = 1
    elif word [ i : i + 2 ] == 'SH' :
        word = word [ : i ] + 'SS' + word [ i + 2 : ]
        skip = 1
    elif word [ i : i + 2 ] == 'PH' :
        word = word [ : i ] + 'FF' + word [ i + 2 : ]
        skip = 1
    elif modified and word [ i : i + 3 ] == 'GHT' :
        word = word [ : i ] + 'TTT' + word [ i + 3 : ]
        skip = 2
    elif modified and word [ i : i + 2 ] == 'DG' :
        word = word [ : i ] + 'GG' + word [ i + 2 : ]
        skip = 1
    elif modified and word [ i : i + 2 ] == 'WR' :
        word = word [ : i ] + 'RR' + word [ i + 2 : ]
        skip = 1
    elif word [ i ] == 'H' and ( word [ i - 1 ] not in self . _uc_v_set or word [ i + 1 : i + 2 ] not in self . _uc_v_set ) :
        word = word [ : i ] + word [ i - 1 ] + word [ i + 1 : ]
    elif word [ i ] == 'W' and word [ i - 1 ] in self . _uc_v_set :
        word = word [ : i ] + word [ i - 1 ] + word [ i + 1 : ]
    if word [ i : i + skip + 1 ] != key [ - 1 : ] :
        key += word [ i : i + skip + 1 ]
key = self . _delete_consecutive_repeats ( key )
if key [ - 1 : ] == 'S' :
    key = key [ : - 1 ]
if key [ - 2 : ] == 'AY' :
    key = key [ : - 2 ] + 'Y'
if key [ - 1 : ] == 'A' :
    key = key [ : - 1 ]
if modified and key [ : 1 ] == 'A' :
    key = original_first_char + key [ 1 : ]
if max_length > 0 :
    key = key [ : max_length ]
return key


signature:
def chebyshev ( src , tar , qval = 2 , alphabet = None ) :
    r"""Return the Chebyshev distance between two strings .
 This is a wrapper for the : py : meth : ` Chebyshev . dist _ abs ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version alphabet
 alphabet : collection or int
 The values or size of the alphabet
 Returns
 float
 The Chebyshev distance
 Examples
 > > > chebyshev ( ' cat ' , ' hat ' )
 1.0
 > > > chebyshev ( ' Niall ' , ' Neil ' )
 1.0
 > > > chebyshev ( ' Colin ' , ' Cuilen ' )
 1.0
 > > > chebyshev ( ' ATCG ' , ' TAGC ' )
 1.0
 > > > chebyshev ( ' ATCG ' , ' TAGC ' , qval = 1)
 0.0
 > > > chebyshev ( ' ATCGATTCGGAATTTC ' , ' TAGCATAATCGCCG ' , qval = 1)
 3.0"""

implementation: return Chebyshev ( ) . dist_abs ( src , tar , qval , alphabet )


signature:
def dist_abs ( self , src , tar , qval = 2 , alphabet = None ) :
    r"""Return the Chebyshev distance between two strings .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version alphabet
 alphabet : collection or int
 The values or size of the alphabet
 Returns
 float
 The Chebyshev distance
 Examples
 > > > cmp = Chebyshev ( )
 > > > cmp . dist _ abs ( ' cat ' , ' hat ' )
 1.0
 > > > cmp . dist _ abs ( ' Niall ' , ' Neil ' )
 1.0
 > > > cmp . dist _ abs ( ' Colin ' , ' Cuilen ' )
 1.0
 > > > cmp . dist _ abs ( ' ATCG ' , ' TAGC ' )
 1.0
 > > > cmp . dist _ abs ( ' ATCG ' , ' TAGC ' , qval = 1)
 0.0
 > > > cmp . dist _ abs ( ' ATCGATTCGGAATTTC ' , ' TAGCATAATCGCCG ' , qval = 1)
 3.0"""

implementation: return super ( self . __class__ , self ) . dist_abs ( src , tar , qval , float ( 'inf' ) , False , alphabet )


signature:
def mean_pairwise_similarity ( collection , metric = sim , mean_func = hmean , symmetric = False ) :
    """Calculate the mean pairwise similarity of a collection of strings .
 Takes the mean of the pairwise similarity between each member of a
 collection , optionally in both directions ( for asymmetric similarity
 metrics .
 Parameters
 collection : list
 A collection of terms or a string that can be split
 metric : function
 A similarity metric function
 mean _ func : function
 A mean function that takes a list of values and returns a float
 symmetric : bool
 Set to True if all pairwise similarities should be calculated in both
 directions
 Returns
 float
 The mean pairwise similarity of a collection of strings
 Raises
 ValueError
 mean _ func must be a function
 ValueError
 metric must be a function
 ValueError
 collection is neither a string nor iterable type
 ValueError
 collection has fewer than two members
 Examples
 > > > round ( mean _ pairwise _ similarity ( [ ' Christopher ' , ' Kristof ' ,
 . . . ' Christobal ' ] ) , 12)
 0.519801980198
 > > > round ( mean _ pairwise _ similarity ( [ ' Niall ' , ' Neal ' , ' Neil ' ] ) , 12)
 0.545454545455"""

implementation:
if not callable ( mean_func ) :
    raise ValueError ( 'mean_func must be a function' )
if not callable ( metric ) :
    raise ValueError ( 'metric must be a function' )
if hasattr ( collection , 'split' ) :
    collection = collection . split ( )
if not hasattr ( collection , '__iter__' ) :
    raise ValueError ( 'collection is neither a string nor iterable type' )
elif len ( collection ) < 2 :
    raise ValueError ( 'collection has fewer than two members' )
collection = list ( collection )
pairwise_values = [ ]
for i in range ( len ( collection ) ) :
    for j in range ( i + 1 , len ( collection ) ) :
        pairwise_values . append ( metric ( collection [ i ] , collection [ j ] ) )
        if symmetric :
            pairwise_values . append ( metric ( collection [ j ] , collection [ i ] ) )
return mean_func ( pairwise_values )


signature:
def pairwise_similarity_statistics ( src_collection , tar_collection , metric = sim , mean_func = amean , symmetric = False , ) :
    """Calculate the pairwise similarity statistics a collection of strings .
 Calculate pairwise similarities among members of two collections ,
 returning the maximum , minimum , mean ( according to a supplied function ,
 arithmetic mean , by default ) , and ( population ) standard deviation
 of those similarities .
 Parameters
 src _ collection : list
 A collection of terms or a string that can be split
 tar _ collection : list
 A collection of terms or a string that can be split
 metric : function
 A similarity metric function
 mean _ func : function
 A mean function that takes a list of values and returns a float
 symmetric : bool
 Set to True if all pairwise similarities should be calculated in both
 directions
 Returns
 tuple
 The max , min , mean , and standard deviation of similarities
 Raises
 ValueError
 mean _ func must be a function
 ValueError
 metric must be a function
 ValueError
 src _ collection is neither a string nor iterable
 ValueError
 tar _ collection is neither a string nor iterable
 Example
 > > > tuple ( round ( _ , 12 ) for _ in pairwise _ similarity _ statistics (
 . . . [ ' Christopher ' , ' Kristof ' , ' Christobal ' ] , [ ' Niall ' , ' Neal ' , ' Neil ' ] ) )
 (0.2 , 0.0 , 0.118614718615 , 0.075070477184)"""

implementation:
if not callable ( mean_func ) :
    raise ValueError ( 'mean_func must be a function' )
if not callable ( metric ) :
    raise ValueError ( 'metric must be a function' )
if hasattr ( src_collection , 'split' ) :
    src_collection = src_collection . split ( )
if not hasattr ( src_collection , '__iter__' ) :
    raise ValueError ( 'src_collection is neither a string nor iterable' )
if hasattr ( tar_collection , 'split' ) :
    tar_collection = tar_collection . split ( )
if not hasattr ( tar_collection , '__iter__' ) :
    raise ValueError ( 'tar_collection is neither a string nor iterable' )
src_collection = list ( src_collection )
tar_collection = list ( tar_collection )
pairwise_values = [ ]
for src in src_collection :
    for tar in tar_collection :
        pairwise_values . append ( metric ( src , tar ) )
        if symmetric :
            pairwise_values . append ( metric ( tar , src ) )
return ( max ( pairwise_values ) , min ( pairwise_values ) , mean_func ( pairwise_values ) , std ( pairwise_values , mean_func , 0 ) , )


signature:
def stem ( self , word , early_english = False ) :
    """Return the Porter2 ( Snowball English ) stem .
 Parameters
 word : str
 The word to stem
 early _ english : bool
 Set to True in order to remove - eth & - est ( 2nd & 3rd person
 singular verbal agreement suffixes )
 Returns
 str
 Word stem
 Examples
 > > > stmr = Porter2 ( )
 > > > stmr . stem ( ' reading ' )
 ' read '
 > > > stmr . stem ( ' suspension ' )
 ' suspens '
 > > > stmr . stem ( ' elusiveness ' )
 ' elus '
 > > > stmr . stem ( ' eateth ' , early _ english = True )
 ' eat '"""

implementation:
# lowercase , normalize , and compose
word = normalize ( 'NFC' , text_type ( word . lower ( ) ) )
# replace apostrophe - like characters with U + 0027 , per
# http : / / snowball . tartarus . org / texts / apostrophe . html
word = word . replace ( '’' , '\'' )
word = word . replace ( '’' , '\'' )
# Exceptions 1
if word in self . _exception1dict :
    return self . _exception1dict [ word ]
elif word in self . _exception1set :
    return word
# Return word if stem is shorter than 3
if len ( word ) < 3 :
    return word
# Remove initial ' , if present .
while word and word [ 0 ] == '\'' :
    word = word [ 1 : ]
    # Return word if stem is shorter than 2
    if len ( word ) < 2 :
        return word
# Re - map vocalic Y to y ( Y will be C , y will be V )
if word [ 0 ] == 'y' :
    word = 'Y' + word [ 1 : ]
for i in range ( 1 , len ( word ) ) :
    if word [ i ] == 'y' and word [ i - 1 ] in self . _vowels :
        word = word [ : i ] + 'Y' + word [ i + 1 : ]
r1_start = self . _sb_r1 ( word , self . _r1_prefixes )
r2_start = self . _sb_r2 ( word , self . _r1_prefixes )
# Step 0
if word [ - 3 : ] == '\'s\'' :
    word = word [ : - 3 ]
elif word [ - 2 : ] == '\'s' :
    word = word [ : - 2 ]
elif word [ - 1 : ] == '\'' :
    word = word [ : - 1 ]
# Return word if stem is shorter than 2
if len ( word ) < 3 :
    return word
# Step 1a
if word [ - 4 : ] == 'sses' :
    word = word [ : - 2 ]
elif word [ - 3 : ] in { 'ied' , 'ies' } :
    if len ( word ) > 4 :
        word = word [ : - 2 ]
    else :
        word = word [ : - 1 ]
elif word [ - 2 : ] in { 'us' , 'ss' } :
    pass
elif word [ - 1 ] == 's' :
    if self . _sb_has_vowel ( word [ : - 2 ] ) :
        word = word [ : - 1 ]
# Exceptions 2
if word in self . _exception2set :
    return word
# Step 1b
step1b_flag = False
if word [ - 5 : ] == 'eedly' :
    if len ( word [ r1_start : ] ) >= 5 :
        word = word [ : - 3 ]
elif word [ - 5 : ] == 'ingly' :
    if self . _sb_has_vowel ( word [ : - 5 ] ) :
        word = word [ : - 5 ]
        step1b_flag = True
elif word [ - 4 : ] == 'edly' :
    if self . _sb_has_vowel ( word [ : - 4 ] ) :
        word = word [ : - 4 ]
        step1b_flag = True
elif word [ - 3 : ] == 'eed' :
    if len ( word [ r1_start : ] ) >= 3 :
        word = word [ : - 1 ]
elif word [ - 3 : ] == 'ing' :
    if self . _sb_has_vowel ( word [ : - 3 ] ) :
        word = word [ : - 3 ]
        step1b_flag = True
elif word [ - 2 : ] == 'ed' :
    if self . _sb_has_vowel ( word [ : - 2 ] ) :
        word = word [ : - 2 ]
        step1b_flag = True
elif early_english :
    if word [ - 3 : ] == 'est' :
        if self . _sb_has_vowel ( word [ : - 3 ] ) :
            word = word [ : - 3 ]
            step1b_flag = True
    elif word [ - 3 : ] == 'eth' :
        if self . _sb_has_vowel ( word [ : - 3 ] ) :
            word = word [ : - 3 ]
            step1b_flag = True
if step1b_flag :
    if word [ - 2 : ] in { 'at' , 'bl' , 'iz' } :
        word += 'e'
    elif word [ - 2 : ] in self . _doubles :
        word = word [ : - 1 ]
    elif self . _sb_short_word ( word , self . _r1_prefixes ) :
        word += 'e'
# Step 1c
if ( len ( word ) > 2 and word [ - 1 ] in { 'Y' , 'y' } and word [ - 2 ] not in self . _vowels ) :
    word = word [ : - 1 ] + 'i'
# Step 2
if word [ - 2 ] == 'a' :
    if word [ - 7 : ] == 'ational' :
        if len ( word [ r1_start : ] ) >= 7 :
            word = word [ : - 5 ] + 'e'
    elif word [ - 6 : ] == 'tional' :
        if len ( word [ r1_start : ] ) >= 6 :
            word = word [ : - 2 ]
elif word [ - 2 ] == 'c' :
    if word [ - 4 : ] in { 'enci' , 'anci' } :
        if len ( word [ r1_start : ] ) >= 4 :
            word = word [ : - 1 ] + 'e'
elif word [ - 2 ] == 'e' :
    if word [ - 4 : ] == 'izer' :
        if len ( word [ r1_start : ] ) >= 4 :
            word = word [ : - 1 ]
elif word [ - 2 ] == 'g' :
    if word [ - 3 : ] == 'ogi' :
        if ( r1_start >= 1 and len ( word [ r1_start : ] ) >= 3 and word [ - 4 ] == 'l' ) :
            word = word [ : - 1 ]
elif word [ - 2 ] == 'l' :
    if word [ - 6 : ] == 'lessli' :
        if len ( word [ r1_start : ] ) >= 6 :
            word = word [ : - 2 ]
    elif word [ - 5 : ] in { 'entli' , 'fulli' , 'ousli' } :
        if len ( word [ r1_start : ] ) >= 5 :
            word = word [ : - 2 ]
    elif word [ - 4 : ] == 'abli' :
        if len ( word [ r1_start : ] ) >= 4 :
            word = word [ : - 1 ] + 'e'
    elif word [ - 4 : ] == 'alli' :
        if len ( word [ r1_start : ] ) >= 4 :
            word = word [ : - 2 ]
    elif word [ - 3 : ] == 'bli' :
        if len ( word [ r1_start : ] ) >= 3 :
            word = word [ : - 1 ] + 'e'
    elif word [ - 2 : ] == 'li' :
        if ( r1_start >= 1 and len ( word [ r1_start : ] ) >= 2 and word [ - 3 ] in self . _li ) :
            word = word [ : - 2 ]
elif word [ - 2 ] == 'o' :
    if word [ - 7 : ] == 'ization' :
        if len ( word [ r1_start : ] ) >= 7 :
            word = word [ : - 5 ] + 'e'
    elif word [ - 5 : ] == 'ation' :
        if len ( word [ r1_start : ] ) >= 5 :
            word = word [ : - 3 ] + 'e'
    elif word [ - 4 : ] == 'ator' :
        if len ( word [ r1_start : ] ) >= 4 :
            word = word [ : - 2 ] + 'e'
elif word [ - 2 ] == 's' :
    if word [ - 7 : ] in { 'fulness' , 'ousness' , 'iveness' } :
        if len ( word [ r1_start : ] ) >= 7 :
            word = word [ : - 4 ]
    elif word [ - 5 : ] == 'alism' :
        if len ( word [ r1_start : ] ) >= 5 :
            word = word [ : - 3 ]
elif word [ - 2 ] == 't' :
    if word [ - 6 : ] == 'biliti' :
        if len ( word [ r1_start : ] ) >= 6 :
            word = word [ : - 5 ] + 'le'
    elif word [ - 5 : ] == 'aliti' :
        if len ( word [ r1_start : ] ) >= 5 :
            word = word [ : - 3 ]
    elif word [ - 5 : ] == 'iviti' :
        if len ( word [ r1_start : ] ) >= 5 :
            word = word [ : - 3 ] + 'e'
# Step 3
if word [ - 7 : ] == 'ational' :
    if len ( word [ r1_start : ] ) >= 7 :
        word = word [ : - 5 ] + 'e'
elif word [ - 6 : ] == 'tional' :
    if len ( word [ r1_start : ] ) >= 6 :
        word = word [ : - 2 ]
elif word [ - 5 : ] in { 'alize' , 'icate' , 'iciti' } :
    if len ( word [ r1_start : ] ) >= 5 :
        word = word [ : - 3 ]
elif word [ - 5 : ] == 'ative' :
    if len ( word [ r2_start : ] ) >= 5 :
        word = word [ : - 5 ]
elif word [ - 4 : ] == 'ical' :
    if len ( word [ r1_start : ] ) >= 4 :
        word = word [ : - 2 ]
elif word [ - 4 : ] == 'ness' :
    if len ( word [ r1_start : ] ) >= 4 :
        word = word [ : - 4 ]
elif word [ - 3 : ] == 'ful' :
    if len ( word [ r1_start : ] ) >= 3 :
        word = word [ : - 3 ]
# Step 4
for suffix in ( 'ement' , 'ance' , 'ence' , 'able' , 'ible' , 'ment' , 'ant' , 'ent' , 'ism' , 'ate' , 'iti' , 'ous' , 'ive' , 'ize' , 'al' , 'er' , 'ic' , ) :
    if word [ - len ( suffix ) : ] == suffix :
        if len ( word [ r2_start : ] ) >= len ( suffix ) :
            word = word [ : - len ( suffix ) ]
        break
else :
    if word [ - 3 : ] == 'ion' :
        if ( len ( word [ r2_start : ] ) >= 3 and len ( word ) >= 4 and word [ - 4 ] in tuple ( 'st' ) ) :
            word = word [ : - 3 ]
# Step 5
if word [ - 1 ] == 'e' :
    if len ( word [ r2_start : ] ) >= 1 or ( len ( word [ r1_start : ] ) >= 1 and not self . _sb_ends_in_short_syllable ( word [ : - 1 ] ) ) :
        word = word [ : - 1 ]
elif word [ - 1 ] == 'l' :
    if len ( word [ r2_start : ] ) >= 1 and word [ - 2 ] == 'l' :
        word = word [ : - 1 ]
# Change ' Y ' back to ' y ' if it survived stemming
for i in range ( 0 , len ( word ) ) :
    if word [ i ] == 'Y' :
        word = word [ : i ] + 'y' + word [ i + 1 : ]
return word


signature:
def synoname_toolcode ( lname , fname = '' , qual = '' , normalize = 0 ) :
    """Build the Synoname toolcode .
 This is a wrapper for : py : meth : ` SynonameToolcode . fingerprint ` .
 Parameters
 lname : str
 Last name
 fname : str
 First name ( can be blank )
 qual : str
 Qualifier
 normalize : int
 Normalization mode ( 0 , 1 , or 2)
 Returns
 tuple
 The transformed names and the synoname toolcode
 Examples
 > > > synoname _ toolcode ( ' hat ' )
 ( ' hat ' , ' ' , ' 000003 $ $ h ' )
 > > > synoname _ toolcode ( ' niall ' )
 ( ' niall ' , ' ' , ' 000005 $ $ n ' )
 > > > synoname _ toolcode ( ' colin ' )
 ( ' colin ' , ' ' , ' 000005 $ $ c ' )
 > > > synoname _ toolcode ( ' atcg ' )
 ( ' atcg ' , ' ' , ' 000004 $ $ a ' )
 > > > synoname _ toolcode ( ' entreatment ' )
 ( ' entreatment ' , ' ' , ' 0000011 $ $ e ' )
 > > > synoname _ toolcode ( ' Ste . - Marie ' , ' Count John II ' , normalize = 2)
 ( ' ste . - marie ii ' , ' count john ' , ' 0200491310 $ 015b049a127c $ smcji ' )
 > > > synoname _ toolcode ( ' Michelangelo IV ' , ' ' , ' Workshop of ' )
 ( ' michelangelo iv ' , ' ' , ' 3000550015 $ 055b $ mi ' )"""

implementation: return SynonameToolcode ( ) . fingerprint ( lname , fname , qual , normalize )


signature:
def uealite ( word , max_word_length = 20 , max_acro_length = 8 , return_rule_no = False , var = 'standard' , ) :
    """Return UEA - Lite stem .
 This is a wrapper for : py : meth : ` UEALite . stem ` .
 Parameters
 word : str
 The word to stem
 max _ word _ length : int
 The maximum word length allowed
 max _ acro _ length : int
 The maximum acronym length allowed
 return _ rule _ no : bool
 If True , returns the stem along with rule number
 var : str
 Variant rules to use :
 - ` ` Adams ` ` to use Jason Adams ' rules
 - ` ` Perl ` ` to use the original Perl rules
 Returns
 str or ( str , int )
 Word stem
 Examples
 > > > uealite ( ' readings ' )
 ' read '
 > > > uealite ( ' insulted ' )
 ' insult '
 > > > uealite ( ' cussed ' )
 ' cuss '
 > > > uealite ( ' fancies ' )
 ' fancy '
 > > > uealite ( ' eroded ' )
 ' erode '"""

implementation: return UEALite ( ) . stem ( word , max_word_length , max_acro_length , return_rule_no , var )