Functions/Select-ApproximateString.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function Select-ApproximateString {
    <#
        .SYNOPSIS
            Perform approximate string matching.
        .DESCRIPTION
            This function lets you perform string matching using
            different approximate string matching algorithms.
        .EXAMPLE
            Select-ApproximateString -Search $searchQuery -In $searchData
        .EXAMPLE
            $searchData | Select-ApproximateString $searchQuery -Algorithm HammingDistance -Tolerance Weak
        .INPUTS
            System.String
            System.String[]
        .OUTPUTS
            System.String
        .NOTES
            Author: Øyvind Kallstad
            Date: 08.07.2016
            Version: 1.0
        .LINK
            https://communary.wordpress.com/
            https://github.com/gravejester/Communary.PASM
    #>

    [CmdletBinding()]
    param (
        # The search query.
        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string] $Search,

        # The data you want to search through.
        [Parameter(Position = 1, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [Alias('In')]
        [string] $Data,

        # Algorithm to use for approximate string matching.
        [Parameter()]
        [PasmAlgorithm] $Algorithm = [PasmAlgorithm]::JaroWinklerDistance,

        # Matching tolerance.
        [Parameter()]
        [ValidateSet('Strong','Normal','Weak')]
        [string] $Tolerance = 'Normal',

        # Makes matches case-sensitive. By default, matches are not case-sensitive.
        [Parameter()]
        [switch] $CaseSensitive
    )

    BEGIN {
        switch ($Tolerance) {
            'Strong' { $toleranceValue = 80}
            'Normal' { $toleranceValue = 60}
            'Weak' { $toleranceValue = 40}
        }
    }

    PROCESS {
        foreach ($string in $Data) {
            $pasmScore = Get-PasmScore -String1 $Search -String2 $string -CaseSensitive:$CaseSensitive
            if ($pasmScore -ge $toleranceValue) {
                Write-Output $string
            }
        }
    }
}