内容简介:Wow, that's a lot of code. Let's break it down.We import
Hamilton the Musical will start streaming on Disney Plus this Friday, so happy Hamilfilm week! To celebrate, learn how to build a SMS chatbot that recommends the Hamilton song most relevant to you right now using Twilio Programmable SMS and Functions, Microsoft Azure Cognitive Services , and JavaScript.
See it in-action: text how you’re feeling to +13364295064 . The longer and more descriptive your message is, the more data the app has to analyze what Hamilton song you need now!
Prerequisites and setting up Azure Cognitive Services
To follow along with this post, you need three things:
- A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
- A Twilio phone number with SMS capabilities - configure one here
- Microsoft Azure - make a free account here if you don't have one already
To use Azure Cognitive Services, you will need an Azure key and endpoint . Follow the directions here to create a Cognitive Services resource using the Azure services portal .
After filling out the Resource, click Create . Once your Resource deploys, click Go to resource . You should see your endpoint and key in the quickstart pane that opens, or you can also click Keys and Endpoint beneath Resource Management on the left-hand pane.
If you are shown two keys, you will only need the first one which we will now configure to be the value for an environment variable.
Configure a Twilio Function with Azure
Configure your Twilio Functions with your Azure endpoint and key as environment variables from the last step.
Then add the dependencies @azure/ai-text-analytics 1.0.0
and whichx *
as shown below. This post will also use Whichx , a naive Bayesian classifier that can succinctly and cleanly analyze data. You can read more on Naive Bayes here .
Click Save and you can now use Azure AI Text Analytics and reference your Azure endpoint and key in any of your Twilio Functions!
Make a Twilio Function
On the left-hand panel underneath Functions , click Manage . To make a new Function, click the red plus button and then select a Blank template followed by Create .
Give your Function a name like "What Ham Song do you need" and a path, like "/hamilfilm".
Analyze an inbound SMS using Azure Cognitive Services and Naive Bayes with Node.js
Replace the Function code with the following:
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics"); const WhichX = require("whichx"); exports.handler = async function(context, event, callback) { let twiml = new Twilio.twiml.MessagingResponse(); const key = context.AZURE_KEY_HAMILFILM; const endpoint = context.AZURE_ENDPOINT_HAMILFILM; const textAnalyticsClient = new TextAnalyticsClient(endpoint, new AzureKeyCredential(key)); const input = [ event.Body ]; const songs = { "non-stop": { desc: "Do not assume you are the smartest in the room. Time to do some work.", link: "youtube.com/watch?v=_YHVPNOHySk" }, "wait for it": { desc: "Good things take time. You will get praise and validation soon. If you're doubting yourself, just keep going. You are inimitable, an original.", link: "youtube.com/watch?v=ulsLI029rH0" }, "schuyler sisters": { desc: "Girl power! You are empowered and thus empower others. Keep your siblings close. You may be looking for a significant other, a friend, a peer, or a general mind at work.", link: "youtube.com/watch?v=UeqKF_NF1Qs" }, "dear theodosia": { desc: "you get all emotional and teary over your kid or your pet. They are cute, young, innocent, and have their whole lives ahead of them, which you will make better.", link: "youtube.com/watch?v=TKpJjdKcjeo" }, "story of tonight": { desc: "You are feeling emotional over what you, your friends, and your family will do in the future. The night is still young. You can all do so much and change the world!", link: "youtube.com/watch?v=3vqwrepaMR0" }, "my shot": { desc: "life is tough but you are tougher. All you need is one chance, one shot per se, and here is the motivation you need to accomplish anything.", link: "youtube.com/watch?v=Ic7NqP_YGlg" }, "alexander hamilton": { desc: "You don't get the hype over Hamilton. Hamilton may be new to you. This song will sum it up succinctly for you and you'll learn some history too.", link: "youtube.com/watch?v=VhinPd5RRJw" }, }; const sentimentResult = await textAnalyticsClient.analyzeSentiment(input); let sentiment, pos, neg, neutral, max; sentimentResult.forEach(document => { console.log(`ID: ${document.id}`); console.log(`Document Sentiment: ${document.sentiment}`); console.log(`Positive: ${document.confidenceScores.positive.toFixed(2)} Negative: ${document.confidenceScores.negative.toFixed(2)} Neutral: ${document.confidenceScores.neutral.toFixed(2)}`); document.sentences.forEach(sentence => { sentiment = sentence.sentiment; console.log(`Sentence sentiment: ${sentiment}`); pos = sentence.confidenceScores.positive.toFixed(2); neg = sentence.confidenceScores.negative.toFixed(2); neutral = sentence.confidenceScores.neutral.toFixed(2); var obj = {"positive": pos, "negative": neg, "neutral": neutral}; max = Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b); }); }); //Build our Bayesian model var whichfw = new WhichX(); whichfw.addLabels(["non-stop", "wait for it", "schuyler sisters", "dear theodosia", "story of tonight", "my shot", "alexander hamilton"]); Object.keys(songs).forEach((s) => { whichfw.addData(s.toLowerCase(), songs[s].desc) } ); const song = whichfw.classify(event.Body); const reasonWhySong = songs[song].desc; const link = songs[song].link; twiml.message(`You seem to be feeling ${max}. ${reasonWhySong} We recommend listening to ${song} right now: ${link}`); callback(null, twiml); };
Wow, that's a lot of code. Let's break it down.
We import Azure AI Text Analytics and WhichX at the top with:
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics"); const WhichX = require("whichx");
Then we make our Function asynchronous to give the Function more time to analyze the inbound SMS input, make a MessagingResponse
object that we will later return as an outbound SMS, create variables referencing our Azure endpoint and key environment variables, and pass them to textAnalyticsClient
. Lastly, we pass in the inbound text message body to an array input
.
exports.handler = async function(context, event, callback) { let twiml = new Twilio.twiml.MessagingResponse(); const key = context.AZURE_KEY_HAMILFILM; const endpoint = context.AZURE_ENDPOINT_HAMILFILM; const textAnalyticsClient = new TextAnalyticsClient(endpoint, new AzureKeyCredential(key)); const input = [ event.Body ];
Next we make the key-value object holding the collection of Hamilton songs the user can be classified as. Each song has a brief corresponding description the classifier will attempt to match to according to the inbound SMS.
const songs = { "non-stop": { desc: "Do not assume you are the smartest in the room. Time to do some work.", link: "youtube.com/watch?v=_YHVPNOHySk" }, ... };
Now we call our client's analyzeSentiment
method, which returns a SentimentBatchResult
object, and create some global variables.
const sentimentResult = await textAnalyticsClient.analyzeSentiment(input); let sentiment, pos, neg, neutral, max;
Iterate through the list of results, and print each document's ID and document-level sentiment (analyzes the whole text) with confidence scores. For each document, result
contains sentence-level sentiment (analyzes just a sentence) along with confidence scores (percent confident the model is that the sentiment is positive, negative, or neutral) and more information that we do not need for this post. Lastly, we find the key (positive, negative, or neutral) that has the highest confidence level value.
sentimentResult.forEach(document => { console.log(`ID: ${document.id}`); console.log(`Document Sentiment: ${document.sentiment}`); console.log(`Positive: ${document.confidenceScores.positive.toFixed(2)} Negative: ${document.confidenceScores.negative.toFixed(2)} Neutral: ${document.confidenceScores.neutral.toFixed(2)}`); document.sentences.forEach(sentence => { sentiment = sentence.sentiment; console.log(`Sentence sentiment: ${sentiment}`); pos = sentence.confidenceScores.positive.toFixed(2); neg = sentence.confidenceScores.negative.toFixed(2); neutral = sentence.confidenceScores.neutral.toFixed(2); var obj = {"positive": pos, "negative": neg, "neutral": neutral}; max = Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b); }); });
Finally, we build our naive Bayesian classifier, using it to classify the inbound text according to Hamilton songs by adding the labels of the Hamilton songs we want to classify. You could build a classifier in a variety of ways, but this is a succinct way to do so.
//Build our Bayesian model var whichfw = new WhichX(); whichfw.addLabels(["non-stop", "wait for it", "schuyler sisters", "dear theodosia", "story of tonight", "my shot", "alexander hamilton"]); Object.keys(songs).forEach((s) => { whichfw.addData(s.toLowerCase(), songs[s].desc) } ); const song = whichfw.classify(event.Body); const reasonWhySong = songs[song].desc; const link = songs[song].link; twiml.message(`You seem to be feeling ${max}. ${reasonWhySong} We recommend listening to ${song} right now: ${link}`); callback(null, twiml);
Save your Function. You can view the complete code on GitHub here . Let's now configure a Twilio phone number to analyze texts using IBM Watson.
Configure your Twilio Phone Number with a Twilio Function
If you don't have a Twilio number yet, go to the Phone Numbers section of your Twilio Console and search for a phone number in your country and region, making sure the SMS checkbox is ticked.
In the Messaging section of your purchased number, in the A Message Comes In section, set the dropdown menu to Function instead of Webhook and then on the right select your Function from the larger dropdown menu, as shown below. Hit Save .
Whip out your phone and text your Twilio number how you are feeling to see what Hamilton song you should listen to right now.
What's Next for Recommending Hamilton Songs
I will be listening to Hamilton to celebrate Hamilton coming to Disney Plus. In the meantime, you can use different tools to analyze texts likeIBM Watson, Google Cloud Natural Language ,TensorFlow.js, and more. You could also recommend a Hamilton lyric (must include "You're On Your Own. Awesome. Wow! Do You Have A Clue What Happens Now?" ) Let me know what you're building and what your favorite Hamilton song is online or in the comments.
- Twitter: @lizziepika
- GitHub: elizabethsiegle
- Email:lsiegle@twilio.com
Authors
以上所述就是小编给大家介绍的《Build a Hamilton Song Recommendation SMS Bot with Machine Learning》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
图解服务器端网络架构
[日] 宫田宽士 / 曾薇薇 / 人民邮电出版社 / 2015-4 / 79.00元
本书以图配文,详细说明了服务器端网络架构的基础技术和设计要点。基础设计是服务器端网络架构最重要的一个阶段。本书就立足于基础设计的设计细分项目,详细介绍各细分项目的相关技术和设计要点。全书共分为5章,分别讲述进行物理设计、逻辑设计、安全设计和负载均衡设计、高可用性设计以及管理设计时所必需的技术和设计要点。一起来看看 《图解服务器端网络架构》 这本书的介绍吧!
Base64 编码/解码
Base64 编码/解码
RGB HSV 转换
RGB HSV 互转工具