Functions/Get-OverlapCoefficient.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
function Get-OverlapCoefficient {
    <#
        .SYNOPSIS
            Get the Overlap Coefficient of two strings.
        .DESCRIPTION
            The overlap coefficient is a similarity measure related to the Jaccard index that measures the overlap between two sets,
            and is defined as the size of the intersection divided by the smaller of the size of the two sets.
        .EXAMPLE
            Get-OverlapCoefficient 'Pennsylvania' 'Pencilvaneya'
        .LINK
            http://en.wikipedia.org/wiki/Overlap_coefficient
            https://communary.wordpress.com/
            https://github.com/gravejester/Communary.PASM
        .NOTES
            Author: Øyvind Kallstad
            Date: 03.11.2014
            Version: 1.0
            Dependencies: Get-Intersection
    #>

    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [ValidateNotNullorEmpty()]
        [string] $String1,

        [Parameter(Position = 1)]
        [ValidateNotNullorEmpty()]
        [string] $String2,

        [Parameter()]
        [switch] $CaseSensitive
    )

    if (-not($CaseSensitive)) {
        $String1 = $String1.ToLowerInvariant()
        $String2 = $String2.ToLowerInvariant()
    }

    try {
        $intersectionSize = (Get-Intersection $String1 $String2 -CaseSensitive:$CaseSensitive).Length
        $smallestStringSize = [Math]::Min($String1.Length,$String2.Length)

        Write-Output ($intersectionSize / $smallestStringSize)
    }

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