Skip to content

Instantly share code, notes, and snippets.

@olafwrieden
Last active August 25, 2020 11:08
Show Gist options
  • Save olafwrieden/a3486bf145c6b082897578eef8f630db to your computer and use it in GitHub Desktop.
Save olafwrieden/a3486bf145c6b082897578eef8f630db to your computer and use it in GitHub Desktop.
Sentiment Analysis Snippets for Azure, AWS, GCP.
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
const client = new TextAnalyticsClient(process.env["TEXT_ANALYSIS_ENDPOINT"], new AzureKeyCredential(process.env["TEXT_ANALYSIS_KEY"]));
module.exports = async function (context, req) {
// Extract submitted text from request body
const text = req.body && req.body.text;
if (!text) {
// Error if no text was found
return context.res = {
status: 422,
body: "A text key value pair is required."
};
} else {
// Await sentiment analysis for the text
const sentiment = await client.analyzeSentiment([text]);
// Return sentiment details
return context.res = {
status: 200,
body: sentiment[0]
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment