Skip to content

Instantly share code, notes, and snippets.

@kpatnayakuni
Last active October 30, 2017 17:48
Show Gist options
  • Save kpatnayakuni/20753d6386b6f072fd3fc4a00cd5a635 to your computer and use it in GitHub Desktop.
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
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