Get-TMVerbSynonym.ps1

<#PSScriptInfo
.DESCRIPTION
    The Get-TMVerbSynonym advanced function returns the synonyms for a verb, and indicates if they are approved verbs using Get-Verb.
 
.VERSION
    1.4
 
.GUID
    ad741aa7-9b1a-4a35-8f58-127abe6036c6
 
.AUTHOR
    Tommy Maynard @thetommymaynard
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


Function Get-TMVerbSynonym {
<#
.SYNOPSIS
    The Get-TMVerbSynonym advanced function returns the synonyms for a verb.
 
.DESCRIPTION
    The Get-TMVerbSynonym advanced function returns the synonyms for a verb, and indicates if they are approved verbs using Get-Verb. Additionally, if the verb is approved, it will indicate the group. This advanced function relies on the thesaurus at altervista.org.
 
.PARAMETER Verb
    This mandatory parameter is the verb for which the function will find synonyms.
 
.PARAMETER Key
    This parameter requires an API key parameter value to use this function. Versions 1.2 and greater include an API key, so there's no need to register for one.
 
.PARAMETER Approved
    This switch parameter ensures that the results are only approved verbs.
 
.EXAMPLE
    -------------------------- EXAMPLE 1 --------------------------
    PS > Get-TMVerbSynonym -Verb Launch | Format-Table -AutoSize
 
    Verb Synonym Group Approved Notes
    ---- ------- ----- -------- -----
    Launch Abolish False Antonym
    Launch Begin False
    Launch Commence False
    Launch Displace False
    Launch Establish False
    Launch Found False
    Launch Get Common True
    Launch Get Down False
    Launch Impel False
    Launch Move Common True
    Launch Open Common True
    Launch Open Up False
    Launch Plunge False
    Launch Propel False
    Launch Set About False
    Launch Set In Motion False
    Launch Set Out False
    Launch Set Up False
    Launch Smooth False
    Launch Smoothen False
    Launch Start Lifecycle True
    Launch Start Out False
 
    This example returns all the synonyms for the verb "launch."
 
.EXAMPLE
    -------------------------- EXAMPLE 2 --------------------------
    PS > Get-TMVerbSynonym -Verb Launch -Approved | Format-Table -AutoSize
 
    Verb Synonym Group Approved Notes
    ---- ------- ----- -------- -----
    Launch Get Common True
    Launch Move Common True
    Launch Open Common True
    Launch Start Lifecycle True
 
    This example returns only the synonyms for the verb "Launch" that are approved verbs. If there were no approved verbs, this example would return no results.
 
.EXAMPLE
    -------------------------- EXAMPLE 3 --------------------------
    PS> Get-TMVerbSynonym -Verb car | Format-Table -Autosize
    WARNING: The word "Car" may not have any verb synonyms.
 
    This example attempts to return synonyms for the word car. Since car cannot be used as a verb, it returns a warning message. This function only works when the word supplied can be used as a verb.
 
.EXAMPLE
    -------------------------- EXAMPLE 4 --------------------------
    PS> Get-TMVerbSynonym -Verb exit | Sort-Object Approved -Descending | Format-Table -AutoSize
 
    Verb Synonym Group Approved Notes
    ---- ------- ----- -------- -----
    Exit Move Common True
    Exit Enter Common True Antonym
    Exit Be Born False Antonym
    Exit Pop Off False
    Exit Play False
    Exit Perish False
    Exit Pass Away False
    Exit Pass False
    Exit Leave False
    Exit Kick The Bucket False
    Exit Go Out False
    Exit Go False
    Exit Give-Up The Ghost False
    Exit Get Out False
    Exit Expire False
    Exit Drop Dead False
    Exit Die Out False Related Term
    Exit Die Off False Related Term
    Exit Die Down False Related Term
    Exit Die False
    Exit Decease False
    Exit Croak False
    Exit Conk False
    Exit Choke False
    Exit Change State False
    Exit Cash In One's Chips False
    Exit Buy The Farm False
    Exit Snuff It False
    Exit Turn False
 
    This example returns synonyms for the verb "exit," and sorts the verbs by those that are approved. At the time of writing, this example only returned two approved verbs: Move and Enter. Enter is actually an antonym, and is indicated as such in the Notes property.
 
.NOTES
    Name: Get-TMVerbSynonym
    Author: Tommy Maynard
    Web: http://tommymaynard.com
    Last Edit: 06/09/2016 [1.3], 01/04/2017 [1.4]
    Version 1.2
        - Skipped 1.1
        - Included my key for http://thesaurus.altervista.org. This keeps from needing to register for a key.
        - Decreased number of spaces in help. Other help changes due to not needing to register for a key.
        - As API key is included, modified code to
    Version 1.3
        - Modified code to handle logic outside of the object creation time.
        - Added Group property: Indicates name of the verb's group when verb is approved.
        - Changed Approved string property of Yes and No, to $true and $false.
        - Rewrote help where necessary to indicate changes.
    Version 1.4
        - Changed -- to $null for properties that do not have a value.
        - Removed redundant Get-Verb execution, when checking if a synonym is approved (uses OutVariable and temporary variable).
        - Renamed $Approved to $ApprovedVerb due to introducing the Approved switch parameter.
        - Added Approved switch parameter to only return approved synonyms without Where-Object filtering.
        - Added hardcoded position parameter attribute to the Verb parameter.
        - Added verb supplied by user to output object; renamed Verb property used for synonym to Synonym. This creates a list by default; however, it will allow for the Verb parameter taking multiple verbs... version 1.5 perhaps.
        - Rewrote help where necessary to indicate changes.
        - Added *another* If statement, to ensure an object isn't created if the Verb and Synonym are the same: Get synonyms won't return Get; Start synonyms won't return Start.
#>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true,Position=0)]
        [string]$Verb,

        [Parameter()]
        [string]$Key = 'fkS0rTuZ62Duag0bYgwn',

        [Parameter()]
        [switch]$Approved
    )

    Begin {
        # Modify case: Capitalize first letter.
        $Verb = (Get-Culture).TextInfo.ToTitleCase($Verb)

        # Obtain thesaurus information on verb.
        try {
            Write-Verbose -Message "Downloading synonyms for $Verb."
            [xml]$Data = (Invoke-WebRequest -Uri "http://thesaurus.altervista.org/thesaurus/v1?word=$Verb&language=en_US&key=$Key&output=xml" -Verbose:$false).Content

        } catch [System.Net.WebException] {
            Write-Warning -Message "Unable to find any synonyms for $Verb. Please check your spelling."

        } catch {
            Write-Warning -Message 'Unhandled error condition in Begin Block.'
        }
    } # End Begin.

    Process {
        # Check supplied verb against thesaurus.
        Write-Verbose -Message "Checking for synonoms for $Verb."
        If ($Data) {

            Write-Verbose -Message 'Attempting to parse synonyms list.'
            try {
                $Synonyms = ($Data.response.list | Where-Object -Property Category -eq '(verb)' | 
                    Select-Object -ExpandProperty Synonyms).Split('|') | 
                        Select-Object -Unique | Sort-Object
                
                Write-Verbose -Message 'Building results.'
                Foreach ($Synonym in $Synonyms) {
                    $Synonym = (Get-Culture).TextInfo.ToTitleCase($Synonym)

                    # Clear paraenthesis: (Antonym) and (Related Term) --> Antonym and Related Term.
                    # Write to Notes variable.
                    If ($Synonym -match '\(*\)') {
                        $Notes = $Synonym.Split('(')[-1].Replace(')','')
                        $Synonym = ($Synonym.Split('(')[0]).Trim()
                    } Else {
                        $Notes = $null
                    }

                    # Determine if verb is approved.
                    If (Get-Verb -Verb $Synonym -OutVariable TempVerbCheck) {
                        $ApprovedVerb = $true
                        $Group = $TempVerbCheck.Group
                        Remove-Variable -Name TempVerbCheck
                    } Else {
                        $ApprovedVerb = $false
                        $Group = $null
                    }

                    # Build Objects.
                    If ($Verb -ne $Synonym) {
                        If ($Approved) {
                            If ($ApprovedVerb -eq $true) {
                                [pscustomobject]@{
                                    Verb = $Verb
                                    Synonym = $Synonym
                                    Group = $Group
                                    Approved = $ApprovedVerb
                                    Notes = $Notes
                                }
                            }
                        } Else {
                            [pscustomobject]@{
                                Verb = $Verb
                                Synonym = $Synonym
                                Group = $Group
                                Approved = $ApprovedVerb
                                Notes = $Notes
                            }
                        } # End If-Else.
                    } # End If ($Verb -ne $Synonym).
                } # End Foreach.

            } catch [System.Management.Automation.RuntimeException] {
                Write-Warning -Message "The word ""$Verb"" may not have any verb synonyms."

            } catch {
                Write-Warning -Message 'Unhandled error condition in Process Block.'
            }
        }
    } # End Process

    End {
     } # End End.
 } # End Function: Get-TMVerbSynonym.