Check-Spelling.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 8af82790-c6ea-48a2-878b-fc8ac2d61a48
 
.AUTHOR Prateek Singh
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS SpellCheck
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>
 



<#
 
.DESCRIPTION
PowerShell to Spell check Strings passed as parameter, to Identify spelling mistakes and Rectify those spelling mistakes. Running the Command requires a subscription key that you can subscribe from - https://www.projectoxford.ai/Subscription and to understand better how to use the function go to my blog - https://geekeefy.wordpress.com/2016/03/08/spellchk/
 
#>
 

Param()


Function Check-Spelling()
{
[CmdletBinding()]
Param(
        [Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$True)]
        [String] $String,
        [Switch] $ShowErrors,
        [Switch] $RemoveSpecialChars
)
Process{
        If($RemoveSpecialChars){ $String = Clean-String $String    }
        
        Foreach($S in $String)
        {
            $SplatInput = @{
            Uri= "https://api.projectoxford.ai/text/v1.0/spellcheck?Proof"
            Method = 'Post'
            }

            $Headers =  @{'Ocp-Apim-Subscription-Key' = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"}
            $body =     @{'text'=$s    }
            Try{
                $SpellingErrors = (Invoke-RestMethod @SplatInput -Headers $Headers -Body $body -ErrorVariable +E ).SpellingErrors
                $OutString = $String # Make a copy of string to replace the errorswith suggestions.

                If($SpellingErrors)  # If Errors are Found
                {
                    # Nested Foreach to generate the Rectified string Post Spell-Check
                    Foreach($E in $spellingErrors){
                    
                        If($E.Type -eq 'UnknownToken') # If an unknown word identified, replace it with the respective sugeestion from the API results
                        {
                            $OutString= Foreach($s in $E.suggestions.token)
                                        {
                                            $OutString -replace $E.token, $s
                                        }
                        }
                        Else      # If REPEATED WORDS then replace the set by an instance of repetition
                        {
                            $OutString = $OutString -replace "$($E.token) $($E.token) ", "$($E.token) "
                        }
                    }

                    # InCase ShowErrors switch is ON
                    If($ShowErrors -eq $true)
                    {
                        return $SpellingErrors |select @{n='ErrorToken';e={$_.Token}},@{n='Type';e={$_.Type}}, @{n='Suggestions';e={($_.suggestions).token|?{$_ -ne $null}}}
                    }
                    Else      # Else return the spell checked string
                    {
                        Return $OutString 
                    }
                }
                else     # When No error is found in the input string
                {            
                        Return "No errors found in the String."
                }
                
            }
            Catch{
                "Something went wrong While extracting Text from Image, please try running the script again`nError Message : "+$E.Message
            }
        }
    }
}

# Function to Remove special character s and punctuations from Input string
Function Clean-String($Str)
{
    Foreach($Char in [Char[]]"!@#$%^&*(){}|\/?><,.][+=-_"){$str=$str.replace("$Char",'')}
    Return $str
}