Functions/Get-JaccardDistance.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
function Get-JaccardDistance {
    <#
        .SYNOPSIS
            Get the Jaccard Distance between two strings.
        .DESCRIPTION
            The Jaccard distance, which measures dissimilarity between sample sets, is complementary to the
            Jaccard coefficient and is obtained by subtracting the Jaccard coefficient from 1, or, equivalently,
            by dividing the difference of the sizes of the union and the intersection of two sets by the size of the union
        .EXAMPLE
            Get-JaccardDistance 'karolin' 'kharolin'
        .LINK
            http://en.wikipedia.org/wiki/Jaccard_index
            https://communary.wordpress.com/
            https://github.com/gravejester/Communary.PASM
        .NOTES
            Author: Øyvind Kallstad
            Date: 03.11.2014
            Version: 1.0
            Dependencies: Get-JaccardIndex
    #>

    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory)]
        [ValidateNotNullOrEmpty()]
        $a,

        [Parameter(Position = 1, Mandatory)]
        [ValidateNotNullOrEmpty()]
        $b,

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

    try {
        # calculate the jaccard distance
        $jaccardDistance = 1 - (Get-JaccardIndex $a $b -CaseSensitive:$CaseSensitive)
        Write-Output $jaccardDistance
    }

    catch {
        Write-Warning $_.Exception.Message
    }
}