-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.ps1
76 lines (50 loc) · 2.3 KB
/
translator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Add your key here
$key="YOUR_KEY"
#You need to add your resource location if you use a Cognitive Services resource
$location="YOUR_LOCATION"
#The endpoint is global for the Translator service, DO NOT change it
$endpoint="https://api.cognitive.microsofttranslator.com/"
#Text to be translated
$text="Hello"
# Code to call Text Analytics service to analyze sentiment in text
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Ocp-Apim-Subscription-Region", $location )
$headers.Add( "Content-Type","application/json" )
$body = "[{'text': '$text'}]"
write-host "Translating text..."
$result = Invoke-Webrequest -Method Post `
-Uri "$endpoint/translate?api-version=3.0&from=en&to=fr&to=it&to=zh-Hans" `
-Headers $headers `
-Body $body
$analysis = $result.content | ConvertFrom-Json
$french = $analysis.translations[0]
$italian = $analysis.translations[1]
$chinese = $analysis.translations[2]
Write-Host ("Original Text: $text`nFrench Translation: $($french.text)`nItalian Translation: $($italian.text)`nChinese Translation: $($chinese.text)`n")
# Code to Translate audio to text in another language
$wav = "./data/translation/english.wav"
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Content-Type","audio/wav" )
write-host "Translating audio..."
$audio_result = Invoke-RestMethod -Method Post `
-Uri "https://$location.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US" `
-Headers $headers `
-InFile $wav
$original_audio_text = $audio_result.DisplayText
Write-Host ("The audio said '$($original_audio_text)'")
# Code to call translate audio text to different language
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Ocp-Apim-Subscription-Region", $location )
$headers.Add( "Content-Type","application/json" )
$body = "[{'text': '$original_audio_text'}]"
write-host "Translating text from audio to French..."
$result = Invoke-Webrequest -Method Post `
-Uri "$endpoint/translate?api-version=3.0&from=en&to=fr" `
-Headers $headers `
-Body $body
$analysis = $result.content | ConvertFrom-Json
$french_translation = $analysis.translations.text
Write-host ("Translated text: '$french_translation'")