UsingLists

Making our Chatbot Smarter - (using - specialFolderPath, Repeat For Each, Exit Repeat) 

We can make our chatbot smarter by adding more words and responses. We do that by adding more code. How many words do we need to have a smart chatbot?

Right now, our chatbot has the intelligence of a 1 year old. To make it smart, we have to add a LOT OF WORDS. (see below). That is one heck of a lot of coding to do.



A 6 year old knows 20,000 words !  Do we have to add 20,000 else-if's and code?


How can we work smarter and make our job easier?

We can use a list of words and responses and work from that. 
By putting our words and responses in a list or table or spreadsheet, we can add intelligence without having to add more code. 
We just write a small chunk of code to use that list.



Exercise: (highly recommended if you want to understand this)
 
       Download and Try Animal Wizard - Look at the code. We are going to do the same to our Chatbot




Changing Our Chatbot to Use a List/Table/Spreadsheet
We are now going to add the same ideas to our chatbots. You can add the code to your current chatbot or start anew with the chatbot below.

In Excel Spreadsheet:

          1. Create a file for your new keywords and responses. ( I am going to use a Excel spread sheet to start with) The first column will be the words, the 2nd column will be the responses

                                          

          When done, save it as a tab-delimited "txt" file

                                        

                        Name it and Save it:


                                                   

    A "tab" will separate the 2 items - the "Word" and the "Response" (as seen in LiveCode and Notepad). That is how we can separate the columns and tell the WORD from the RESPONSE

                      


2. Change the code in your program to read in the file of words/responses into a variable (myList)

   In the Card Script

      Add the global myList to the top of the card script


         e.g.  (old code is black, your new code is red )

global theInput
global theResponse
global myList



        Add this to the beginning of the "openCard" handler (It opens the file (your list of words and responses, and puts it into the variable "myList")

   put specialFolderPath("Desktop") & "/" & "myChatbot.txt" into myFile
   put URL ( "file:" & myFile ) into myList 


         e.g.  (old code is black, your new code is red )

on openCard
   put specialFolderPath("Desktop") & "/" & "myChatbot.txt" into myFile
   put URL ( "file:" & myFile ) into myList 

   put "Hello, Let's talk" & return into field "log"
   select before line 1 of  field "input"              // put the cursor at the start of the field
end openCard
  


2. Add the code to search thru your list at the beginning of "getResponse"


         e.g.  (old code is black, your new code is red )

on getResponse
   put field "input" into theInput                       // save what the user typed
   put theInput & return after field "log"           // put it into the log and go to next line
   put empty into theResponse                        // clear out the response variable
   global x

   repeat for each word w in theInput              // check each word in the user's sentence
      put findWordInList(w) into theResponse   // try to find the word in our list or words/response
      if theResponse is not empty then             // if we returned a response
         exit repeat                                         // ... we found a word and are done
      end if
   end repeat

  if theResponse is empty then                                  // it did not find a word/response in your list, try more words

         if "no" is in theInput  then
         ....
(all your other tests are still good)



Add another end if at the end of your searches



         e.g.  (old code is black, your new code is red )

            put "Tell me more about your family" into theResponse
         else 
            getRandomResponse
         end if
 end if



add a new function to search  your list of words - "findWordInList" in between any other functions or handlers
            remember - In our file, we have the word - (a tab) -then the response


        e.g.  (old code is black, your new code is red )


                     ...
end getResponse

function findWordInList theWord                       // you call it with a word e.g. 
   set itemDelimiter to tab                                // we used a "tab" to separate the columns
   repeat for each line x in myList                      // go through every line in the list
      if theWord = item 1 of x then                     // item #1 = the WORD
         return item 2 of x                                   // if found, return item #2 - the RESPONSE
      end if
   end repeat
   return empty                                             // not found - return nothing (empty)
end findWordInList

on getRandomResponse
   put random(4) into x
                         ...


ċ
cyril.pruszko@pgcps.org,
Mar 2, 2016, 6:08 AM
Comments