LUIS and .NET

Language Understanding (LUIS)

Language Understanding Intelligent Service (LUIS) gives your applications the ability to understand what a person wants in their own words. LUIS makes use of machine learning. This enables developers to build applications that can receive user input in a natural language form and extract meaning from it.

LUIS Applications

A LUIS app is a domain-specific language model. A model can start with a list of general user intentions (or actions) such as “Order Coffee” or “Cancel Meeting.” Once the intentions are identified, you can supply text inputs or utterances for the intents. The utterances get labeled with specific details you want LUIS app to pull out of the utterance.

After the model has been trained and published, it is ready to receive and process utterances. The LUIS app receives the utterance as an HTTP request and responds with extracted user intentions. This happens in the form of a JSON object.

LUIS Concepts

Intents

An intent represents actions the user wants to perform, such as ordering coffee or cancelling meetings.

Utterances

An utterance is text input for the intent that your app needs to understand. For example: “Cancel meeting with Peter at 10,” or a piece of a sentence, like “at 10” or “with Peter.”

Entities

An entity represents the detailed information that is relevant in the utterance. For example, in the utterance “Cancel meeting with Peter at 10,” “Peter” is a person and “10” is a time.

Creating a LUIS App

To get started creating LUIS apps, you would need to have a Microsoft Azure account and create a Cognitive Services API account in the Azure portal.

Microsoft Azure dashboard
Figure 1: Microsoft Azure dashboard

Then, you will need to create LUIS.ai account.

Sign up for LUIS
Figure 2: Sign up for LUIS

When you click the link to create a LUIS.ai account, the screen in Figure 2 will appear. You have to either Log in or Sign up.

You will have to give the LUIS.ai app the necessary permissions to start using it, as shown in Figure 3.

LUIS.ai settings
Figure 3: LUIS.ai settings

Now, you are ready to start creating LUIS apps. Figure 4 shows that you can create a new app or import an existing app. Click Create new app.

Create new app
Figure 4: Create new app

Fill in the app’s details

App Details
Figure 5: App Details

The details are quite self-explanatory and surprisingly easy and quick to fill in. Once you have your app set up, you need to start creating Intents.

Choose Intents under the App Assets menu (see Figure 6) to be able to create a new Intent (see Figure 7).

Create intent
Figure 6: Create intent

New Intent
Figure 7: New Intent

The next screen will allow you to enter terms which a potential user might use to activate this task. For example: “Order,” “Buy,” or “Delete.” You will see that I have kept my examples quite simple (perhaps a bit too simple).

Trigger Terms
Figure 8: Trigger Terms

Train your intents.

Training
Figure 9: Training

Add and edit Entities:

Entities
Figure 10: Entities

New Entity
Figure 11: New Entity

Set up a Phrase list so that your app can understand more phrases.

Phrase List
Figure 12: Phrase List

New Phrase list
Figure 13: New Phrase list

Verify all your settings for your app is correct, then publish.

Verify settings
Figure 14: Verify settings

Publish
Figure 15: Publish

Now that you have published your LUIS app, you need to write a standalone app based on the LUIS.ai app you have set up.

Start Visual Studio and create either a C# or VB.NET Console application. Once the application is loaded, set a reference to the System.Web namespace, by selecting Project, Add Reference and ticking the appropriate checkbox:

Add Reference
Figure 16: Add Reference

Add the following code.

C#:

using System;
using System.Net.Http;
using System.Web;

namespace LUIS_Sample
{
   class Program
   {
      static void Main(string[] args)
      {
         ConnectToLUIS();
         Console.ReadLine();
      }

      static async void ConnectToLUIS()
      {
         var hcLUIS = new HttpClient();
         var strQuery = HttpUtility.ParseQueryString(string.Empty);

         var ID = "d0dcd219-050f-458f-83a5-df7da3757248";
         var Key = "709fe955f19e466785ed6567b1c3c7b4";

         hcLUIS.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-
            Key", Key);

         strQuery["q"] = "excuse me";

         strQuery["timezoneOffset"] = "0";
         strQuery["staging"] = "false";

         var URL = "https://westus.api.cognitive.microsoft.com/
            luis/v2.0/apps/" + ID + "?" + strQuery;
         var res = await hcLUIS.GetAsync(URL);

         var strContent = await res.Content.ReadAsStringAsync();

         Console.WriteLine(strContent.ToString());
      }
   }
}

VB.NET:

Imports System.Net.Http
Imports System.Web

Module Module1

   Sub Main()

      ConnectToLUIS()

      Console.ReadLine()

   End Sub

   Private Async Sub ConnectToLUIS()

      Dim hcLUIS = New HttpClient()
      Dim strQuery = HttpUtility.ParseQueryString(String.Empty)

      Dim ID = "d0dcd219-050f-458f-83a5-df7da3757248"
      Dim Key = "709fe955f19e466785ed6567b1c3c7b4"

      hcLUIS.DefaultRequestHeaders.Add("Ocp-Apim-Subscription- _
         Key", Key)

      strQuery("q") = "excuse me"

      strQuery("timezoneOffset") = "0"
      strQuery("staging") = "false"

      Dim URL = "https://westus.api.cognitive.microsoft.com/luis/ _
         v2.0/apps/" & ID & "?" + strQuery

      Dim res = Await hcLUIS.GetAsync(URL)

      Dim strContent = Await res.Content.ReadAsStringAsync()

      Console.WriteLine(strContent.ToString())

   End Sub

End Module

You supply the Key and App ID given to you when you created your LUIS.ai app, add another phrase, and connect to the app’s endpoint. The app then waits for a response from your published LUIS.ai app and displays it.

The code for this article is available on GitHub:

VB.NET

C#

Conclusion

LUIS plays a major part in the online world we live in today. The more we can train computers to understand us and to respond properly, the easier it will become for computers and humans to live in unison. AI is here to stay, and day by day, systems become cleverer and more human. Thanks, Microsoft.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read