by Tika Carr
June 2, 2004

Introduction

This article will discuss ways of programming an AI system. The idea for this is to create a talking car that can interact with it's driver and others. However, these are the basics I've been working out in my mind and these basics could be applied to any other application, such as talking computers, robots, and others. I have not formally studied AI programming, but I have had years of experience in computer programming in general. I started out on my parents' TRS-80 Model III computer with only 16K memory and a cassette recorder for storage. Back then, my parents bought workbooks on how to program in TRS-80 Level II BASIC and also books filled with programming code to be typed into the computer. I spent hours doing just that and even modifying the programs afterwards. This is how I first learned to program.

Please keep in mind that this article can get quite technical and might be hard for some to follow my logic. This is just my theory on building an AI system and I have not actually tested this in code. I am first to admit that English was not my top subject in school therefore I may have forgotten some of the more finer analysis of the English language. I usually tend to draw on my experience as a writer. In the past I've written articles for some print and online computer magazines.

Eliza - The Basic AI

Back in the TRS-80 days, there was a program called "Eliza" in one of the books my parents bought. It was quite a long program but I spent a few hours typing it in and saved it to cassette. The actual fuzzy logic code was short and simple, composed of string searches using the INSTRING$ function which returned an integer value to denote whether or not the particular string was found in another string. By using this built-in function, you could see if the word "love" was in the sentence "I love my car." for example. The rest of the program was just storing keywords, pronouns, adverbs, responses and the like into DATA statements which were then read into arrays. We often had a great time playing with this program and having the computer talk back to us via the CRT, often carrying on a conversation. Unfortunately, due to the simplicity of the program, some of its replies could be regarded as rather insulting or inappropriate.

After a few years I had moved on to the PC compatible computers and started programming in MicroSoft's QuickBasic 4.5. Also at that time I had obtained a Sound Blaster 16 sound card for my computer I had at the time, along with a 2x CD-ROM drive as part of a Multimedia Upgrade Kit. Quite a few programs and utilities came with the kit, including a program called Dr. SBaitso. This program used the same Eliza type technique to allow you to carry on a conversation with the computer. You could type your questions and responses into the keyboard and the computer would use the sound card and a text-to-speech engine (software) to speak back it's own replies. I was very intrigued by this, even though the TTS engine was quite crude at best. Eventually, I went on to convert the TRS-80 Model III Eliza program to QuickBasic 4.5 and then I further developed it in an effort to make the computer less insulting in it's replies or remarks. I still kept to the same basic Eliza AI technology, however. I also had not developed it to work with the SoundBlaster's TTS software. After a few more years, I had lost interest and unfortunately have deleted all of the code. But I don't think it would take me too long to rewrite new code based on the same principles as the original Eliza. In fact, I have an even better idea.

Using Databases

I have recently started working with php/MySQL database programming and have installed a few php/MySQL applications for web use. I have web and MySQL servers on my system to develop and test code with. I have been thinking that a good AI system would be more accurate in it's interactions with humans if it could access, store and retrieve from a database, such as MySQL. Of course this would have to be the AI's memory bank. The fact that SQL is designed in such a way that using keyboard searches are very quick, this would give an AI system greater power and speed. An AI system (hereafter referred to as just AI) could parse user input and use keywords in a database to look up related topics that were discussed with this user before or look up synonyms or antonyms, depending on the 'mood' of the conversation. From this, the AI could put together responses or even start conversations at random using semi-randomly chosen data from it's database. This would be far quicker and more powerful than the Eliza technique of using limited fixed data read into arrays. With a database, the AI is only limited to the amount of storage space it's equipped with. Furthermore, it could also be programmed to take keywords, key ideas and phrases from conversations and store them in areas that are found using certain keywords, thus expanding it's vocabulary and idea base. This would be how the AI could "learn".

Remembering People and Conversations

Because of the power of database storage, an AI could easily remember a person and the conversations that was had by the person. For example, let's say Lisa was introduced to the AI. Lisa and the AI had a conversation about strawberry bubble gum. In the conversation Lisa told the AI that she enjoys the flavor but some gums are too sweet for her. The AI also was told that Lisa was told by her dentist to chew less gum or a sugarless gum due to her dental problems. When the AI asked her what the problems were, Lisa confided that she had a cavity filled during her last visit to the dentist. For this example, let's keep it simple and assume that Lisa talked to the AI using a keyboard and not voice. Now, let's take a look at how the AI would have stored that information:

Name picked up by the AI: Lisa

User's gender that the AI figured out from a list of names or by asking: female

Nouns picked up by the AI: strawberry, bubble gum, dentist, flavor, gum, cavity, problem(s)

Adjectives picked up by the AI: enjoys, sweet, too, less, sugarless, told

Verbs picked up by the AI: enjoys, chew, filled, visit

And so forth...


The AI could then look up each word and add the keyword "lisa" to each word, if it's not there already. This would be a bit limited. Another approach would be to create a table called "Lisa" and store the whole conversation. This could eat up a lot of space in the database, however.

A more efficient way would be for the AI to look at adjectives, and follow the conversation to find phrases such as looking up "enjoy" in the conversation and then look at the next word "the", notes that it's a pronoun, stores it after "enjoy" in the "key phrase" temp variable, then goes to the next word and sees "flavor". So already it has the key phrase "enjoy the flavor" in it's temp variable. Now suppose the sentence was actually "I enjoy the flavor of strawberry bubble gum but some gums are too sweet for me." The key phrase here would be "enjoy the flavor of strawberry bubble gum". This would be linked to the keyword "Lisa" since the AI would note that "I" means "Lisa", whom the AI was conversing with. And so now the AI knows that Lisa likes strawberry bubble gum. If the AI encounters a punctuation (denoting end of that sentence) it could stop there. However, the AI did not see an end to the sentence so it goes on to the next word, "but". Now the AI could handle this by looking at the "mood" of the key phrase. It looks for a verb, which was "enjoy". It can look up in it's dictionary (described later) and see that "enjoy" is a positive verb. So the sentence started out as positive. When it encounters the word "but", the AI will assume an opposite mood change. Thus it will treat the rest of the sentence as a separate sentence, but will link it to the first part of it. The first part of the sentence being a positive integer and the second part of the linked sentences being a negative integer. Now the AI knows that Lisa likes strawberry bubble gum but in the same analysis it knows that there is a problem with some being too sweet.

This is a rough sketch of how an AI would use a database and fuzzy logic techniques to store only key phrases of information in the database in reference to a certain person. So in future conversations with Lisa, the AI could use a prolonged silence to randomly first decide whether or not to break the silence and then if so, if it randomly looked at that conversation, it could draw out the adjective 'enjoys' along with the noun 'strawberry bubble gum' and ask "Lisa, have you enjoyed a piece of strawberry bubble gum lately?" The AI then could look at the negative linked sentence and note that it might have been too sweet. Another link might have been to her filled cavity. So if Lisa said "yes" the AI would remind her "That is good. But be sure to visit your dentist again soon." or if she said "No, I'm staying away from gum for now." The AI could say "I can understand. I know you don't like gum that is too sweet."

Seeding Conversations and Set-Ups

Using the above technique, an AI could also be able to keep a conversation on topic. The key phrases could be kept in categories. An AI could also string along a conversation to which the ends justify the means. For example, I was conversing with someone in email and gave the example of a talking car. Here is a sample conversation which the AI would start by itself, after a long silence and a random decision to break the silence. The AI in this example sees that in a previous conversation the driver had promised the AI car a car wash. The AI not only linked this to the driver, but also to a "To Do" list of sorts, and drew on that to start the conversation that follows:

AI: Do you remember last weekend when you promised me a car wash?

Driver: Yeah?

AI: Well... I am still quite a bit dusty.

Driver: You want a car wash now? I'm on my way to work!


At this point the AI sees that the driver is protesting and the sentence seems to been given a negative 'weight', that is, that the driver is mentioning they are going to a destination different from the car wash. The AI searches for relative information regarding the driver and work. It keeps 'car wash' as it's 'end topic' and now 'work', which seems to be the obstacle or alternate destination as the 'start topic'. So the AI also looks up car wash and sees keywords as 'wash', 'clean', 'appearance', 'clothing', 'jewelry', etc. It searches for keywords that match keywords associated with 'work' for other conversations with the driver. It might find that the driver mentioned wearing a Plaid Jacket to work one day and it held a negative weight due to co-workers not liking the pattern or color. The AI sees: Jacket-Clothing-Appearance-Clean-Wash, going backwards. Since it fits, it decides to ask about the Plaid Jacket and work. It saw 'wore' in the past conversation so now it has: Plaid Jacket, Wore, Work. It then fills in the sentence structure:

AI: You're not wearing that ugly plaid jacket to work again, are you?

Driver: No, but I do love that jacket!


The AI stays on topic noting how much the Driver loves the jacket. And goes on to try and link the jacket with the start topic 'car wash' using the keyword 'wash'.

AI: What if your jacket needed washing before you go to work?

Driver: Then I'd wash it, dummy!

AI: So you'd wash that ugly old jacket before you'd go to work but you'd never think to wash me before you go to work?


At the last sentence, the AI already knew that it would ask a question that would bring the two topics, 'work' and 'car wash' (me = car) in alignment. In doing so it also noted the conversation started to have a negative weight due to the fact the destination was not the car wash. So the AI would choose a negative tone of voice, such as insult or distaste type of tone while vocalizing that last sentence. This would give the impression that:

  1. The AI stood on topic
  2. The AI had 'set up' the driver, by aligning the topics.
  3. In doing so, the AI had provided a defensive case on it's own behalf to get a car wash, since logically if Driver washes jacket before work, then Driver should wash car (the AI) before work.

Linking Key Phrases

As noted previously, key phrases can be linked to each other, especially if they happen to be in the same conversation or even the same sentence broken up into positive and negative weights. This can help an AI follow along in a conversation and keep on topic, as well as learn and show recognition of a person's preferences, traits and habits. In the Bubble gum conversation, there was a sentence which held a positive and a negative statement. However, any other key phrases in that same conversation could be linked to each other. Not only did Lisa find some gums too sweet, she found she had to have a cavity filled and it seemed like the gum was the cause. So the filled cavity key phrase would also be linked to the enjoyment of bubble gum key phrase. This helps the AI make comparisons and gives the AI more possible keywords to build a response sentence from.

Using A Weight System to Determine Mood

Using a weight system for key phrases can further help the AI determine the mood of a conversation. As we've seen above, it can help the AI set up a conversation and stay on topic, as well as seem to 'learn' someone's preferences. A weight system would attach a positive or negative number to each key phrase.

In the case of a whole conversation, the AI could count up the number of positive and negative weights and see what was greater. This is how the AI could tell the overall mood of the conversation. From this you could set the AI to have a threshold where if the Positive to Negative ratio is a certain amount, the AI could refuse to discuss the topic associated with that conversation with the person associated with that conversation. For example, suppose that the AI had a set positive to negative ratio of 1:5. And suppose the AI had a conversation with someone where there were 15 or more negatively weighted key phrases and only 3 positively weighted key phrases. The AI would see that it was definitely a negative conversation so it would flag that whole conversation as having a negative 'mood' for that topic. Next time the AI tries to start a conversation, and it randomly picks that topic for that person, it sees the mood is negative, so it skips that topic and randomly picks another one, before it goes on to structure it's starting sentence. If the topic is rated as positive, then it would use the keywords from the key phrases in that topic (conversation) to build a starting remark or question.

But what about 'neutral' or 'zero' moods? If an AI sees an equal proportion of positive and negatives, then the topic, or key phrase is said to be 'neutral' and the key phrase and/or topic is given a value of zero. Before any topic or key phrase is evaluated before storing in the database, it is automatically given a value of zero. So basically, in this instance the AI would not change the weight. In evaluating a randomly chosen topic for possible use, the AI would only disregard negative topics. It would choose to use a neutral topic, if picked, just as it would choose to use a positive topic, if picked. The AI may also use the weight system to change the mood of the conversation. For example, if the bubble gum conversation with Lisa turned out to have a negative weight ratio, and Lisa happened to start a sentence that seemed to lead the AI to that same topic again, the AI would see that was previously a negative topic and may try to structure a response that it could analyze as having a positive weight. If the current conversation seems to be getting close to the negative ratio again, despite the positively weighted replies by the AI, The AI may draw on some known subject changing techniques such as just randomly choosing a positively weighted topic that follows the same keywords and forming a new remark from it, saying "speaking of..." and using a key word from the current conversation, or starting something entirely new. Or, randomly even just come out and suggest that it might be best to change the subject. It may randomly do so after that or wait for the other person to do so. If they do not after a specified amount of silence, then the AI might randomly do so.

If someone wishes to continue a negative conversation, the AI might say it does not wish to speak to the person further and may opt to stop talking to the person altogether. In that case, the AI would then flag that person as having a negative weight, and thus either not speak to them in the future or not have a very pleasant tone when conversing with that particular person. Likewise, if the person has a positive weight the AI's tone with that person might be in the range of happiness to excitement. You could also adjust the tone of the AI based on these weighted ratios, with enough fuzzy logic programming skill.

Use of Dictionaries and Thesauruses

An AI's 'best friend' is an integrated dictionary and thesaurus. The AI can draw out key words and phrases from a dictionary to use in constructing a sentence or determining keywords or topic names. A Thesaurus is also good for coming up with words to match a potential sentence's weight or mood. If for example, the AI is conversing about good weather as someone say "The weather is terrible today", but it knows by previous conversations with that person that the topic of 'weather' also had a negative mood, the AI may try to change the mood of the sentence. In trying to do so, it might use the Thesaurus to find antonyms that would give an opposite word to the weight. For example, the adjective 'terrible' is what caused a negative weight. So an antonym for 'terrible' would be maybe 'wonderful'. So the AI would try to structure a sentence using weather and wonderful, and the noun 'today' would cause a keyword search and a returned result of say, 'tomorrow'. So the AI has 'wonderful', 'weather', 'tomorrow' and thus could respond with "But tomorrow's weather just might turn out to be wonderful."

Building Sentences from Key Phrases and Keywords

One of the key functions for an AI besides database query and sentence analysis is sentence structuring. Earlier I've discussed and gave examples of how an AI could build up a sentence from keywords found in the database, dictionary or thesaurus. Now I'll try to go into this a bit more in detail.

As discussed previously, the AI could find key words to build a sentence from, such as 'weather', 'wonderful' and 'tomorrow'. But it still needs to find the other words to make up a sentence and know how to put them in the right order to make sense. This is where what I call 'phrasing' would come in.

The AI could also have a database of basic phrases and words, such as sentence starters and fillers. Starters could be words such as 'But', 'However', 'Nonetheless', 'Maybe', etc. or phrases such as 'I think that', 'I hope that', 'I know that', 'Wouldn't it be nice if', etc. So if given the afore mentioned words, it could decide to start a sentence by randomly deciding to use a word or phrase. Let's say the AI decided on a phrase. It would randomly pick one, let's say 'Wouldn't it be nice if'. So it has a start.

It then randomly picks a noun from the words it intended to use. If it choose 'weather' it would also look up to see if it fits in categories other than a person, place or thing, such as a description of time or a measurement of some kind. If it doesn't (as it doesn't here) then it would tag on a pronoun such as 'the' since it didn't see that it was talking about a person in the sentence it got the words from. So now it has 'Wouldn't it be nice if the weather'.

Now it looks for a filler. Again it can use either an adverb such as 'was', 'were', 'is' or 'are' and analyzing the tense of the sentence. Since weather is not plural it would choose 'is'. So now it has 'Wouldn't it be nice if the weather is'.

Next the AI will want an adjective to describe the weather. But it wants to choose a positive adjective because it saw the sentence had a negative weight, so it has to make it a positive weight. The word 'wonderful' was picked earlier to fulfill this plan. So the AI now can speak out it's reply: "Wouldn't it be nice if the weather is wonderful?"

Keep in mind that the phrases it picks would also have a flag to let the AI know if starting a sentence with a particular phrase (like 'Wouldn't it be nice if') is a question, and thus insert the proper punctuation at the end of the sentence. In most cases except the extreme, the question mark would override other punctuation in question sentences, unless the mood is one of extreme excitement, in which the AI would then insert the question mark first, then the exclamation mark. This would be the only exception. If the AI outputs it's replies by voice, then the voice engine would have to compensate and deliver the sentence in a questioning rise and falls, yet in an excited tone.

Conclusion

This is just some of my theory of how a fuzzy logic AI system could be designed. It can get quite complicated especially when you have to take into consideration all kinds of situations in the language the AI is being built to use. This article has assumed the AI was mostly a text/text interface using the English language. Much more could be added and improved, including giving the AI a voice and accepting voice input. That however is beyond the scope of this article and may someday be the topic of another article in the future.

Using the ideas in this article, however, one could conceivably build an AI that could possibly carry on some very intelligent conversations, as well as learn and recognize people, their habits and how they feel about different topics based on a weight system. This could work quite well for robotic and computer applications, and ultimately, could be a good option for the AI programming of a robot car computer.



Bookmark and Share