Functions/Get-FuzzyMatchScore.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<#
    2016// Øyvind Kallstad (@okallstad)
#>

function Get-FuzzyMatchScore {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [string] $Search,

        [Parameter(Position = 1)]
        [string] $String
    )

    $score = 100

    # Use approximate string matching to get some values needed to calculate the score of the result
    $longestCommonSubstring = Get-LongestCommonSubstring -String1 $String -String2 $Search
    $levenshteinDistance = Get-LevenshteinDistance -String1 $String -String2 $Search
    $commonPrefix = Get-CommonPrefix -String1 $String -String2 $Search

    # By running the result through this regex pattern we get the length of the match as well as the
    # the index of where the match starts. The shorter the match length and the index, the more
    # score will be added for the match.
    $regexMatchFilter = $Search.ToCharArray() -join '.*?'
    $match = Select-String -InputObject $String -Pattern $regexMatchFilter -AllMatches
    $matchLength = ($match.Matches | Sort-Object Length | Select-Object -First 1).Value.Length
    $matchIndex = ($match.Matches | Sort-Object Length | Select-Object -First 1).Index

    # Calculate score
    $score = $score - $levenshteinDistance
    $score = $score * $longestCommonSubstring.Length
    $score = $score - $matchLength
    $score = $score - $matchIndex

    if ($commonPrefix) {
        $score =  $score + $commonPrefix.Length
    }

    Write-Output $score
}