forked from Azure-Samples/cognitive-services-speech-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintent.js
More file actions
125 lines (103 loc) · 5.45 KB
/
intent.js
File metadata and controls
125 lines (103 loc) · 5.45 KB
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
124
125
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// pull in the required packages.
var sdk = require("microsoft-cognitiveservices-speech-sdk");
(function() {
"use strict";
module.exports = {
main: function(settings, audioStream) {
// now create the audio-config pointing to our stream and
// the speech config specifying the language.
var audioConfig = sdk.AudioConfig.fromStreamInput(audioStream);
var intentConfig = sdk.SpeechConfig.fromSubscription(settings.luSubscriptionKey, settings.luServiceRegion);
// setting the recognition language to English.
intentConfig.speechRecognitionLanguage = settings.language;
// create the translation recognizer.
var recognizer = new sdk.IntentRecognizer(intentConfig, audioConfig);
// Set up a Language Understanding Model from Language Understanding Intelligent Service (LUIS).
// See https://www.luis.ai/home for more information on LUIS.
if (settings.luAppId !== "" && settings.luAppId !== "YourLanguageUnderstandingAppId") {
var lm = sdk.LanguageUnderstandingModel.fromAppId(settings.luAppId);
recognizer.addAllIntents(lm);
}
// Before beginning speech recognition, setup the callbacks to be invoked when an event occurs.
// The event recognizing signals that an intermediate recognition result is received.
// You will receive one or more recognizing events as a speech phrase is recognized, with each containing
// more recognized speech. The event will contain the text for the recognition since the last phrase was recognized.
recognizer.recognizing = function (s, e) {
var str = "(recognizing) Reason: " + sdk.ResultReason[e.result.reason] + " Text: " + e.result.text;
console.log(str);
};
// The event signals that the service has stopped processing speech.
// https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognitioncanceledeventargs?view=azure-node-latest
// This can happen for two broad classes or reasons.
// 1. An error is encountered.
// In this case the .errorDetails property will contain a textual representation of the error.
// 2. Speech was detected to have ended.
// This can be caused by the end of the specified file being reached, or ~20 seconds of silence from a microphone input.
recognizer.canceled = function (s, e) {
var str = "(cancel) Reason: " + sdk.CancellationReason[e.reason];
if (e.reason === sdk.CancellationReason.Error) {
str += ": " + e.errorDetails;
}
console.log(str);
};
// The event recognized signals that a final recognition result is received.
// This is the final event that a phrase has been recognized.
// For continuous recognition, you will get one recognized event for each phrase recognized.
recognizer.recognized = function (s, e) {
var str = "(recognized) Reason: " + sdk.ResultReason[e.result.reason];
// Depending on what result reason is returned, different properties will be populated.
switch (e.result.reason) {
// This case occurs when speech was successfully recognized, but the speech did not match an intent from the Language Understanding Model.
case sdk.ResultReason.RecognizedSpeech:
str += " Text: " + e.result.text;
break;
// Both speech an intent from the model was recognized.
case sdk.ResultReason.RecognizedIntent:
str += " Text: " + e.result.text + " IntentId: " + e.result.intentId;
// The actual JSON returned from Language Understanding is a bit more complex to get to, but it is available for things like
// the entity name and type if part of the intent.
str += " Intent JSON: " + e.result.properties.getProperty(sdk.PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
break;
// No match was found.
case sdk.ResultReason.NoMatch:
var noMatchDetail = sdk.NoMatchDetails.fromResult(e.result);
str += " NoMatchReason: " + sdk.NoMatchReason[noMatchDetail.reason];
break;
}
console.log(str);
};
// Signals that a new session has started with the speech service
recognizer.sessionStarted = function (s, e) {
var str = "(sessionStarted) SessionId: " + e.sessionId;
console.log(str);
};
// Signals the end of a session with the speech service.
recognizer.sessionStopped = function (s, e) {
str = "(sessionStopped) SessionId: " + e.sessionId;
console.log(str);
};
// Signals that the speech service has started to detect speech.
recognizer.speechStartDetected = function (s, e) {
var str = "(speechStartDetected) SessionId: " + e.sessionId;
console.log(str);
};
// Signals that the speech service has detected that speech has stopped.
recognizer.speechEndDetected = function (s, e) {
var str = "(speechEndDetected) SessionId: " + e.sessionId;
console.log(str);
};
// start the recognizer and wait for a result.
recognizer.recognizeOnceAsync(
function (result) {
recognizer.close();
recognizer = undefined;
},
function (err) {
recognizer.close();
recognizer = undefined;
});
}
}
}());