This post is part of a series on creating a natural language processor in C#. The other entries in this series are:
Writing a Natural Language Parser in C# Part 1–Why?
Writing a Natural Language Parser in C# Part 2 - Architecture
Writing a Natural Language Parser in C# Part 4–Tokens
Writing a Natural Language Parser in C# Part 5 - Questions and Rules
The command processor is is the class that controls the flow of parsing a request and replying to it. Below is a full listing of the class.
CommandProcessor class
- [Export]
- [PartCreationPolicy(CreationPolicy.NonShared)]
- public class CommandProcessor
- {
- [Import]
- private QuestionManager _questionManager { get; set; }
-
- public void ProcessCommand(string command, Guid userId,
- SmartHome.Global.ConversationMode mode,
- string address,
- Action<string> callback)
- {
- string localCommand = command.ToLower().Replace("'", "");
- var context = ServiceLocator.GetInstance<ConversationContext>();
- context.Init(userId, mode, address, callback);
- var tokenManager = ServiceLocator.GetInstance<TokenManager>();
- var buckets = tokenManager.TokenizeInput(command, userId);
- context.LogRequest(command, buckets);
-
- //before we check the tokens against rules, let's see if we match any questions
- Question question = null;
- List<Token> tokens = new List<Token>();
-
- _questionManager.CheckForMatchingQuestion(buckets, mode, userId,
- address, ref question, ref tokens);
-
- if (question != null)
- {
- question.ExecuteIfAnswered.Invoke(context,
- question.State, tokens);
-
- _questionManager.RemoveQuestion(question);
-
- return;
- }
-
- RuleMethod ruleMethod = RuleManager.LocateMatchingRule(buckets, context);
-
- if (ruleMethod != null)
- {
- ruleMethod.Rule.Invoke(null, ruleMethod.PassIns);
- }
- else
- {
- context.Say("I didn't understand your request", null);
- }
- }
- }
First of all, you can see that MEF is being used as an IOC container. The only method in this class is the ProcessCommand method that takes the following parameters.
- A string that is the command the user sent in, “What’s the weather” for example.
- The user id. This is a Guid because we are using the Membership provider for authentication.
- The conversation mode is specified, next, as an enumeration. The values in the enumeration specify what channel the user specified the command through (email, IM, etc.).
- An address is specified as a string for the next parameter. We can tell from the mode what channel the user is on, but if they have multiple IM accounts or email addresses, we also need the address to figure out how to reply to them on the right account.
- Last is a call back to be called after the command is processed. This is primarily here for commands sent to the system on a duplex WCF service. In order to reply to the user on this service, we need to be able to respond on the specified interface defined in the WCF service. This callback allows that.
To start off, we clean the command up a bit by making it lower case and removing apostrophes. Now, we create an instance of ConversationContext and initialize it. We’ll look at this class in just a minute. Next, we create the TokenManager and pass the user’s command to it. What is passes us back is a Dictionary<int, List<TokenResult>> where the int specifies a start position where tokens were found and the List contains all the tokens that were found there. We’ll look at the tokenization of the string in detail in the next part in this series.
Since the system saves all messages sent by the system or the user, we next log the users request. The buckets are passed in because they will be serialized and saved along with the command. When future requests come in, this will allow us to pull any past request from the database, de-serialize it and inspect it.
In some cases, the system will ask clarifying questions of the user. For example, if you as the system to remind you of something, it will need to know when to remind you and how. So, it will ask you these questions and wait for your reply. In the next code block we use the QuestionManager to check and see if the current request is actually the answer to a previously asked question.
If the command is not the answer to a question, we need to check and see if it matches one of the rules we have defined. Rules are how you specify sentences the system should recognize and what it should do when it receives that sentence. The RuleManager class compares our buckets with the rules to find a match. If one is found, it is invoked. Otherwise, the system replies that it did not understand the command.
Notice that the response is sent via the Say method on the ConversationContext class. Since this functionality completes the processing cycle, let’s look at that class next.
ConversationContext
As has been said before, the purpose of this class is to manage the conversation’s details such as the user it belongs to, it’s mode, address and history. Below is a listing of this class.
ConversationContext Class
- [Export]
- [PartCreationPolicy(CreationPolicy.NonShared)]
- public class ConversationContext
- {
- public User ConversationUser { get; set; }
- public Conversation Conversation { get; set; }
- public List<ConversationHistory> ConversationHistory { get; set; }
- public SmartHome.Global.ConversationMode Mode { get; set; }
- public Action<string> Callback { get; set; }
- public string Address { get; set; }
-
- [Import]
- private IEventAggregator EventAggregator { get; set; }
-
- [Import]
- public QuestionManager QuestionManagerReference { get; set; }
-
- public void Init(Guid userId, SmartHome.Global.ConversationMode mode,
- string address, Action<string> callback)
- {
- ConversationUser = UserData.GetUserByUserId(userId);
- Conversation = ConversationData.GetConversationByUserAndMode(
- userId, mode, address);
- ConversationHistory = ConversationData.GetConversationHistory(
- Conversation.ConversationId);
- Mode = mode;
- Callback = callback;
- Address = address;
- }
-
- public void LogRequest(string request, object tag)
- {
- string tagString = SerializeTag(tag);
- var history = ConversationData.CreateConversationHistory(
- Conversation.ConversationId, request, tagString, tag, true);
- ConversationHistory.Add(history);
- }
-
- public void Say(string comment, object tag)
- {
- string tagString = string.Empty;
- tagString = SerializeTag(tag);
-
- EventAggregator.GetEvent<ReplyToChannelEvent>().Publish
- (new ReplyToChannelEventArgs
- {
- Mode = (SmartHome.Global.ConversationMode)Conversation.Mode,
- Reply = comment,
- TagString = tagString,
- Tag = tag,
- ConversationId = Conversation.ConversationId,
- Callback = Callback,
- UserId = ConversationUser.UserId,
- Address = Address
- });
- }
-
- private string SerializeTag(object tag)
- {
- string tagString = string.Empty;
-
- if (tag != null)
- {
- if (tag is IEntityWithChangeTracker)
- (tag as IEntityWithChangeTracker).SetChangeTracker(null);
- var ser = new DataContractSerializer(tag.GetType());
- var ms = new MemoryStream();
- ser.WriteObject(ms, tag);
- tagString = Encoding.Default.GetString(ms.ToArray());
- }
-
- return tagString;
- }
-
- public void AskQuestion(string text, List<Token> expectedReplies, object state,
- Action<ConversationContext, object, List<Token>> executeOnAnswer)
- {
- Question question = new Question
- {
- Address = Address,
- ExecuteIfAnswered = executeOnAnswer,
- ExpectedReplys = expectedReplies,
- Mode = Mode,
- PosedDateTime = DateTime.Now,
- UserId = ConversationUser.UserId,
- State = state,
- QuestionText = text
- };
-
- QuestionManagerReference.AddQuestion(question);
-
- Say(text, null);
- }
- }
At the top of this class are instance variables that hold the instance’s state. The Init method initializes these variables.
Next is the LogRequest method we saw earlier that serializes the tag that is passed in (the buckets) and writes the request to the database.
The next method is Say(). We saw this method being used in the CommandProcessor class to reply to the user. It’s also used by the various rules for the same purpose. If you as the system about the weather, it will send the details back to you with the Say method. As you can see, I’m using the EventAggregator from the PRISM library to make the component of the system more loosely coupled. The say method serializes the tag parameter just like the LogRequest method does and then it raises an event asking someone to reply to a user. There are various classes responsible for communicating with the user on different channels. These classes are listening for this event and when they receive it, they will inspect the event args to see if they should respond. If so, they will send the response to the user and log the communication to the database.
Context
Looking at the listing, the next method is a helper that serializes objects into xml so they can be written to the database. This is a very important piece of functionality. When a rule is invoked by the system, it is passed an instance of the ConversationContext class. You can see the ConversationHistory property on this class which contains the entire history of the conversation. Most of these entries have a piece of context that helps to either explain why the system responded the way it did or to store a piece of information that may be useful later. For example, suppose you ask the system to list all the reminders it has for you. These will be returned to you in a numbered list. Suppose you’d like to delete the third item in the list. You could do that by telling the system to “delete 3”. How does the system know what was listed, much less which in the list was number three? It does this by walking backward through the conversation history until it finds a list that was stored in the tag. It then uses that list.
As another example, suppose you ask the system to remind you to meet with Phil. The system will ask you what time you would like to be reminded. You reply that you’d like to be reminded at 4:00 pm. How does the system know what you’re talking about? You guessed it. By storing the reminder as a tag. This is a powerful idea and allows the system to have a stateful conversation via a stateless protocol in a similar way that ASP.Net does with its serialized state.
The last method in the ConversationContext class is AskQuestion(). This is similar to Say() in that it also raised the event to have the reply sent back to the user. However, it also adds the question to a collection maintained by the QuestionManager.
Next time, we’ll look at the TokenManager and how the commands are tokenized.