Skip to content

Commit b717aff

Browse files
authored
TTS / Sentiment GA (handoff from Nirupa) (#353)
* TTS / Sentiment GA (handoff from Nirupa) * Lint / feedback * Lint / feedback * Fix tests
1 parent 301906a commit b717aff

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2018, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
async function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
sessionId = 'YOUR_SESSION_ID',
20+
query = 'YOUR_QUERY',
21+
languageCode = 'YOUR_LANGUAGE_CODE',
22+
outputFile = 'YOUR_OUTPUT_FILE'
23+
) {
24+
// [START dialogflow_detect_intent_with_texttospeech_response]
25+
// Imports the Dialogflow client library
26+
const dialogflow = require('dialogflow').v2;
27+
28+
// Instantiate a DialogFlow client.
29+
const sessionClient = new dialogflow.SessionsClient();
30+
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
35+
// const sessionId = `user specific ID of session, e.g. 12345`;
36+
// const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`;
37+
// const languageCode = 'BCP-47 language code, e.g. en-US';
38+
// const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`;
39+
40+
// Define session path
41+
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
42+
const fs = require(`fs`);
43+
const util = require(`util`);
44+
45+
async function detectIntentwithTTSResponse() {
46+
// The audio query request
47+
const request = {
48+
session: sessionPath,
49+
queryInput: {
50+
text: {
51+
text: query,
52+
languageCode: languageCode,
53+
},
54+
},
55+
outputAudioConfig: {
56+
audioEncoding: `OUTPUT_AUDIO_ENCODING_LINEAR_16`,
57+
},
58+
};
59+
sessionClient.detectIntent(request).then(responses => {
60+
console.log('Detected intent:');
61+
const audioFile = responses[0].outputAudio;
62+
util.promisify(fs.writeFile)(outputFile, audioFile, 'binary');
63+
console.log(`Audio content written to file: ${outputFile}`);
64+
});
65+
}
66+
detectIntentwithTTSResponse();
67+
// [END dialogflow_detect_intent_with_texttospeech_response]
68+
}
69+
70+
main(...process.argv.slice(2));
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright 2018, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
async function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
sessionId = 'YOUR_SESSION_ID',
20+
query = 'YOUR_QUERY',
21+
languageCode = 'YOUR_LANGUAGE_CODE'
22+
) {
23+
// [START dialogflow_detect_intent_with_sentiment_analysis]
24+
// Imports the Dialogflow client library
25+
const dialogflow = require('dialogflow').v2;
26+
27+
// Instantiate a DialogFlow client.
28+
const sessionClient = new dialogflow.SessionsClient();
29+
30+
/**
31+
* TODO(developer): Uncomment the following lines before running the sample.
32+
*/
33+
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
34+
// const sessionId = `user specific ID of session, e.g. 12345`;
35+
// const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`;
36+
// const languageCode = 'BCP-47 language code, e.g. en-US';
37+
38+
// Define session path
39+
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
40+
41+
async function detectIntentandSentiment() {
42+
// The text query request.
43+
const request = {
44+
session: sessionPath,
45+
queryInput: {
46+
text: {
47+
text: query,
48+
languageCode: languageCode,
49+
},
50+
},
51+
queryParams: {
52+
sentimentAnalysisRequestConfig: {
53+
analyzeQueryTextSentiment: true,
54+
},
55+
},
56+
};
57+
58+
// Send request and log result
59+
const responses = await sessionClient.detectIntent(request);
60+
console.log('Detected intent');
61+
const result = responses[0].queryResult;
62+
console.log(` Query: ${result.queryText}`);
63+
console.log(` Response: ${result.fulfillmentText}`);
64+
if (result.intent) {
65+
console.log(` Intent: ${result.intent.displayName}`);
66+
} else {
67+
console.log(` No intent matched.`);
68+
}
69+
if (result.sentimentAnalysisResult) {
70+
console.log(`Detected sentiment`);
71+
console.log(
72+
` Score: ${result.sentimentAnalysisResult.queryTextSentiment.score}`
73+
);
74+
console.log(
75+
` Magnitude: ${
76+
result.sentimentAnalysisResult.queryTextSentiment.magnitude
77+
}`
78+
);
79+
} else {
80+
console.log(`No sentiment Analysis Found`);
81+
}
82+
// [END dialogflow_detect_intent_with_sentiment_analysis]
83+
}
84+
detectIntentandSentiment();
85+
}
86+
main(...process.argv.slice(2));

dialogflow/system-test/detect.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const {assert} = require('chai');
2020
const execa = require('execa');
2121

2222
const cmd = 'node detect.js';
23+
const cmd_tts = 'node detect-intent-TTS-response.v2.js';
24+
const cmd_sentiment = 'node detect-intent-sentiment.v2.js';
2325
const cwd = path.join(__dirname, '..');
26+
const projectId =
27+
process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT;
28+
const testQuery = 'Where is my data stored?';
2429

2530
const audioFilepathBookARoom = path
2631
.join(__dirname, '../resources/book_a_room.wav')
@@ -52,4 +57,23 @@ describe('basic detection', () => {
5257
);
5358
assert.include(stdout, 'Detected intent');
5459
});
60+
61+
it('should detect Intent with Text to Speech Response', async () => {
62+
const {stdout} = await execa.shell(
63+
`${cmd_tts} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US' './resources/output.wav'`,
64+
{cwd}
65+
);
66+
assert.include(
67+
stdout,
68+
'Audio content written to file: ./resources/output.wav'
69+
);
70+
});
71+
72+
it('should detect sentiment with intent', async () => {
73+
const {stdout} = await execa.shell(
74+
`${cmd_sentiment} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US'`,
75+
{cwd}
76+
);
77+
assert.include(stdout, 'Detected sentiment');
78+
});
5579
});

0 commit comments

Comments
 (0)