Last active
October 30, 2017 17:48
-
-
Save kpatnayakuni/20753d6386b6f072fd3fc4a00cd5a635 to your computer and use it in GitHub Desktop.
Get synonyms and antonyms for a given word from thesaurus.com using PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-Synonym | |
{ | |
Param | |
( | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] | |
[String] $Word | |
) | |
$Uri = "http://www.thesaurus.com/browse/$Word" | |
$ElementTagName = 'span' | |
$ClassName = 'text' | |
try { | |
$Web = Invoke-WebRequest -Uri $Uri | |
} | |
catch { | |
Write-Host -ForegroundColor Red "Error:No word found! `nCheckthe spelling and try again." | |
break | |
} | |
$Elements = $Web.ParsedHtml.getElementsByTagName($ElementTagName) | |
$Synonyms = $Elements | Where-Object { ($_.className -eq $ClassName) -and ($_.nextSibling -ne $null) } | |
$Antonyms = $Elements | Where-Object { ($_.className -eq $ClassName) -and ($_.nextSibling -eq $null) } | |
$OriginalColor = $Host.UI.RawUI.ForegroundColor | |
Write-Host -ForegroundColor Yellow 'Synonyms for ' -NoNewline | |
Write-Host $Word | |
$Host.UI.RawUI.ForegroundColor = 'Green' | |
$Synonyms | Select-Object innerText | Format-Wide -Column 6 | |
$Host.UI.RawUI.ForegroundColor = $OriginalColor | |
Write-Host -ForegroundColor Yellow 'Antonyms for ' -NoNewline | |
Write-Host $Word | |
$Host.UI.RawUI.ForegroundColor = 'Red' | |
$Antonyms | Select-Object innerText | Format-Wide -Column 6 | |
$Host.UI.RawUI.ForegroundColor = $OriginalColor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment