-
-
Save PrateekKumarSingh/7ee24980b25ecc60496e to your computer and use it in GitHub Desktop.
Wisdom form the Shellfather, Jeffrey Snover - via Don Jones
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-Snoverism | |
{ | |
$URL = "http://snoverisms.com/" | |
(Invoke-WebRequest $URL).images.src |?{$_ -match 'quotes'}| Get-Random -Count 1 | %{$URL+$_} | |
} | |
Function Get-ImageText() | |
{ | |
[CmdletBinding()] | |
Param( | |
#[Parameter(ValueFromPipeline=$True)] | |
#[String] $Path, | |
[Parameter(ValueFromPipeline=$True)] | |
[String] $URL | |
) | |
Process{ | |
$SplatInput = @{ | |
Uri= "https://api.projectoxford.ai/vision/v1/ocr" | |
Method = 'Post' | |
#InFile = $Path | |
ContentType = 'application/json' | |
} | |
$Headers = @{ | |
# Your Secret Subscription Key goes here. | |
'Ocp-Apim-Subscription-Key' = "####################" | |
} | |
If($URL) | |
{ | |
$Body = @{"URL"= "$URL"} | ConvertTo-Json | |
} | |
Try{ | |
$Data = Invoke-RestMethod @SplatInput -Body $Body -Headers $Headers -ErrorVariable E | |
$Language = $Data.Language | |
$i=0; foreach($D in $Data.regions.lines){ | |
$i=$i+1;$s=''; | |
''|select @{n='LineNumber';e={$i}},@{n='LanguageCode';e={$Language}},@{n='Sentence';e={$D.words.text |%{$s=$s+"$_ "};$s}}} | |
} | |
Catch{ | |
"Something went wrong While extracting Text from Image, please try running the script again`nError Message : "+$E.Message | |
} | |
} | |
} | |
Function Translate-text | |
{ | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$True)] | |
[String] $Text, | |
[String] [validateSet('Arabic','Hindi','Japanese','Russian','Spanish','French',` | |
'English','Korean','Urdu','Italian','Portuguese','German','Chinese Simplified') | |
]$From, | |
[String] [validateSet('Arabic','Hindi','Japanese','Russian','Spanish','French',` | |
'English','Korean','Urdu','Italian','Portuguese','German','Chinese Simplified') | |
]$To | |
) | |
Begin{ | |
# Language codes hastable | |
$LangCodes = @{'Arabic'='ar' | |
'Chinese Simplified'='zh-CHS' | |
'English'='en' | |
'French'='fr' | |
'German'='de' | |
'Hindi'='hi' | |
'Italian'='it' | |
'Japanese'='ja' | |
'Korean'='ko' | |
'Portuguese'='pt' | |
'Russian'='ru' | |
'Spanish'='es' | |
'Urdu'='ur' | |
} | |
# Secret Client ID and Key you get after Subscription | |
$ClientID = 'OCR-BingTranslation' | |
$client_Secret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
# If ClientId or Client_Secret has special characters, UrlEncode before sending request | |
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID) | |
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret) | |
} | |
Process{ | |
ForEach($T in $Text) | |
{ | |
Try{ | |
# Azure Data Market URL which provide access tokens | |
$URI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13" | |
# Body and Content Type of the request | |
$Body = "grant_type=client_credentials&client_id=$clientIDEncoded&client_secret=$client_SecretEncoded&scope=http://api.microsofttranslator.com" | |
$ContentType = "application/x-www-form-urlencoded" | |
# Invoke REST method to Azure URI | |
$Access_Token=Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Method Post | |
# Header value with the access_token just recieved | |
$Header = "Bearer " + $Access_Token.access_token | |
# Invoke REST request to Microsoft Translator Service | |
[string] $EncodedText = [System.Web.HttpUtility]::UrlEncode($T) | |
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $EncodedText + "&from=" + $LangCodes.Item($From) + "&to=" + $LangCodes.Item($To); | |
$Result = Invoke-RestMethod -Uri $URI -Headers @{Authorization = $Header} -ErrorVariable Error | |
Return $Result.string.'#text' | |
} | |
catch | |
{ | |
"Something went wrong While Translating Text, please try running the script again`nError Message : "+$Error.Message | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment