Measure-StringDistance.ps1

#https://github.com/michaellwest/PowerShell-Modules/blob/master/CorpApps/Measure-StringDistance.ps1

function Measure-StringDistance {
    <#
        .SYNOPSIS
            Compute the distance between two strings using the Levenshtein distance formula.
         
        .DESCRIPTION
            Compute the distance between two strings using the Levenshtein distance formula.
        .PARAMETER Source
            The source string.
        .PARAMETER Compare
            The comparison string.
        .EXAMPLE
            PS C:\> Measure-StringDistance -Source "Michael" -Compare "Micheal"
            2
            There are two characters that are different, "a" and "e".
        .EXAMPLE
            PS C:\> Measure-StringDistance -Source "Michael" -Compare "Michal"
            1
            There is one character that is different, "e".
        .NOTES
            Author:
            Michael West
    #>


    [CmdletBinding()]
    [OutputType([int])]
    param (
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [string]$Source = "",
        [string]$Compare = ""
    )
    $n = $Source.Length;
    $m = $Compare.Length;
    $d = New-Object 'int[,]' $($n+1),$($m+1)
        
    if ($n -eq 0){
      return $m
    }
    if ($m -eq 0){
        return $n
    }

    for ([int]$i = 0; $i -le $n; $i++){
        $d[$i, 0] = $i
    }
    for ([int]$j = 0; $j -le $m; $j++){
        $d[0, $j] = $j
    }

    for ([int]$i = 1; $i -le $n; $i++){
        for ([int]$j = 1; $j -le $m; $j++){
            if ($Compare[$($j - 1)] -eq $Source[$($i - 1)]){
                $cost = 0
            }
            else{
                $cost = 1
            }
            $d[$i, $j] = [Math]::Min([Math]::Min($($d[$($i-1), $j] + 1), $($d[$i, $($j-1)] + 1)),$($d[$($i-1), $($j-1)]+$cost))
        }
    }
        
    return $d[$n, $m]
}

function Measure-StringDistance:::Example {
    Measure-StringDistance -Source "Michael" -Compare "Micheal"
    Measure-StringDistance -Source 'https://vm-ccpla-dev:8889/CORP.CCP.Core.Service.SSO/CORPSSOService.svc?singleWsdl' -Compare 'CORP.CCP.Core.Service.SSO/CORPSSOService.svc'
}