-
Notifications
You must be signed in to change notification settings - Fork 1
/
AzureFunction
123 lines (89 loc) · 4.39 KB
/
AzureFunction
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.SYNOPSIS
Run phrase and sentiment analytics on supplied text.
.DESCRIPTION
The script runs the recieved text thorugh Microsoft translator service and translate it to English.
After the text is trenslated it is sendt to Text analytics API to run sentiment an phrase analisys.
The concept is to try and determinate if the author of the text is happy or not, and what the text is about.
IE: Run any open support tickets through to try and prioritize each ticket. Or use the key phrases to automatickly route the ticket to the correct department
.EXAMPLE
You will need to send json formatted data containing a job ID and the text to analyse
{
"id": 3,
"text": "Donald Trump a réaffirmé, lundi, ses positions critiques vis-à-vis de l’Otan, de l’UE, et de la politique d’accueil des migrants lors d’entretiens accordés à des médias européen
s. Une vision toujours proche de celle de Vladimir Poutine.\r\n\r\nUne erreur catastrophique de Merkel sur l’accueil des migrants, l’Otan obsolète, le succès du Brexit qui marque le début de
la fin de l’Union européenne. Si le fond ressemble à du Vladimir Poutine, la forme, elle, est clairement signée Donald Trump.\r\n\r\nLundi 16 janvier, à cinq jours de son investiture, le magn
at de l’immobilier n’a pas mâché ses mots pour exposer ses vues sur les sujets d’actualité les plus brûlants sur le Vieux Continent, auprès des journaux britannique Times et allemand Bild."
}
When the job is finished you will recieve a Json formatted response containing sentiment score, key phrases an the translated text.
.NOTES
Created by Martin Ehrnst
www.adatum.no
.CHANGELOG
12.01.17: v0.5 Beta (Initial release)
This version has zero error handling and must be considered POC
#>
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$text = $requestBody.text
$id = $requestBody.id
if ($req_query_text)
{
$text = $req_query_text
}
if ($req_query_id)
{
$id = $req_query_id
}
$TextToTranslate = $text
#Replacing characters that are unsupported
$TextToTranslate = $TextToTranslate -replace "[«»""#]","'"
#region Configuration
$TransaccountKey = $env:TranslationAccountKey
$AnalAccountKey = $env:AnalyticsAccountKey
[string]$ToLanguage = "en"
#endregion
#region CreateToken
$tokenServiceURL = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
$query = "?Subscription-Key=$TransaccountKey"
$TokenUri = $tokenServiceUrl+$query
$token = Invoke-RestMethod -Uri $TokenUri -Method Post
$auth = "Bearer "+$token
#endregion
#region RunTranslation
$translationURL = "http://api.microsofttranslator.com/v2/Http.svc/Translate"
$uri = $translationUrl
$RunTrans = Invoke-RestMethod -Method Get -Headers @{'Authorization' = $Auth} -Uri ('http://api.microsofttranslator.com/v2/Http.svc/Translate?text={0}&to={1}' -f `
($TextToTranslate, $ToLanguage | %{ [Web.HttpUtility]::UrlEncode($_) } ))
$translatedText = $runTrans.string.'#text'#The translated text
#replace characters that are unsupported
$translatedText = $translatedText -replace "[æå]","a"
$translatedText = $translatedText -replace "[ø]","o"
$translatedText = $translatedText -replace "[£$]",""
#endregion
#region RunTextAnalytics
$TextAnalyticsHeader = @{
'Ocp-Apim-Subscription-Key' = $AnalAccountKey
'Content-Type' = 'application/json'
}
$TextAnalyticsBody = [ordered]@{
"documents" =
@(
@{
"language" = "en";
"id" = $id;
"text" = $translatedText }
)
} | ConvertTo-Json
$RunSentiment = Invoke-RestMethod -Method post -Headers $TextAnalyticsHeader -uri 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment' -Body $TextAnalyticsBody
$sentimentScore = $RunSentiment.documents.score
$sentimentScore = $sentimentScore.ToString("P") #setting percentage insted of value
$runPhrases = Invoke-RestMethod -Method post -Headers $TextAnalyticsHeader -uri 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases' -Body $TextAnalyticsBody
$keyPhrases = $runPhrases.documents.keyPhrases -join ', ' #seperate each phrase with a comma
$TextAnalisys = @{
'Sentiment Score' = $sentimentScore
'Key phrases' = $keyPhrases
'Translation' = $TranslatedText
} | Convertto-json
$TextAnalisys #Print the result
#endregion
Out-File -Encoding Ascii -FilePath $res -inputObject "$TextAnalisys"