AI Zone Admin Forum Add your forum

NEWS: survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Creating a Chatbot in VB/Access - Where do I Start?
  [ # 31 ]

Task 10. How do we “ThinkHardAbout” something? We compare each of our BrainPatterns to WhatWasSaid and look for a match.  This is how (copy and add to your form’s code):

Private Sub ThinkHardAbout(WhatWasSaid As String)

  Dim Thought As Integer

  For Thought = 1 To Brain.IQ
      Brain.CurrentThought = BrainPatterns(Thought)
      If DoesMatch(WhatWasSaid, Brain.CurrentThought.Pattern) Then
        Brain.AppropriateResponse = Brain.CurrentThought.Response
        Exit For
      End If
End Sub


  [ # 32 ]

Task 11. We need logic for the DoesMatch function. Copy and paste this into your form’s code:

Private Function DoesMatch(WhatWasSaid As String, Pattern As String) As Boolean

  If WhatWasSaid = Pattern Then
      DoesMatch = True
      DoesMatch = False
  End If

End Function


  [ # 33 ]

Task 12. We called “InitializeChatHistory” in our On Load event code so we need to add a Sub for that.  We iniitalize the rows with spaces and line feeds so that the responses appear first at the bottom of the chat history textbox and then scroll up as they are replaced.

Private Sub InitializeChatHistory()

  Dim ChatHistoryRow As Integer
  For ChatHistoryRow = 1 To ChatHistoryMaxRows
      ChatHistory(ChatHistoryRow) = ” ” + vbCrLf

End Sub


  [ # 34 ]

Task 13. Now our chatbot has hopefully found a matching response it needs to be able to say it.  The following code updates the Chat History and adds the user’s statement and the Chatbot’s reply.  Cut and paste the following into your form’s code:

Private Sub Say(Response As String)

  Dim ChatHistoryRow As Integer

  Me.txtChatHistory = “”

  For ChatHistoryRow = 1 To ChatHistoryMaxRows - 2
      ChatHistory(ChatHistoryRow) = ChatHistory(ChatHistoryRow + 2)
      Me.txtChatHistory = Me.txtChatHistory + ChatHistory(ChatHistoryRow)
  If Nz(Me.txtChat.Text,””) <> “” Then ChatHistory(ChatHistoryMaxRows - 1) = Brain.YourName + “: ” + Me.txtChat.Text + vbCrLf
  Me.txtChatHistory = Me.txtChatHistory + ChatHistory(ChatHistoryMaxRows - 1)
  ChatHistory(ChatHistoryMaxRows) = Brain.MyName + “: ” + Nz(Response, “”) + vbCrLf
  Me.txtChatHistory = Me.txtChatHistory + ChatHistory(ChatHistoryMaxRows)
  Me.txtChat = “”

End Sub


  [ # 35 ]

Task 14. Now you are done! Open your form and chat away, enjoying hours of engaging conversation.  At this point you may want to make your form nicer, positioning and aligning your textboxes, adding a left margin to the textboxes so that the text is not touching the left side, add a picture for your chatbot, etc.  You may find you want to adjust the language of some of the patterns and responses slightly, and just maybe your chatbot’s conversation is not as engaging as you desired and you might decide you need to increase your chatbot’s knowledge by adding even more BrainPatterns!


  [ # 36 ]

Task 15. But wait! This chatbot it too simple you say?  You notice that you must type the pattern to be matched EXACTLY or the chatbot will not respond appropriately?  You changed your mind and you want a more advanced chatbot.  OK.  We will add the ability to use regular expressions in the pattern matching of your chatbot.  Copy and paste the following code (taken from links referenced below and modified) and replace the DoesMatch function:

Public Function DoesMatch(Statement As String, RegExPattern As String) As Boolean

  Dim re As RegExp
  Dim matches As MatchCollection
  Set re = New RegExp
  re.IgnoreCase = True
  re.Global = True
  re.Pattern = RegExPattern
  Set matches = re.Execute(Statement)
  If matches.Count = 0 Then
      DoesMatch = False
      Exit Function
  End If
  DoesMatch = True
End Function

      a. You will get an error: User Defined Type Not Allowed.
          i. After stopping execution of your code and while still viewing the code for your form select the “Tools” option from the top menu bar.  Select “References”.  Scroll way down to the the references starting with “M” and check off the one called “Microsoft VBScript Regular Expressions 1.0”.  Save. Close and reopen your form.
      b. See link here:
      c. For syntax info see here:
      d. For a simple reference see here:
      e. Google regular expressions for more info than you ever wanted on regular expressions.
      f. Try entering “.*\bcat\b.*” as a pattern and “You used the word cat in a sentence.” as the response.
      g. Try entering “.*\cat.*” as a pattern to match anything starting with “cat” so this would match “cat” and “catherine”.
      h. Try entering “.*\cat\b.*\bdog\b.*” as a pattern to match any sentence with the word “cat” in it followed by the word “dog”.

Now the sky’s the limit!   


 < 1 2 3
3 of 3
  login or register to react