Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active April 13, 2018 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PrateekKumarSingh/7676e473e37e639dcd81 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/7676e473e37e639dcd81 to your computer and use it in GitHub Desktop.
Updated on 26-May-16 10:29:38 by Prateek Singh
Function Get-Synonym
{
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({
If($_ -match "^[a-zA-Z]+$"){
$True
}
else{
Throw "$_ is not a valid Word or contains something beyond Alphabets A-Z or a-z"
}
})
] $Word
)
Function Get-FullWord ($Abbreviation)
{
$Table = @{
"Adj"="Adjective"
"Adv"="Adverb"
}
$FullWord=$Table | %{ if($_.containskey($Abbreviation)){$_.get_item($Abbreviation)}else{$Abbreviation} }
Return $FullWord
}
Try
{
$webpage = Invoke-WebRequest -Uri "http://thesaurus.altervista.org/thesaurus/v1?word=$Word&key=qFTNpXBBp8xiNR0LdGCt&language=en_US" -UseBasicParsing -ErrorVariable ErrVar
$Word = (Get-Culture).textinfo.totitlecase($Word)
# Capturing the HTML output
$content = $webpage.Content
$Category = (Select-Xml -content $content -xpath '//list/category').node.InnerText
$Category = $Category | %{$str=$_;(Get-Culture).textinfo.totitlecase($str.Substring(1,$str.Length-2))}
$Data = ((Select-Xml -content $content -xpath '//list/synonyms').node.InnerText)
$Object = @()
if($Category.count -gt 1)
{
For($i=0;$i -lt $($Category.Count);$i++)
{
$synonyms = (Get-Culture).textinfo.totitlecase($Data[$i]).Split('|')
For($j=0;$j -lt $($synonyms.Count);$j++)
{
$Object += New-Object psobject -Property @{Word=$Word;Category=Get-FullWord($Category[$i]);Synonym=$synonyms[$j]}
}
}
Return $Object
}
else
{
$synonyms = (Get-Culture).textinfo.totitlecase($Data).Split('|')
For($j=0;$j -lt $($synonyms.Count);$j++)
{
$Object += New-Object psobject -Property @{Word=$Word;Category=Get-FullWord($Category);Synonym=$synonyms[$j]}
}
Return $Object
}
}
catch
{
Write-Host $_.exception.message -ForegroundColor Yellow
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment