-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIntegrateAzureSpeechAPIWithLUIS_PrebuiltDomain.cs
62 lines (55 loc) · 2.02 KB
/
IntegrateAzureSpeechAPIWithLUIS_PrebuiltDomain.cs
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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Intent;
namespace IntegrateSpeechWithLUIS
{
class Program
{
static void Main(string[] args)
{
// Read text from Mic
string text = GetTextFromMicAsync().Result;
// Read intent
var response = GetIntents("PREDICTION_KEY", "PREDICTION_ENDPOINT", "APP_ID", text).Result;
Console.WriteLine(response);
}
private static async Task<string> GetTextFromMicAsync()
{
var config = SpeechConfig.FromSubscription("SPEECH_SERVICE_KEY", "REGION");
using (var recognizer = new IntentRecognizer(config))
{
Console.WriteLine("Speak now...");
var result = await recognizer.RecognizeOnceAsync();
if (result.Reason == ResultReason.RecognizedSpeech)
{
return result.Text;
}
else
{
return "Speech could not be recognized.";
}
}
}
private static async Task<string> GetIntents(string predictionKey, string predionEndpoint, string appId, string text)
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
try
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", predictionKey);
queryString["query"] = text;
var preEndpointUri = String.Format("{0}luis/prediction/v3.0/apps/{1}/slots/production/predict?{2}", predionEndpoint, appId, queryString);
var response = await client.GetAsync(preEndpointUri);
var res = await response.Content.ReadAsStringAsync();
return res;
}
catch (Exception)
{
throw;
}
}
}
}