📜 ⬆️ ⬇️

Talk with ghosts: Ada Lovelace

Halloween ghosts can be not only ghosts outside the window and scary costumes in which colleagues came. For a true introvert - it will be communication with crowds of people during the holiday, and not only. Last year, I participated in creating the bot of Ada Lovelace , the world's first female programmer. During the year I occasionally chatted with her and dreamed of making her more perfect. I invite you to read about new features that we have added: quotes from Ada herself, mathematical tasks from her and image recognition.



We recently hosted a hackathon for Microsoft partner students, where one of the teams embarked on the idea of ​​improving the previous version of the Ada Lovelace bot. Let's see what happened.


As ada taught


The Ada Lovelace bot is built on the Microsoft Bot Framework , which allows it to be deployed not only in Telegram, but also in other channels such as Skype, Slack, Facebook Messenger, Web Chat, Cortana, SMS, etc. The guys tried to recreate the bot, taking as a basis the real quotations of Ada Lovelace. It's quite interesting what the girl thought at that moment - a strong mathematician and the first programmer.


From the new functional : Ada will propose to solve mathematical problems with her, and algorithms problems, since we all know that she loved mathematics very much, and also introduced terms such as “cycle” and “working cell” for the first time. This time, in addition to the fan component, Ada can pull you up in math, transferring from one number system to another, as well as in the basic concepts of "cycles" and "work cells". All tasks have a terrible legend, glowing passion.


Here is an example of one of the tasks that Hell asks you to solve.

In the evenings, I like to sit on the veranda, filling up my diary with new and new research in the field of mathematics. But one day, I noticed my neighbor - Mr. Sanders constantly goes to the neighboring garden in the evenings. It became interesting to me. After waiting for midnight, when the white mist, illuminated by moonlight, envelops the garden, I decided to learn his secret. Having circled the fountain from all sides in a barely visible moonlight, I noticed a strange pattern on the bottom of the fountain. It contained four separately placed inscriptions: "1010", "1100", "10010", "1111". Probably, these are the digits in the decimal system. I wonder if my assumptions are correct, what numbers were coded there? What secret was Mr. Sanders hiding? Do you have any suggestions? Enter the answer holistically below. " Or for example: “In the morning, after breakfast, I really like to stay in my father’s study and read my father’s research. Once again, reading his diary, I discovered the shabby entries of some mathematical calculation. In this mathematical calculation, there was a number that was incremented (+) or decremented (-) by a specific other number. This calculation was completed only when the sum reached a certain value, which we asked ourselves. I decided to call this repetition "The Cycle". For practical confirmation of the correctness of the definition of my term, I decided to carry out a simple mathematical calculation. After how many actions the cycle will stop, provided that: the starting number is 5, the increment value is 3, the threshold number for terminating the cycle is 35.


Some of the tasks are provided with hints, if a person cannot solve the first time, he will receive a hint, and if he makes a mistake 3 times, he will be told the answer. As soon as the interlocutor starts a conversation about math or programming, Ada can offer him to solve the puzzle.


The bot will issue tasks to the buddy using the IDialog dialog wrapper. Random will randomly choose the moment to give the user a task. Next, a dialog will be created in which the tasks from the json file from Blob to Microsoft Azure will be set.


The code is here.
public async System.Threading.Tasks.Task MessageRecieveAsync(IDialogContext context, IAwaitable<IActivity> result) { IActivity message = await result; string reply = ""; if (tasks.Unreaded) { reply = "           "; await context.PostAsync(reply); reply = tasks.Tasks[tasks.Number].Condition; await context.PostAsync(reply); tasks.Unreaded = false; context.Wait(MessageRecieveAsync); } else if (Helpers.NumParser(message.AsMessageActivity().Text) == tasks.Tasks[tasks.Number].Answer) { reply = "  !"; await context.PostAsync(reply); tasks.Tasking = false; context.Done(this); return; } else { reply = ".    . \n\n\u200C" + tasks.Tasks[tasks.Number].Explanation + "\n\n\u200C    "; await context.PostAsync(reply); context.Done(this); return; } } 

As Ada saw


Besides the fact that Ada learned to ask the interlocutors of the task, she also learned to see. If you send her some picture, Ada will see it and be able to say what is depicted on it. The vision of Hell was given thanks to Microsoft Cognitive Services . To implement the vision of Ada, we used the Vision API. If there are people in the picture, she will be able to appreciate the emotional mood of the people in the picture.


In order for the bot to recognize the picture, we must read the attached files to the message in the messenger. We do it like this:


The code is here.
 if (activity.Attachments?.Any() == true) 

If Attachments will have an attached file, in particular an image, then first we create an http-client, through which we will send a photo. Then open the Stream for the photo and create the file by reference. The recognition itself will occur according to the Description obtained after recognition of the picture.


The code is here.
 private async System.Threading.Tasks.Task StartRecognize(MemoryStream photo) { try { \_analysisResult = await \_visionServiceClient.DescribeAsync(photo); \_isVision = true; } catch { } } public async Task<string> MakeSomeSummary(MemoryStream photo) { string result = "nothing"; await StartRecognize(photo); if (\_isVision) { if (\_analysisResult.Description.Captions.Length > 0) { result = \_analysisResult.Description.Captions[0].Text; } } return await Helpers.TranslateText(result, "ru", await Helpers.GetAuthenticationToken("SubKey")); } 

In order for Ada to respond to us in Russian, we will translate the Description using the Translation API .


The code is here.
 public static async Task<string> TranslateText(string inputText, string language, string accessToken) { string result = ""; string url = " [http://api.microsofttranslator.com/v2/Http.svc/Translate](http://api.microsofttranslator.com/v2/Http.svc/Translate)"; string query = $"?text={System.Net.WebUtility.UrlEncode(inputText)}&to={language}&contentType=text/plain"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await client.GetAsync(url + query); result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) return "Hata: " + result; var translatedText = XElement.Parse(result).Value; return translatedText; } return result; } public static async Task<string> GetAuthenticationToken(string key) { string endpoint = " [https://api.cognitive.microsoft.com/sts/v1.0/issueToken](https://api.cognitive.microsoft.com/sts/v1.0/issueToken)"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); var response = await client.PostAsync(endpoint, null); var token = await response.Content.ReadAsStringAsync(); return token; } } 

After recognition, we create an answer and check if we have emotions. It is much more interesting here. Ada will recognize the average emotional activity throughout the picture, recognize all faces and determine which emotion dominates the picture. We collect all the emotions on each face and find the average temperature in the hospital according to the arithmetic average of each emotion ;.


The code is here.
 public async Task<string> MakeAboveEmotion(MemoryStream photo) { string result = "nothing"; await StartRecognize(photo); if (\_isEmotion) { double[] aboveEmo = new double[8]; Emotions.ToList().ForEach(x => { aboveEmo[0] += x.Scores.Anger; }); Emotions.ToList().ForEach(x => { aboveEmo[1] += x.Scores.Contempt; }); Emotions.ToList().ForEach(x => { aboveEmo[2] += x.Scores.Disgust; }); Emotions.ToList().ForEach(x => { aboveEmo[3] += x.Scores.Fear; }); Emotions.ToList().ForEach(x => { aboveEmo[4] += x.Scores.Happiness; }); Emotions.ToList().ForEach(x => { aboveEmo[5] += x.Scores.Neutral; }); Emotions.ToList().ForEach(x => { aboveEmo[6] += x.Scores.Sadness; }); Emotions.ToList().ForEach(x => { aboveEmo[7] += x.Scores.Surprise; }); int mx = aboveEmo.ToList().IndexOf(aboveEmo.ToList().Max()); switch (mx) { case 0: result = "anger emotions"; break; case 1: result = "contempt emotions"; break; case 2: result = "disgust emotions"; break; case 3: result = "fear emotions"; break; case 4: result = "happiness emotions"; break; case 5: result = "neutral emotions"; break; case 6: result = "sadness emotions";; break; case 7: result = "surprise emotions"; break; default: result = "nothing"; break; } } return await Helpers.TranslateText(result, "ru", await Helpers.GetAuthenticationToken("SubKey")); } 

As ada said


Do not forget about the main functionality of the bot. In the new version, the guys tried to expand the base of intents. Now you can hear Ada Lovelace quotes. For example, one of the questions you can get an answer that contains facts about the life of the world's first programmer.


For example, here is a quote about mathematics.

For those who consider mathematical science not only a huge set of abstract and immutable truths, its inner beauty, symmetry and logical completeness, considered in their connection with each other and as a whole, meet the interests of deep logical minds, but mathematics conceals an even deeper of interest to humanity, if we recall that this science is a language through which we can single-handedly adequately express the great facts of the natural world and these incessant changes in interconnections that are clearly or not Discriminately, consciously or unconsciously occur around us, in our immediate physical perception: those who meditate in this way think of mathematical truth as a tool through which the weak mind of a person can most effectively read the works of his Creator will be treated with a particular interest to everything which can facilitate the understanding of the principles of nature, bringing them into obvious practical forms.


We hope that this will help you to plunge into the thoughts of the great mathematician. Of course, it was not done without expanding the base of the main answers.


You can talk to Ada in Telegram: @adalovelacebot_bot


If you want to participate in improving the bot, pull requests are welcome .


About the authors



')

Source: https://habr.com/ru/post/341384/


All Articles