conversations with my computer - probert

Upload: anonymous-kdqf49qb

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Conversations With My Computer - Probert

    1/230

  • 7/27/2019 Conversations With My Computer - Probert

    2/230

    CONTENTS

    SLOT AND FRAME PARSINGSCANNING

    SYNTACTICAL STRUCTURESHUMAN PARSINGCOMMON PHRASESCOMPONENT PARSING

    QUALIFIERS AND AMBIGUITYUNDERSTANDING AND RESPONDING

    GEORGEINTELLIGENCE THROUGH LEARNING

    TOOLS

    APPENDIX NOTES FOR PROGRAMMERS

  • 7/27/2019 Conversations With My Computer - Probert

    3/230

    INTRODUCTION

    While some were introduced to the idea of talking to computers through ArthurC. Clarke's book "2001", I was weaned on Star Trek. I make no apology fornever having finished reading 2001. Nor for never sitting all the way throughthe film that followed, "2001, A Space Odyssey". The fact is I find them bothrather tedious! However, an interesting point comes out. Both readers of 2001and audiences attending the film made few criticisms of HAL the talkingcomputer. It would appear that most people take the idea of understandingtheir natural language for granted. In her book "Artificial Intelligence andNatural Man" Margaret Boden is some what scathing of people's naivety inaccepting the idea that a computer can understand human language, even withits subtle variations. But in fairness to the public, Margaret, how manypeople in 1977 had seen a computer? Let alone learnt how to program one. Thereason people didn't wonder at HAL's quite extraordinary powers ofcomprehension was that they did not see anything strange in them. "If I canunderstand English, and a computer is an electronic brain, then of course it

    can understand." Might go the reasoning.Later audiences were treated to the idea of confusing computers. This idea wasused in Star Trek to defeat several unpleasant computers whose logic had gotthe better of their human companions, and in Doctor Who where a certaincomputer (in "the Green Death") was particularly unpleasant. This lattercomputer was destroyed following being asked a paradoxical question. Thepublic were awakening to the idea that computer's may be bright, but they'renot brilliant!

    Enough of science fiction, what of the reality? The matter is that people arefascinated by the idea of "thinking machines". And what better sort ofthinking machine than one that can converse in your own language? During the

    1960s a lot of work was undertaken by the psychology and psychiatricprofessions in modelling neurosis with computer simulations.

    In 1962 K.M. Colby, a psychoanalyst, attempted to model free association inpsychotherapy with a computer system he called a "simulation of a neuroticprocess". The computer modelled a woman who believes that her father hasabandoned her, but cannot consciously accept that she hates him. The computeroperator plays the role of the psychotherapist with this system. In his book"Experimental Treatment of Neurotic Computer Programs", K.M. Colby details thefollowing interaction between the computer and the therapist;

    computer: FATHER IS INDIFFERENT TO ME.MOTHER SAYS FATHER ABANDONED US.

    FATHER PREFERRED SISTER.

  • 7/27/2019 Conversations With My Computer - Probert

    4/230

    I AVOID FATHER.SISTER ADMIRED FATHER.I FEEL TENSE NOW.

    therapist: YOU ADMIRE FATHER.

    computer: NO.I DO NOT ADMIRE FATHER.I ADMIRE AL.MOTHER OUGHT NOT TO ADMIRE AL.MOTHER DETESTS FATHER.

    Colby's hope in developing this computer model was that it would be of use instudying the ways in which belief systems and defensive habits of thought canbe modified by particular conceptual interventions by, say, a therapist. Iwonder?

    Another medical type artificial intelligence computer system was called ELIZA.ELIZA was developed by Joseph Weizenbaum to model a psychiatrist. The computeroperator may then take the role of a patient and talk with the program. In his

    book "ELIZA", Weizenbaum details the following interaction between ELIZA and ahuman operator;

    operator: MEN ARE ALL ALIKE

    eliza: IN WHAT WAY

    operator THEY'RE ALWAYS BUGGING US ABOUT SOMETHING OR OTHER.

    eliza: CAN YOU THINK OF A SPECIFIC EXAMPLE

    operator: WELL, MY BOYFRIEND MADE ME COME HERE.

    eliza: YOUR BOYFRIEND MADE YOU COME HEREoperator: HE SAYS I'M DEPRESSED MUCH OF THE TIME.

    eliza: I AM SORRY TO HEAR THAT YOU ARE DEPRESSED

    operator: IT'S TRUE. I AM UNHAPPY.

    eliza: DO YOU THINK COMING HERE WILL HELP YOU NOT TO BE UNHAPPY

    operator: I NEED SOME HELP, THAT MUCH SEEMS CERTAIN.

    eliza: WHAT WOULD IT MEAN TO YOU IF YOU GOT SOME HELP

  • 7/27/2019 Conversations With My Computer - Probert

    5/230

    operator: PERHAPS I COULD LEARN TO GET ALONG WITH MY MOTHER.

    eliza: TELL ME MORE ABOUT YOUR FAMILY

    operator: MY MOTHER TAKES CARE OF ME.

    eliza: WHO ELSE IN YOUR FAMILY TAKES CARE OF YOU

    operator: MY FATHER.

    eliza: YOUR FATHER

    operator: YOU ARE LIKE MY FATHER IN SOME WAYS.

    eliza: WHAT RESEMBLANCE DO YOU SEE

    operator: YOU ARE NOT VERY AGGRESSIVE BUT I THINK YOU DON'T WANT ME TO NOTICETHAT.

    eliza: WHAT MAKES YOU THINK I AM NOT VERY AGGRESSIVE

    operator: YOU DON'T ARGUE WITH ME.

    eliza: WHY DO YOU THINK I DON'T ARGUE WITH YOU

    operator: YOU ARE AFRAID OF ME.

    eliza: DOES IT PLEASE YOU TO THINK I AM AFRAID OF YOU

    operator: MY FATHER IS AFRAID OF EVERYBODY.

    eliza: WHAT ELSE COMES TO MIND WHEN YOU THINK OF YOUR FATHER

    operator: BULLIES.eliza: DOES THAT HAVE ANYTHING TO DO WITH THE FACT THAT YOUR BOYFRIEND

    MADE YOU COME HERE

    Exponents of ELIZA point out that it can talk about any subject. Critics maynotice that it does not always talk very intelligently! But ELIZA did trick asalesman at the computer company. Not a very difficult task perhaps, but hereanyway is a transcript of the conversation that is reported to have takenplace by teletype.

    man: DO YOU THINK THAT I CAN USE THE COMPUTER THIS MORNING?

  • 7/27/2019 Conversations With My Computer - Probert

    6/230

    teletype: WHY DO YOU ASK?

    man: I MIGHT BE ABLE TO MAKE SOME ADDITIONAL TELCOMP SALES.

    teletype: WHY AREN'T YOU SURE?

    man: MY PROSPECTIVE CUSTOMERS HAVE NEVER SEEN A DEMONSTRATION OF THESYSTEM.

    teletype: DOES THIS MEAN VERY MUCH TO YOU?

    man: OF COURSE IT DOES.

    teletype: YOU SEEM VERY POSITIVE ABOUT THAT.

    man: THIS IS RIDICULOUS.

    teletype: WHAT DO YOU MEAN THIS IS RIDICULOUS?

    man: IS THERE ANY REASON WHY I CAN'T USE THE COMPUTER NOW?

    teletype: TELL ME MORE ABOUT YOUR PROSPECTIVE CUSTOMERS.

    man: PLEASE DIAL ME UP ON 491-1850.

    From the anger apparent in the salesman's responses, he may not have realisedthat he was talking to a computer. But he certainly knew that who ever he wastalking to was fairly obtuse.

  • 7/27/2019 Conversations With My Computer - Probert

    7/230

    SLOT AND FRAME PARSING

    The simplest way in which a computer program can respond to supplied naturallanguage is with a slot-and-frame parser. At its most primitive level theslot-and-frame approach simply compares the supplied text with known phrases.This approach was used in early adventure game programs. Anyone who played anyof the first adventure games will be familiar with typing in phrases like; "GOEAST" and "TAKE LAMP".

    GET INPUT PHRASE

    \/INITIALISE COUNTER

    \/> COMPARE INPUT PHRASE WITH LIBRARY PHRASE \/ NOYES /\ \/ \/ INCREMENT COUNTER PRINT RESPONSE

    \/ NOHAS COUNTER \/ REACHED LIMIT?

    \/ PRINT YES>I DON'T >STOP UNDERSTAND

    Flow diagram of a slot-and-frame parser

  • 7/27/2019 Conversations With My Computer - Probert

    8/230

    A slot-and-frame parser is simply implemented with two tables: one of knownphrases, a second with corresponding responses.

    Phrase ResponseHELLO HELLO HOW ARE YOU? VERY WELL THANK YOU HOW'S THE WIFE? STILL BREATHING DO YOU WANT A BEER? IS THE POPE A CATHOLIC?DO YOU TAKE SUGAR? NO THANKS

    An example slot-and-frame phrase and response table

    The following program illustrates the idea of a slot-and-frame parser. Thenumber of known phrases is very small, but may be expanded to the extent ofthe available computer's memory.

    ---------Listing 1---------

    /* Primitive Slot & Frame parser demonstration */

    char *phrase[] = { "HELLO","WHAT IS YOUR NAME","HOW OLD ARE YOU",

    "DO YOU LIKE CRICKET","HAVE YOU EVER BEEN TO SPAIN","DO YOU LIKE CHIPS","" };

    char *response[] = { "Hello","Billy","Too Old!","Yes I like cricket","No I have never been to Spain","Yes, I like chips","" };

  • 7/27/2019 Conversations With My Computer - Probert

    9/230

    void RESPOND(char *text){

    int n;

    for(n = 0; *phrase[n]; n++){

    /* Case insensitive comparison */if (stricmp(text,phrase[n]) == 0){

    /* A precise match is found */printf("%s",response[n]);return;

    }}printf("I Don't understand");

    }

    main(){

    char text[250];

    for(;;){

    printf("\n?");gets(text);if (*text){

    RESPOND(text);}else

    break;}

    }

    The first problem encountered with the primitive slot-and-frame approach isthat even the most irrelevant difference between the entered phrase, and theknown phrase will not be understood. If the phrase "What is your name" isentered the demonstration program, a response of "Biily" will be printed.However, the inclusion of the name Billy to a question is enough to causeconfusion. "How old are you Billy" is a seemingly valid question, but theprogram cannot find a matching known phrase and so the response is "I don'tunderstand".

    The next generation of slot-and-frame parsers scanned the input phrase forknown words or phrases, and responded accordingly. The early slot-and-frame

    parsers were written in early dialects of BASIC. BASIC still does not have

  • 7/27/2019 Conversations With My Computer - Probert

    10/230

    string handling facilities that are as powerful as those provided by C. Thedifferences between this example second generation slot-and-frame parser andthe primitive one are twofold. First, the input string is converted to uppercase. Second, the C strstr() function is used to locate a known phrase withinthe input phrase. We have to convert the input phrase to the same case as theknown phrases because strstr() is case sensitive.

    ---------Listing 2---------

    /* Second Generation Slot & Frame parser demonstration */

    char *phrase[] = { "HELLO","WHAT IS YOUR NAME","HOW OLD ARE YOU","DO YOU LIKE CRICKET","HAVE YOU EVER BEEN TO SPAIN","DO YOU LIKE CHIPS",

    "" };char *response[] = { "Hello",

    "Billy","Too Old!","Yes I like cricket","No I have never been to Spain","Yes, I like chips","" };

    void RESPOND(char *text){

    int n;

    for(n = 0; *phrase[n]; n++){

    /* Search for the known phrase occurring within the text string */if (strstr(text,phrase[n])){

    /* The known phrase was located */printf("%s",response[n]);return;

    }}/* No known phrases were located within the text string */printf("I Don't understand");

    }

  • 7/27/2019 Conversations With My Computer - Probert

    11/230

    main(){

    char text[250];

    for(;;){

    printf("\n?");gets(text);if (*text){

    strupr(text);RESPOND(text);

    }else

    break;}

    }

    With such a small development, the flexibility of the parser has grownenormously. Now phrases such as "How old are you?" and "How old are you,

    Billy?" generate a sensible response, where before they didn't.The third generation of slot-and-frame parsers search are sometimes called"verb and noun search parsers". The input phrase is searched for known verbs(action words), and nouns (objects). This type of parser is extensively usedin adventure games where the input phrase will be a noun and a verb, withperhaps a smattering or prepositions. Something along the lines of; "TAKE THEBUCKET", or "OPEN THE DOOR" or "SMASH THE WINDOW WITH THE HAMMER". Let us usethe phrase "SMASH THE WINDOW WITH THE HAMMER" for an example. The program mayrecognise the verb "SMASH", the object "WINDOW" but also require aqualification object "HAMMER". So long as the player enters a phrasecontaining these three words the parser will understand the command. So,"HAMMER WINDOW SMASH" and "SMASH HAMMER WINDOW" and even "SMASH THE HAMMER

    WITH THE WINDOW" will all be accepted. A verb and noun search parser is verylax on semantics, this doesn't matter too much in the adventure game domain.In conversational computing a lack of semantics can lead to erroneousdeductions.

    One other point to remember about all the slot-and-frame computer parsers isthat the computer program never understands what is being said. Rather itsimply makes automated responses to standard phrases.

  • 7/27/2019 Conversations With My Computer - Probert

    12/230

    SCANNING

    Scanning is a process of quick interpretation by ignoring minor words. Whenyou scan a written document, you search very quickly for key facts. Within thecontext of natural language processing, the principle is the same.

    Minor words include most prepositions. A scan of the sentence, "the quickbrown fox jumps over the lazy dog." Will reveal "quick brown fox jumps overlazy dog." The essence of the sentence remains. It is a frequent problem tocomputers that humans expand their speech with superfluous words. An enquireras a train station may wish to know at what time the next train will departfor Manchester. A straight forward enquiry would be. "When does the next trainleave for Manchester?" Is it necessary to specify which train? A scan of thequestion might return. "When train leave for Manchester?" It does not soundvery natural to a human, but it conveys the same request as the initialsentence.

    Writing a computer program to respond to English sentences scanning might be

    used to good effect. The traditional approach, used by SQL and Data Ease, isto insist that the operator does most of the scanning. These programs mayunderstand phrases such as: "For Customers; list records" or "modify recordsin inventory" Where as a human approach may be to command. "Print a list ofall the records in the customers file." Or "modify every record in theinventory file."

    An imaginary application might be a plain English disk operating system whichinstead of accepting commands like "COPY *.C A:" Would understand commandslike "COPY ALL .C FILES TO DRIVE A".

    The argument against computer interfaces that understand plain English hasalways been that the operator has to do too much typing. In the absence of

    reliable and cost-effective audio input devices there is little defenceagainst this argument. Currently commands to a computer must be typed in orselected by pointing to them.

  • 7/27/2019 Conversations With My Computer - Probert

    13/230

    SYNTACTICAL STRUCTURES

    Sentences, if constructed properly, are comprised of two parts: subject andpredicate. For example. The sentence "many hands make light work." Splits into"many hands" and "make light work". The predicative qualifies or describes thesubject. In some sentences either the subject or the predicate may be implied,rather than stated. For example in the sentence "come here!" The subject,"you", is implied. Of course this type of sentence can lead tomisunderstandings!

    Within the subject, one word is the "subject word". In the sentence "manyhands make light work.", the subject word is "hands" because it is a nounfollowing an adjective. Whereas in the sentence, "most of the audience enjoyedthe concert." The subject word is "most", denoted by an adjective phrasefollowing it, "of the audience".

    Within a sentence there is also an object. The object is a noun, perhaps

    represented by a pronoun, upon which the sentence's verb acts. For example. Inthe sentence "the cat chased the mouse." The object is "mouse", because it isthe noun acted upon by the verb, "chased".

    Often a sentence contains phrases and clauses. Clauses are sub-sections of thesentence with their own verb (action word), a phrase does not have a verb. Forexample. "I saw Mrs Brown drive her car along the motorway." This sentencecontains the subject, "I" and the predicative "saw Mrs Brown drive her caralong the motorway." The predicative is further divided into: "saw Mrs Brown","drive her car" and "along the motorway."

    This breaking down of sentences can be taken further. Linguistics declaresthat a phrase can be broken down into a hierarchical structure in which groups

    of words appear as groups. If we analyse the sentence "I believe that the boxfell off the kitchen table." First we can split it into the subject andpredicate;

    Subject PredicateI believe that the box fell off the kitchen table

  • 7/27/2019 Conversations With My Computer - Probert

    14/230

    full syntactical analyse of the sentence is;

    S--------VP--------S-------S-------VP------PP------NP-------N---------N| | | | | | | | |NP V CONJ NP V P DET N N| | | | | | | | |I believe that | fell off the kitchen table

    +--+--+| |DET N| |the box

    Key to diagram

    CONJ ConjunctionDET DeterminerN Noun

    NP Noun PhraseP PrepositionS SentenceVP Verb PhraseV Verb

    A predicate parser that extracts subjects and clauses from a sentence is auseful device for breaking a sentence down into small sections.

    ---------Listing 3

    ---------/* PREDICATE PARSER */

    /*---------------*//* Include files *//*---------------*/#include #include #include #include

  • 7/27/2019 Conversations With My Computer - Probert

    15/230

    /*-----------*//* Constants *//*-----------*/#define num_a 139#define num_q 72#define num_l 20#define num_c 14#define num_f 18#define num_qs 5

    /*-----------*//* Lexicons *//*-----------*//* Verbs and adverbs */static char *actions[num_a] = { "LIKE","DISLIKE", "LOVE" , "HATE","SLEEP",

    "PUMP", "MAKE" , "WEAR" , "RENOUNCE" , "SHORTEN","KEEP", "FEED", "DESCEND" , "CONVERT", "KEEP","OPEN" , "EAT" , "READ" , "DRY" , "KILL" ,"MEND", "CAUSE", "SIT" , "SAT" , "CLOSE","TAKE", "RUN" , "JUMP", "SKIP", "HOP", "BREAK",

    "INVENT", "DISCOVER", "EXPLORE", "PUNCH","KICK","IGNITE", "CHAT", "WRITE", "TALK","LISTEN","WHISTLE","SHOUT","SCREAM","DRINK","BATHE","WASH","CROSS", "ENTER", "CLIMB", "TIE", "CROUCH","CRAWL","BREATHE", "SNIFF", "SPIT" ,"GO","PUT","DRINK","LOOK", "TURN", "BORN","LEAVE", "URINATE","SMOKE", "LIGHT", "DESIGN" , "DRAW" ,"PAINT","BOIL", "STRAIN", "POUR","TELEPHONE","PHONE","CALL" ,"DRESS", "CUT", "SAW", "GONE","STUDY","FOLD" , "PLAY", "PAY", "EARN","MEET", "MET","LIVE","DWELL", "BEEN", "HEAR" , "HEARD","SING" , "SUNG", "DEVELOP", "STIR","SEE","SEEN", "RING", "FEEL" , "FELT", "DIED", "HUNT",

    "ASK", "STAY" ,"KISS", "CUDDLE", "GIVE","BUY","BOUGHT", "LICK" , "TASTE", "ADMIRE" ,"PROVIDE","COME", "BUILD", "BUILT", "REFERENCE" ,"WENT","CONSTRUCT" ,"LEAN","WALK","CARRY","RIDE","ORBIT","DRIVE" , "DROVE" , "DOWN", "ALONG", "SHALL","SHOULD" , "THINK", "THOUGHT","GOT","GET","LEND","LENT","HAVE","HAD","HAS" };

    static char *questions[num_qs] = { "WHAT", "WHEN", "HOW", "WHY", "WHERE" };

  • 7/27/2019 Conversations With My Computer - Probert

    16/230

    /* Prepositions and conjunctions */static char *fillers[num_f] = { "IS", "THE" , "A", "AN" , "ARE" ,"TOO","ALSO",

    "COME" , "FROM" , "ITS" , "THEIR" ,"WITH","THAT" , "ABOUT", "THING" , "THIS" ,"BECAUSE" };

    static char *qualifiers[num_q] = {"FAST", "SLOW", "ROUND", "SQUARE","HAIRY", "BALD", "FRIGHTENING","INTERESTED","SHY", "NICE", "HORRIBLE" , "LOVELY","INTERESTING","PRETTY" , "BEAUTIFUL" , "BLONDE" , "BRUNETTE" ,"SEXY" , "SHINY" , "DULL" , "BORING" , "CAREFUL" ,"HARD" , "SOFT" , "GENTLE" , "QUICK" , "LIGHT","HEAVY", "RADIOACTIVE", "METALLIC","TIRED","GASEOUS", "BRIGHT", "SWEET", "SOUR" , "BITTER","GOOD","BAD","DRUNK","STUPID","MUTUAL","BIG","SMALL", "FAT", "THIN", "TALL" ,"SHORT","LARGE", "PETITE", "TINY" , "BLACK", "BLUE","RED", "YELLOW", "ORANGE","GREEN","BROWN" ,"GINGER" , "PURPLE","MAUVE", "SILVERY", "SILVER","GOLD", "CYAN" ,"LAZY" ,"CARNIVOROUS" ,"PAST", "MY",

    "IN" , "ON", "BESIDE","OLD" };

    static char *locations[num_l] = { "IN" , "ON", "BESIDE" , "EAST" , "WEST" ,"NORTH" ,"SOUTH" , "UNDER" , "UP" ,"BELOW", "ABOVE", "RIGHT", "LEFT","OVER","INTO","AROUND" , "TO", "OFF","THERE" };

    typedef struct {char text[500];char object[80];char description[80];

    char action[80];}SUBJECT;

    SUBJECT subject;SUBJECT predicate;SUBJECT clause1;SUBJECT clause2;SUBJECT clause3;

  • 7/27/2019 Conversations With My Computer - Probert

    17/230

    /*---------------------*//* Function prototypes *//*---------------------*/int IS_COLOUR(char *);int IS_FILLER(char *);int IS_LOCATION(char *);int IS_VERB(char *);void strchg(char *, char *, char *);void truncstr(char *,int );

    /*--------------------*//* Start of functions *//*--------------------*/static void GET_OBJECT(char *text, char *object){

    /* Copy NOUN into object */

    char *p;int n;int ignore;

    char temp[30];char copy[250];char previous[50];

    strcpy(copy,text);strcpy(object,"");strcpy(previous,"");p = strtok(text," ,.;:?!");

    while(p){

    ignore = IS_LOCATION(p);

    if (!ignore){for (n = 0; n < num_q; n++){

    if (stricmp(p,qualifiers[n]) == 0)ignore = 1;

    else{

    strcpy(temp,qualifiers[n]);if (temp[strlen(temp) - 1] == 'E'){

    temp[strlen(temp) - 1] = 0;if (temp[strlen(temp) - 1] == 'L')

    temp[strlen(temp) - 1] = 0;

  • 7/27/2019 Conversations With My Computer - Probert

    18/230

    }strcat(temp,"LY");if (stricmp(p,temp) == 0){

    ignore = 1;break;

    }}

    }}if (!ignore){

    for(n = 0; n < num_qs; n++){

    if (stricmp(p,questions[n]) == 0){

    ignore = 1;break;

    }}

    }if (!ignore)if (IS_VERB(p))

    ignore = 1;

    if (stricmp(p,"OF") == 0){

    if (*object)strcat(object," ");

    strcat(object,"OF");strcat(object,previous);strcpy(previous,"");

    }

    elseif(!ignore){

    if (*object)strcat(object," ");

    strcat(object,p);}

    if (ignore && stricmp(p,"OF") != 0)strcpy(previous,p);

    elsestrcpy(previous,"");

    p = strtok(NULL," ,.;:?!");

  • 7/27/2019 Conversations With My Computer - Probert

    19/230

    }strcpy(text,copy);

    }

    static void GET_DESCRIPTION(char *text, char *object_qualify){

    /* Obtain qualifying statements from phrase string */

    char *p;int n;char copy[250];char temp[50];char previous[50];

    strcpy(copy,text);strcpy(object_qualify,"");strcpy(previous,"");

    p = strtok(text," ,.;:?!");

    while(p){for(n = 0; n < num_q; n++){

    if (stricmp(p,qualifiers[n]) == 0){

    if (*object_qualify)strcat(object_qualify," ");

    strcat(object_qualify,p);break;

    }else{

    strcpy(temp,qualifiers[n]);if (temp[strlen(temp) - 1] == 'E'){

    temp[strlen(temp) - 1] = 0;if (temp[strlen(temp) - 1] == 'L')

    temp[strlen(temp) - 1] = 0;}strcat(temp,"LY");if (stricmp(p,temp) == 0){

    if (*object_qualify)strcat(object_qualify," ");

    strcat(object_qualify,p);

    break;

  • 7/27/2019 Conversations With My Computer - Probert

    20/230

    }}

    }if (stricmp(previous,"THE") == 0 || stricmp(previous,"A") == 0 ||

    stricmp(previous,"AN") == 0 || stricmp(previous,"OF") == 0){

    if (IS_VERB(p)){

    if (*object_qualify)strcat(object_qualify," ");

    strcat(object_qualify,p);}

    }strcpy(previous,p);p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

    static void GET_ACTION(char *text , char *action)

    { /* Obtain action words from the phrase string */

    char *p;char pre[50];char copy[250];

    strcpy(copy,text);strcpy(action,"");strcpy(pre,"");

    p = strtok(text," ,.;:?!");

    while(p){if (IS_VERB(p)){

    if (stricmp(pre,"THE")){

    if (*action)strcat(action," ");

    strcat(action,p);}

    }strcpy(pre,p);p = strtok(NULL," ,.;:?!");

    }

  • 7/27/2019 Conversations With My Computer - Probert

    21/230

    strcpy(text,copy);}

    static int IS_FILLER(char *p){

    /* Return 1 if the word is a filler word */

    char n;

    for(n = 0; n < num_f; n++){

    if (stricmp(p,fillers[n]) == 0)return(1);

    }

    return(0);}

    static int IS_LOCATION(char *p)

    { /* Return 1 if the word is a location description */

    char n;

    for(n = 0; n < num_l; n++){

    if (stricmp(p,locations[n]) == 0)return(1);

    }return(0);

    }

    static int IS_DESCRIPTION(char *p){int n;

    for(n = 0; n < num_q; n++){

    if (stricmp(p,qualifiers[n]) == 0)return 1;

    }return 0;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    22/230

    void GET_PREDICATE(char *data,char *predicate,char *subject){

    /* Splits a sentence into its two parts */

    char *p;char *q;char word[40];char save;char previous[40];

    strcpy(predicate,data);strcpy(subject,"");

    p = data;q = p;do{

    p = strpbrk(p," .,;:?!");if (p){

    save = *p;*p = 0;strcpy(word,q);*p = save;p++;if (stricmp(word,"IT") == 0){

    save = *p;*p = 0;strcpy(predicate,data);*p = save;strcpy(subject,p);return;

    }if (stricmp(word,"MOST") == 0){

    save = *p;*p = 0;strcpy(predicate,data);*p = save;strcpy(subject,p);return;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    23/230

    if (stricmp(word,"IS") == 0 || stricmp(word,"CAN") == 0 ||stricmp(word,"WILL") == 0 || stricmp(word,"AM") == 0 ||stricmp(word,"TO") == 0 || stricmp(word,"FOR") == 0 ||stricmp(word,"MAY") == 0 || stricmp(word,"THAT") == 0 ||stricmp(word,"WAS") == 0 || stricmp(word,"DID") == 0 ||stricmp(word,"DIDN'T") == 0 || stricmp(word,"IN") == 0 ||stricmp(word,"WITH") == 0 || stricmp(word,"SHALL") == 0 ||stricmp(word,"SHOULD") == 0){

    if (q != data){

    save = *q;*q = 0;strcpy(predicate,data);*q = save;strcpy(subject,q);return;

    }}

    if (stricmp(previous,"THE") && stricmp(previous,"A") &&stricmp(previous,"AN") && stricmp(previous,"OF") &&stricmp(previous,"SHALL") && stricmp(word,"SHOULD") &&stricmp(previous,"WILL"))

    {if (q != data){

    if(!IS_FILLER(word) && !IS_LOCATION(word) &&!IS_DESCRIPTION(word) && !IS_VERB(word) &&!IS_FILLER(previous) && !IS_LOCATION(previous) &&!IS_DESCRIPTION(previous) && !IS_VERB(previous))

    {save = *q;

    *q = 0;strcpy(predicate,data);*q = save;strcpy(subject,q);return;

    }

    if(IS_VERB(word)){

    /* Ignore verbs starting a clause/phrase *//* if the previous word is a noun *//* and we are scanning the predicate *//* return from the previous word */

    save = *q;

  • 7/27/2019 Conversations With My Computer - Probert

    24/230

    *q = 0;strcpy(predicate,data);*q = save;strcpy(subject,q);return;

    }}

    }}save = *p;*p = 0;strcpy(previous,q);q = &previous[strlen(previous)-1];while(strchr(" .,;:!?",*q))

    *q-- = 0;

    *p = save;q = p;

    }while(p);

    }int IS_VERB(char *word){

    int n;char verb[40];

    for(n = 0; n < num_a; n++){

    strcpy(verb,actions[n]);if (stricmp(word,verb) == 0){

    return 1;

    }/* Past tense */if (verb[strlen(verb) - 1] == 'E')

    strcat(verb,"D");elseif (verb[strlen(verb) - 1] == 'Y'){

    /* Irregular words! */if (stricmp(verb,"PAY") == 0)

    strcpy(verb,"PAID");elseif (stricmp(verb,"PLAY") == 0)

    strcpy(verb,"PLAYED");

    else

  • 7/27/2019 Conversations With My Computer - Probert

    25/230

    {verb[strlen(verb) - 1] = 0;strcat(verb,"IED");

    }}else

    strcat(verb,"ED");if (stricmp(word,verb) == 0){

    return 1;}/* Plurals */strcpy(verb,actions[n]);strcat(verb,"S");if (stricmp(word,verb) == 0){

    return 1;}/* ING */strcpy(verb,actions[n]);

    if (verb[strlen(verb) - 1] == 'E')truncstr(verb,1);strcat(verb,"ING");if (stricmp(word,verb) == 0){

    return 1;}

    }return 0;

    }

    void truncstr(char *p,int num){

    /* Truncate string by losing last num characters */if (num < strlen(p))p[strlen(p) - num] = 0;

    }

    void main(){

    /* This is a test call */char *p;FILE *fp;

    char text[250];

    fp = fopen("response.out","w+");

  • 7/27/2019 Conversations With My Computer - Probert

    26/230

    do{

    printf("\nEnter a phrase\n");gets(text);if (*text != 0){

    strupr(text);GET_PREDICATE(text,subject.text,predicate.text);GET_ACTION(subject.text,subject.action);fprintf(fp,"\nSUBJECT [%s] PREDICATE [%s]",subject.text,

    predicate.text);GET_OBJECT(subject.text,subject.object);fprintf(fp,"\nOBJECT[%s]",subject.object);fprintf(fp,"\nACTION [%s]",subject.action);GET_DESCRIPTION(subject.text,subject.description);fprintf(fp,"\nDESCRIPTION [%s]",subject.description);

    fprintf(fp,"\nPREDICATE ANALYSIS");GET_PREDICATE(predicate.text,text,clause1.text);if (*clause1.text)

    { p = strstr(predicate.text,clause1.text);*p = 0;

    }GET_ACTION(predicate.text,predicate.action);fprintf(fp,"\nACTION [%s]",predicate.action);GET_OBJECT(predicate.text,predicate.object);fprintf(fp,"\nOBJECT [%s]",predicate.object);GET_DESCRIPTION(predicate.text,predicate.description);fprintf(fp,"\nDESCRIPTION [%s]",predicate.description);

    GET_PREDICATE(clause1.text,text,clause2.text);if (*clause2.text)

    { p = strstr(clause1.text,clause2.text);*p = 0;

    }fprintf(fp,"\nFIRST CLAUSE ANALYSIS");fprintf(fp,"\nCLAUSE [%s]",clause1.text);GET_ACTION(clause1.text,clause1.action);fprintf(fp,"\nACTION [%s]",clause1.action);GET_OBJECT(clause1.text,clause1.object);fprintf(fp,"\nOBJECT [%s]",clause1.object);GET_DESCRIPTION(clause1.text,clause1.description);fprintf(fp,"\nDESCRIPTION [%s]",clause1.description);

    GET_PREDICATE(clause2.text,text,clause3.text);

  • 7/27/2019 Conversations With My Computer - Probert

    27/230

    if (*clause3.text){

    p = strstr(clause2.text,clause3.text);*p = 0;

    }

    fprintf(fp,"\nSECOND CLAUSE ANALYSIS");fprintf(fp,"\nCLAUSE [%s]",clause2.text);GET_ACTION(clause2.text,clause2.action);fprintf(fp,"\nACTION [%s]",clause2.action);GET_OBJECT(clause2.text,clause2.object);fprintf(fp,"\nOBJECT [%s]",clause2.object);GET_DESCRIPTION(clause2.text,clause2.description);fprintf(fp,"\nDESCRIPTION [%s]",clause2.description);

    fprintf(fp,"\nTHIRD CLAUSE ANALYSIS");fprintf(fp,"\nCLAUSE [%s]",clause3.text);GET_ACTION(clause3.text,clause3.action);fprintf(fp,"\nACTION [%s]",clause3.action);GET_OBJECT(clause3.text,clause3.object);

    fprintf(fp,"\nOBJECT [%s]",clause3.object);GET_DESCRIPTION(clause3.text,clause3.description);fprintf(fp,"\nDESCRIPTION [%s]",clause3.description);

    }}while(*text);

    }

    A type of parser which makes use of syntactical structures is the "augmentedtransition network", abbreviated to "ATN". ATN was developed by Thorne,Bratley and Dewar in 1968, and built upon by W.A. Woods. He published his workunder the title "Transition network grammars for natural language analysis" in

    the communications of the association for computing machinery journal, volume13 in 1970. The general idea behind ATN is that in the English language, verbphrases generally follow noun phrases. Thus an ATN seeks a noun phrase, andthen seeks a verb phrase to associate with the noun phrase. This idea of usinggrammatical rules to parse sentences is illustrated in the following exampleof a "depth-first exhaustive search" syntactical parser.

  • 7/27/2019 Conversations With My Computer - Probert

    28/230

    ---------Listing 4---------

    /*---------------*//* Include Files *//*---------------*/#include #include #include

    The following data comprises the rules of grammar, and the lexicon for theparser. Other rules and words can be added as required.

    char *data[] = { "= ","= ","= ","= ",

    "=","= ","=","= ","= ","= ","=","=A","AN","THE","THIS","THAT","=GREEN","RED","BLUE","YELLOW","CYAN","STRONG","BUILT","=JACK","JOHN","ROSE","GRASS","SUN","HOUSE","TREE",

    "=GREW","SHONE","MADE","=TO","OF","IN","=AND","OR","IS","" };

    /*----------------*//* Work variables *//*----------------*/

    char as[100];char h[250];char text[250];

    char gs[50][100];

  • 7/27/2019 Conversations With My Computer - Probert

    29/230

    char ws[50][100];int ra[50];int la[50];int lb[50];

    char temp[100];

    int s;int c;int q;int r;int f;int ls;int c1;int c2;int c3;int c4;int counter;char *p;int i;

    int j;int k;

    void main(){

    s = 1;c = 0;q = 1;

    /* Clear the display */clrscr();

    /* Receive an input phrase from the operator */printf("\nInput a sentence ");gets(text);

    /* Convert the input phrase to upper case */strupr(text);

    /* Insert a space at the beginning of the input string */memmove(&text[1],text,strlen(text) + 1);text[0] = 32;

    /* Append a space to the end of the input string */strcat(text," ");

  • 7/27/2019 Conversations With My Computer - Probert

    30/230

    /* Preserve a copy of the input string */strcpy(H,text);

    /* Load constant data */counter = 0;r = 0;do{

    f = 0;strcpy(as,data[counter]);if (*as){

    while(as[strlen(as) - 1] == 32)as[strlen(as) - 1] = 0;

    for(i = 0; i < strlen(as); i++){

    if(as[i] == '='){

    strncpy(gs[r],as,i);

    gs[r][i + 1] = 0;f = 1;strcpy(ws[r],&as[i+1]);i = strlen(as);

    }}if (f == 0){

    strcpy(gs[r], gs[r - 1]);strcpy(ws[r],as);

    }}r = r + 1;

    counter++;}while(*as);

    /*----------------*//* The parse loop *//*----------------*/

    /* Clear the display */clrscr();

    printf("Syntactical parsing analysis of %s",text);printf("\n\n");

    do

  • 7/27/2019 Conversations With My Computer - Probert

    31/230

    {f = 0;s = 0;ls = strlen(text);c1 = 0;q = 0;

    for(i = q; i < ls; i++){

    if (text[i] == ' '){

    i = i + 1;c1 = c1 + 1;for(j = s; j

  • 7/27/2019 Conversations With My Computer - Probert

    32/230

    c4++;if (c3 > c1 && c4

  • 7/27/2019 Conversations With My Computer - Probert

    33/230

    4 THAT JACK BUILT(THIS) (IS) (THE) (HOUSE) THAT JACK BUILT

    5 THAT JACK BUILT(THIS) (IS) (THE) ((HOUSE)) THAT JACK BUILT

    6 THAT JACK BUILT(THIS) (IS) ((THE)((HOUSE))) THAT JACK BUILT

    7 JACK BUILT(THIS) (IS) ((THE)((HOUSE))) (THAT) JACK BUILT

    8 BUILT(THIS) (IS) ((THE)((HOUSE))) (THAT) (JACK) BUILT

    9 BUILT

    (THIS) (IS) ((THE)((HOUSE))) (THAT) ((JACK)) BUILT

    10 BUILT

    (THIS) (IS) ((THE)((HOUSE))) ((THAT)((JACK))) BUILT11 (THIS) (IS) ((THE)((HOUSE))) ((THAT)((JACK))) (BUILT)

    Parsing complete

    Syntactical parsing analysis of THE SUN SHONE AND THE GRASS GREW

    1 SUN SHONE AND THE GRASS GREW

    (THE) SUN SHONE AND THE GRASS GREW2 SHONE AND THE GRASS GREW(THE) (SUN) SHONE AND THE GRASS GREW

    3 SHONE AND THE GRASS GREW(THE) ((SUN)) SHONE AND THE GRASS GREW

    4 SHONE AND THE GRASS GREW((THE)((SUN))) SHONE AND THE GRASS GREW

    5 AND THE GRASS GREW((THE)((SUN))) (SHONE) AND THE GRASS GREW

  • 7/27/2019 Conversations With My Computer - Probert

    34/230

    6 AND THE GRASS GREW((THE)((SUN))) ((SHONE)) AND THE GRASS GREW

    7 AND THE GRASS GREW((THE)((SUN))) (((SHONE))) AND THE GRASS GREW

    8 AND THE GRASS GREW(((THE)((SUN)))(((SHONE)))) AND THE GRASS GREW

    9 THE GRASS GREW(((THE)((SUN)))(((SHONE)))) (AND) THE GRASS GREW

    10 GRASS GREW(((THE)((SUN)))(((SHONE)))) (AND) (THE) GRASS GREW

    11 GREW(((THE)((SUN)))(((SHONE)))) (AND) (THE) (GRASS) GREW

    12 GREW(((THE)((SUN)))(((SHONE)))) (AND) (THE) ((GRASS)) GREW

    13 GREW(((THE)((SUN)))(((SHONE)))) (AND) ((THE)((GRASS))) GREW

    14 (((THE)((SUN)))(((SHONE)))) (AND) ((THE)((GRASS))) (GREW)

    15 (((THE)((SUN)))(((SHONE)))) (AND) ((THE)((GRASS))) ((GREW))

    16 (((THE)((SUN)))(((SHONE)))) (AND) ((THE)((GRASS))) (((GREW)))

    17 (((THE)((SUN)))(((SHONE)))) (AND) (((THE)((GRASS)))(((GREW))))

    18 ((((THE)((SUN)))(((SHONE))))(AND)(((THE)((GRASS)))(((GREW)))))

    Parsing complete

  • 7/27/2019 Conversations With My Computer - Probert

    35/230

    Syntactical parsing involves complex and very impressive algorithms. Toocomplex for the average human mind? I think so. I think that in order tounderstand natural language we must understand the human mind. In the nextchapter I shall explore natural language parsing from the much simplerpsychology angle. Too simple? Perhaps, but remember that a child canunderstand simple phrases. It takes a rare breed to comprehend linguistics!

  • 7/27/2019 Conversations With My Computer - Probert

    36/230

    HUMAN PARSING

    To be able to program a computer to understand natural language, one mustfirst understand how the human mind handles data received in natural language.This book is not concerned with how the data reaches the thought machinery, bethat a computer program or a brain. Rather this book is concerned with whathappens to the data once it has got there.

    Human's make use of a range of parsers when trying to understand a phrase.First a slot-and-frame parser is used to compare the phrase against a smalllibrary of "common phrases". These are generally exclamations, includinggreetings (HELLO, HI, NICE TO MEET YOU) and dismissals (PUSH OFF, GO AWAY) butalso including warnings (LOOK OUT!). Some questions may also be stored, (HOWARE YOU? WHAT IS YOUR NAME? WOULD YOU LIKE A BEER?) as well as instructions(SHUT THE DOOR, DON'T DO THAT!) and so on. Phrases which fail the slot-and-frame parser are then passed through a more intricate combination of predicateparsers and component analysis. Finally, a verb and noun search may be carriedout. Missing components will be filled in by the mind based upon experience

    both of the question and phrases previously received from the transmitter. Inthis way an individuals' poor grammar is more easily understood after talkingwith them for some time. For example, the rural Jamaicans use a peculiarlanguage called "patoi". A favourite phrase of which is "soon come". Thistranslates, into the Queen's English, as "I shall soon come to join you."

    IS THE DATA A COMMON PHRASE?

    YESNO

    RETRIEVE STANDARD PARSE PHRASERESPONSE FROM LIBRARY :

    :::

    ............................:

  • 7/27/2019 Conversations With My Computer - Probert

    37/230

    ::

    PARSE PHRASE

    EXPAND APOSTROPHES

    EXTRACT COMPONENTS

    IS IT A QUESTION?

    YESNO

    DECIDE TYPE IS IT AN OF QUESTION INSTRUCTION?

    YESNO

    DECIDE TYPE IT'S A OF INSTRUCTION STATEMENT

    Questions may be "interrogative", such as "are you married?". Or they may berequests. "Will you marry me?".

    Instructions encompass commands, requests, entreaties and wishes. An exampleof a command is "shut the door." A wish may be "I want to go to the pub!".

    Statements include exclamations. A statement might be "I went to the pub."Whereas an exclamation may be "I enjoyed that!"

  • 7/27/2019 Conversations With My Computer - Probert

    38/230

    COMMON PHRASES

    These are phrases to which a reaction has become almost instinctive. They varyfrom one individual to the next, but may include the following: Exclamationssuch as: "Hello" and other informal greetings; "Hi", "Wotchya", etc. Commandssuch as "Go away" (and its vulgar counterparts), "Don't do that!" Questionssuch as: "How are you?" , "Who are you?" , "What's your name?" , "Fancy abeer?" and "Have you got a light?"

    Common phrases reduce much ambiguity in communication. For example, without aknowledge of the common phrase "Have you got a light?" Implying a request fora source of ignition, probably for a cigarette, it would be parsed. In parsingthis phrase its whole meaning will change. Probably to an interrogation ofwhether "you" have a source of light energy.

    The way in which we, as humans, react to "common phrases" is still debated bypsychologists. I believe that slot-and-frame parsing is the answer. It may notbe, but it certainly provides a means to model the response to common phrases

    within a computer program.

  • 7/27/2019 Conversations With My Computer - Probert

    39/230

    COMPONENT PARSING

    When a human receives a phrase, be it a sentence or paragraph. The brainbreaks it down into manageable sized phrases and clauses. Much like thepredicate parser. These phrases and clauses are then further broken down intocomponent parts. These components may be summarised as;

    Question/Order (The nature of the phrase)

    Object (What the phrase refers to)

    Subject (What the phrase says about the object)

    Qualifiers ))

    Size ) Descriptions qualifying the object and subject)

    Colour )Number (All, one, two etc)

    Location (Where is the object)

    Action (Verbs)

    Person (You, me, us, them, they, him, her)

    Tense (Past, present, future or indeterminable)

    For example;"What is the matter with Mary Jane?" Breaks down into phrases and clausesthus;

    Subject [What]Predicate [is the matter with Mary Jane?]Object [is the matter]

    The predicate further breaks down into the phrase;

    Phrase [with Mary Jane?]

    Taking the phrase as a whole it breaks down into these components;

  • 7/27/2019 Conversations With My Computer - Probert

    40/230

    The question is WHAT ISThe order isThe object is MATTERThe qualifiers areThe colours areThe sizes areThe subject is WITH MARY JANEThe person isThe number isThe action isThe location isThe tense is present

    Similarly, "It's lovely rice pudding for dinner again!" Produces;

    Subject [It]Predicate [is lovely rice pudding for dinner again!]

    The predicate breaks down into;

    Object [is rice pudding]Description [lovely]

    And produces a sub-phrase;

    Phrase [for dinner again!]

    Analysing the phrase as a whole we get;

    The question isThe order isThe object isThe qualifiers are LOVELY

    The colours areThe sizes areThe subject is LOVELY RICE PUDDING FOR DINNER AGAINThe person isThe number isThe action isThe location isThe tense is indeterminable

    "This is the house that Jack built" produces the following analysis. Compareit with the output of the syntactical parser described in a previous chapter.

    Subject [This]

    Predicate [is the house that Jack built]

  • 7/27/2019 Conversations With My Computer - Probert

    41/230

    The predicate breaks down into an object and a sub-phrase;

    Object [is the house]Phrase [that jack built]

    The sub-phrase breaks down into an object and an action (verb);

    Action [built]Object [that Jack]

    Analysing the phrase as a whole we get the following summary;

    The question isThe order isThe object is HOUSEThe object qualifiers areThe object colours areThe object sizes areThe subject is JACKThe person is

    The number isThe action is BUILTThe location isThe tense is indeterminableEnter a phrase

    A component parser is a fairly complex affair. But can be a useful addition toexisting parsers. The component parser has some similarities to the predicateparser. The lexicon of verbs (actions) is the same for example.

    ---------

    Listing 5---------

    /* COMPONENT PARSER */

    /*---------------*//* Include files *//*---------------*/#include #include #include #include

  • 7/27/2019 Conversations With My Computer - Probert

    42/230

    /*-----------*//* Constants *//*-----------*/#define num_a 140#define num_p 12#define num_q 41#define num_l 20#define num_n 37#define num_c 14#define num_f 17#define num_qs 19#define num_s 9#define num_o 3

    /*-----------*//* Lexicons *//*-----------*/static char *actions[num_a] = { "LIKE" , "DISLIKE", "LOVE" , "HATE" , "SLEEP" ,

    "PUMP", "MAKE" , "WEAR" ,"RENOUNCE", "SHORTEN","KEEP", "FEED", "DESCEND" , "CONVERT", "KEEP",

    "OPEN" , "EAT" , "READ" , "DRY" , "KILL" ,"MEND","CAUSE", "SIT" , "SAT" , "CLOSE","TAKE", "RUN" , "JUMP", "SKIP", "HOP", "BREAK","INVENT", "DISCOVER", "EXPLORE", "PUNCH","KICK","IGNITE", "CHAT", "WRITE", "TALK","LISTEN","WHISTLE","SHOUT","SCREAM","DRINK","BATHE","WASH","CROSS", "ENTER", "CLIMB", "TIE","BREATHE", "SNIFF", "SPIT" ,"GO","PUT","DRINK","LOOK", "TURN", "BORN","LEAVE", "URINATE","SMOKE", "LIGHT", "DESIGN" , "DRAW" ,"PAINT","BOIL", "STRAIN", "POUR","TELEPHONE","PHONE","CALL" ,"DRESS", "CUT", "SAW", "GONE","STUDY","FOLD" , "PLAY", "PAY", "EARN","MEET", "MET",

    "LIVE","DWELL", "BEEN", "HEAR" , "HEARD","SING" , "SUNG", "DEVELOP", "STIR","SEE","SEEN", "RING", "FEEL" , "FELT", "DIED", "HUNT","ASK", "STAY" ,"KISS", "CUDDLE", "GIVE","BUY","BOUGHT", "LICK" , "TASTE", "ADMIRE" ,"PROVIDE","COME", "BUILD", "BUILT", "REFERENCE" ,"WENT","CONSTRUCT","LEAN","WALK","CARRY","RIDE","ORBIT","DRIVE" , "DROVE" , "DOWN", "ALONG","SHALL","SHOULD","THINK", "THOUGHT","GOT","CROUCH","CRAWL", "GET","LEND","LENT","HAVE", "HAS" , "HAD","SUGGEST" };

  • 7/27/2019 Conversations With My Computer - Probert

    43/230

    static char *questions[num_qs] = { "WHAT", "WHEN", "HOW", "WHY", "WHERE","WHO","DO", "DOES" ,"ARE", "IS", "WAS","CAN" ,"AM" ,"WHICH","HAVE" , "WILL" ,"DID" , "HAS", "SHOULD"};

    static char *orders[num_o] = { "USE" , "TELL", "QUIT" };

    static char *fillers[num_f] = { "IS", "THE" , "A", "AN" , "ARE" ,"TOO","ALSO","OF" , "COME" , "FROM" , "ITS" , "THEIR" ,"THAT" , "ABOUT", "THING" , "THIS" , "BECAUSE" };

    static char *qualifiers[num_q] = {"FAST", "SLOW", "ROUND", "SQUARE","HAIRY", "BALD", "FRIGHTENING","INTERESTED","SHY", "NICE", "HORRIBLE" , "LOVELY","INTERESTING","PRETTY" , "BEAUTIFUL" , "BLONDE" , "BRUNETTE" ,"SEXY" , "SHINY" , "DULL" , "BORING" , "CAREFUL" ,"HARD" , "SOFT" , "GENTLE" , "QUICK" , "LIGHT","HEAVY", "RADIOACTIVE", "METALLIC","TIRED","GASEOUS", "BRIGHT", "SWEET", "SOUR" , "BITTER","GOOD","BAD","DRUNK","STUPID","MUTUAL" };

    static char *sizes[num_s] = {"BIG", "SMALL", "FAT", "THIN", "TALL" ,"SHORT","LARGE", "PETITE", "TINY" };

    static char *colours[num_c] = { "BLACK", "BLUE", "RED", "YELLOW", "ORANGE","GREEN","BROWN" , "GINGER" , "PURPLE","MAUVE", "SILVERY", "SILVER", "GOLD", "CYAN" };

    static char *numbers[num_n] = { "ALL" , "SOME" , "FEW" , "MANY" , "MOST" ,"COUPLE","NEXT","PREVIOUS","ONE", "TWO", "THREE" ,"FOUR", "EVERY", "LAST","FIVE", "SIX", "SEVEN","EIGHT", "NINE", "TEN",

    "ELEVEN","TWELVE", "THIRTEEN","FOURTEEN","FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN","TWENTY", "THIRTY", "FORTY","FIFTY", "SIXTY","SEVENTY", "EIGHTY","NINETY" };

    static char *pronouns[num_p] = { "THEM", "THEY", "WE", "HIM", "HER" ,"US" ,"HE", "I", "YOU" ,"MY", "YOUR" , "ME"};

  • 7/27/2019 Conversations With My Computer - Probert

    44/230

    static char *locations[num_l] = { "IN" , "ON", "BESIDE" , "EAST" , "WEST" ,"NORTH" ,"SOUTH" , "UNDER" , "UP" ,"DOWN", "BELOW", "ABOVE", "RIGHT", "LEFT","OVER","INTO","AROUND" , "TO", "OFF","THERE" };

    /*------------*//* Work areas *//*------------*/static char last_subject[80];static char last_object[80];

    /*---------------------*//* Function prototypes *//*---------------------*/int IS_COLOUR(char *);int IS_FILLER(char *);int IS_LOCATION(char *);int IS_NUMBER(char *);

    int IS_PRONOUN(char *);int IS_SIZE(char *);void strchg(char *, char *, char *);void truncstr(char *,int );

    /*--------------------*//* Start of functions *//*--------------------*/static void GET_QUESTION(char *text, char *question){

    /* Extract a question from the phrase string */

    char *p;

    char first_word[40];char save;int n;int more;

    strcpy(question,"");

    do{

    more = 0;p = strpbrk(text," ,.;:?!");save = *p;*p = 0;

    strcpy(first_word,text);

  • 7/27/2019 Conversations With My Computer - Probert

    45/230

    *p = save;

    for(n = 0; n < num_qs; n++){

    if (stricmp(first_word,questions[n]) == 0){

    /* store question */if (*question)

    strcat(question," ");strcat(question,first_word);/* remove question from text */if (p){

    while(strchr(" ,.:;?!",*p) && *p)p++;

    if (*p)memmove(text,p,strlen(p) + 1);

    }else

    *text = 0;

    more = 1;}}

    }while(more);

    }

    static void GET_ORDER(char *text, char *order){

    /* Extract an order from the phrase string */

    char *p;char first_word[40];

    char save;int n;

    strcpy(order,"");p = strpbrk(text," ,.;:?!");save = *p;*p = 0;strcpy(first_word,text);*p = save;

    for(n = 0; n < num_o; n++){

    if (stricmp(first_word,orders[n]) == 0)

    {

  • 7/27/2019 Conversations With My Computer - Probert

    46/230

    /* store order */strcpy(order,first_word);/* remove order from text */while(strchr(" ,.:;?!",*p))

    p++;memmove(text,p,strlen(p) + 1);return;

    }}

    }

    static void GET_OBJECT(char *text, char *object, char *action){

    /* Obtain object from phrase string */

    char *p;int n;int ignore;char temp[30];char copy[250];

    strcpy(copy,text);p = strtok(text," ,.;:?!");

    while(p){

    ignore = 0;

    if (stricmp(p,action) == 0)ignore = 1;

    if (!ignore)ignore = IS_FILLER(p);

    if (!ignore)ignore = IS_NUMBER(p);

    if (!ignore)ignore = IS_COLOUR(p);

    if (!ignore)ignore = IS_SIZE(p);

    if (!ignore)ignore = IS_LOCATION(p);

    if (!ignore)

  • 7/27/2019 Conversations With My Computer - Probert

    47/230

    ignore = IS_PRONOUN(p);

    if (!ignore){

    for (n = 0; n < num_q; n++){

    if (stricmp(p,qualifiers[n]) == 0)ignore = 1;

    else{

    strcpy(temp,qualifiers[n]);if (temp[strlen(temp) - 1] == 'E'){

    temp[strlen(temp) - 1] = 0;if (temp[strlen(temp) - 1] == 'L')

    temp[strlen(temp) - 1] = 0;}strcat(temp,"LY");if (stricmp(p,temp) == 0){

    ignore = 1;break;}

    }}

    }if (!ignore){

    for(n = 0; n < num_qs; n++){

    if (stricmp(p,questions[n]) == 0){

    ignore = 1;

    break;}}

    }

    if (!ignore){

    if (stricmp(p,"IT") == 0)strcpy(object,last_object);

    elsestrcpy(object,p);

    strcpy(text,copy);p = strstr(text,object);

    strcpy(last_object,"");

  • 7/27/2019 Conversations With My Computer - Probert

    48/230

  • 7/27/2019 Conversations With My Computer - Probert

    49/230

    p = strtok(text," ,.;:?!");

    while(p){

    ignore = 0;if (stricmp(p,object) == 0)

    ignore = 1;

    if (!ignore)ignore = IS_FILLER(p);

    if (!ignore)ignore = IS_LOCATION(p);

    if (!ignore)ignore = IS_PRONOUN(p);

    if (!ignore)ignore = IS_COLOUR(p);

    if (!ignore)ignore = IS_SIZE(p);

    if (!ignore)ignore = IS_NUMBER(p);

    if (stricmp(p,action) == 0)ignore = 1;

    if (!ignore){

    if (stricmp(p,"IT")){

    if (*subject)strcat(subject," ");strcat(subject,p);

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    50/230

    static void GET_OBJECT_QUALIFY(char *text, char *object_qualify,char *subject)

    {/* Obtain qualifying statements from phrase string */

    char *p;int n;int ignore;char oq[78];char copy[250];char temp[30];

    strcpy(copy,text);strcpy(object_qualify,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_q; n++)

    { if (stricmp(p,qualifiers[n]) == 0){

    if (*object_qualify)strcat(object_qualify,",");

    strcat(object_qualify,p);break;

    }else{

    strcpy(temp,qualifiers[n]);if (temp[strlen(temp) - 1] == 'E'){

    temp[strlen(temp) - 1] = 0;if (temp[strlen(temp) - 1] == 'L')temp[strlen(temp) - 1] = 0;

    }strcat(temp,"LY");if (stricmp(p,temp) == 0){

    if (*object_qualify)strcat(object_qualify,",");strcat(object_qualify,p);break;

    }}

    }

  • 7/27/2019 Conversations With My Computer - Probert

    51/230

    p = strtok(NULL," ,.;:?!");}strcpy(text,copy);

    ignore = 0;

    strcpy(oq,object_qualify);

    p = strtok(object_qualify,",");while(p){

    if (stricmp(p,subject) == 0){

    ignore = 1;break;

    }p = strtok(NULL,",");

    }

    strcpy(object_qualify,oq);

    if (!ignore)for (n = 0; n < num_q; n++){

    if (stricmp(subject,qualifiers[n]) == 0){

    if (*object_qualify)strcat(object_qualify,",");

    strcat(object_qualify,subject);strcpy(subject,"");break;

    }}

    }

    static void GET_NUMBER(char *text, char *number){

    /* Obtain number words from phrase string */

    char *p;char copy[250];int n;

    strcpy(copy,text);strcpy(number,"");

    p = strtok(text," ,.;:?!");

  • 7/27/2019 Conversations With My Computer - Probert

    52/230

    while(p){

    for(n = 0; n < num_n; n++){

    if (stricmp(p,numbers[n]) == 0){

    if (*number)strcat(number,",");

    strcat(number,p);break;

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

    static void GET_WHO(char *text, char *who){

    /* Obtain first or second person references from phrase string */

    char *p;char copy[250];int n;

    strcpy(copy,text);strcpy(who,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_p; n++)

    { if (stricmp(p,pronouns[n]) == 0){

    if (*who)strcat(who,",");

    strcat(who,p);break;

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    53/230

    static void GET_COLOUR(char *text, char *colour){

    /* Obtain colour words from the phrase string */

    char *p;char copy[250];int n;

    strcpy(copy,text);strcpy(colour,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_c; n++){

    if (stricmp(p,colours[n]) == 0){

    if (*colour)

    strcat(colour,",");strcat(colour,p);break;

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

    static void GET_SIZE(char *text, char *size){

    /* Obtain size description words from the phrase string */

    char *p;char copy[250];int n;

    strcpy(copy,text);strcpy(size,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_s; n++)

    {

  • 7/27/2019 Conversations With My Computer - Probert

    54/230

    if (stricmp(p,sizes[n]) == 0){

    if (*size)strcat(size,",");

    strcat(size,p);break;

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

    static void GET_LOCATION(char *text, char *location){

    /* Obtain location description words from the phrase string */

    char *p;char copy[250];int n;

    strcpy(copy,text);strcpy(location,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_l; n++){

    if (stricmp(p,locations[n]) == 0){

    if (*location)

    strcat(location,",");strcat(location,p);break;

    }}p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    55/230

    static void GET_ACTION(char *text , char *action){

    /* Obtain action words from the phrase string */

    char *p;char n;char word[50];char pre[50];char copy[250];

    strcpy(copy,text);strcpy(action,"");strcpy(pre,"");

    p = strtok(text," ,.;:?!");

    while(p){

    for(n = 0; n < num_a; n++){

    strcpy(word,actions[n]);if (stricmp(p,word) == 0){

    if (stricmp(pre,"THE")){

    if (*action)strcat(action,",");

    strcat(action,p);}break;

    }else{

    /* Past tense */if (word[strlen(word) - 1] == 'E')strcat(word,"D");

    elseif (word[strlen(word) - 1] == 'Y'){

    /* Irregular words! */if (stricmp(word,"PAY") == 0)

    strcpy(word,"PAID");elseif (stricmp(word,"PLAY") == 0)

    strcpy(word,"PLAYED");else

    {

  • 7/27/2019 Conversations With My Computer - Probert

    56/230

    word[strlen(word) - 1] = 0;strcat(word,"IED");

    }}else

    strcat(word,"ED");if (stricmp(p,word) == 0){

    if (*action)strcat(action,",");

    strcat(action,p);break;

    }else{

    /* Plural */strcpy(word,actions[n]);strcat(word,"S");if (stricmp(p,word) == 0){

    if (stricmp(pre,"THE")){if (*action)

    strcat(action,",");strcat(action,p);break;

    }}else{

    /* Active */strcpy(word,actions[n]);strcat(word,"ING");

    if (stricmp(p,word) == 0){if (stricmp(pre,"THE")){

    if (*action)strcat(action,",");

    strcat(action,p);break;

    }}

    }}

    }

    }

  • 7/27/2019 Conversations With My Computer - Probert

    57/230

    strcpy(pre,p);p = strtok(NULL," ,.;:?!");

    }strcpy(text,copy);

    }

    static int IS_FILLER(char *p){

    /* Return 1 if the word is a filler word */

    char n;

    for(n = 0; n < num_f; n++){

    if (stricmp(p,fillers[n]) == 0)return(1);

    }

    return(0);}

    static int IS_NUMBER(char *p){

    /* Return 1 if the word is a number */

    int n;

    for(n = 0; n < num_n; n++){

    if (stricmp(p,numbers[n]) == 0)return(1);

    }return(0);

    }static int IS_COLOUR(char *p){

    /* Return 1 if the word is a colour */char n;

    for(n = 0; n < num_c; n++){

    if (stricmp(p,colours[n]) == 0)return(1);

    }return(0);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    58/230

    static int IS_SIZE(char *p){

    /* Return 1 if the word is a size description word */

    char n;

    for(n = 0; n < num_s; n++){

    if (stricmp(p,sizes[n]) == 0)return(1);

    }return(0);

    }

    static int IS_LOCATION(char *p){

    /* Return 1 if the word is a location description */

    char n;

    for(n = 0; n < num_l; n++){if (stricmp(p,locations[n]) == 0)

    return(1);}return(0);

    }

    static int IS_PRONOUN(char *p){

    /* Return 1 if the word is a first or second person reference */

    char n;for(n = 0; n < num_p; n++){

    if (stricmp(p,pronouns[n]) == 0)return(1);

    }return(0);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    59/230

    void NLP(char *text, char *question, char *order, char *object,char *object_qualify, char *subject, char *number, char *action,char *location, char *colour, char *size, char *who, char *tense)

    {/* Parses a simple phrase and returns the component parts */

    char *p;char *q;

    /* DON'T must be protected from expansion */strchg(text,"DON'T","DON|T");

    /* Expand "'RE" */strchg(text,"'RE"," ARE");

    /* Expand "'S" */strchg(text,"'S"," IS");

    /* Expand I'M */strchg(text,"I'M","I AM");

    /* Expand "N'T" */strchg(text,"N'T"," NOT");

    /* Expand "I'VE" */strchg(text,"I'VE","I HAVE");

    /* Expand "'re" */strchg(text,"'re"," are");

    /* Expand "'s" */strchg(text,"'s"," is");

    /* Expand I'm */strchg(text,"I'm","I am");

    /* Expand "n't" */strchg(text,"n't"," not");

    /* Expand "I've" */strchg(text,"I've","I have");

    /* DON'T must be protected from expansion */strchg(text,"DON|T","DON'T");

    GET_QUESTION(text, question);

    GET_ACTION(text , action);

  • 7/27/2019 Conversations With My Computer - Probert

    60/230

  • 7/27/2019 Conversations With My Computer - Probert

    61/230

    if (*tense == 0){

    /* Perhaps action gives a clue */if (*action){

    if (strchr(action,',') == NULL){

    p = &action[strlen(action) - 2];if (stricmp(p,"ED") == 0)

    *tense = 1;}else{

    q = action;do{

    p = strchr(q,',');q = p - 1;if (*q == 'S'){

    *tense = 2;break;}q--;if (stricmp(q,"ED") == 0){

    *tense = 1;break;

    }q = p + 1;

    }while(p);

    }

    }}

    switch(*tense){

    case 1: printf("past");break;

    case 2: printf("present");break;

    case 3: printf("future");break;

  • 7/27/2019 Conversations With My Computer - Probert

    62/230

    default: printf("indeterminable");}

    }

    void main(){

    char a[80]; /* question */char b[80]; /* order */char c[80]; /* object/directive */char d[80]; /* object qualifier */char e[80]; /* subject */char f[80]; /* number */char g[80]; /* action */char h[80]; /* location/direction */char i[80]; /* Colour */char j[80]; /* size */char k[80]; /* who */char tense; /* Tense, past present or future */

    char text[100];

    do{

    printf("\nEnter a phrase\n");gets(text);if (*text != 0){

    strupr(text);NLP(text,a,b,c,d,e,f,g,h,i,j,k,&tense);

    }}while(*text);

    }

    In trying to understand a phrase, extensive use of the component parts ismade, with occasional referrals back to the phrase as a whole. The problems of"understanding", rather than "responding" are discussed in a later chapter.

    By breaking a phrase down into components understanding various forms of thesame phrase becomes easier. Let's take the basic phrase, "I'M BORED". This canhave variations such as "I AM BORED", "I AM VERY BORED", "I AM EXTREMELYBORED" and so on. But the basic message is the same, "I AM BORED". Withoutbreaking the phrase into components we have to compare the phrase against avast lexicon of known phrases. However, with components we only need tocompare the object and subject, either of which will be set to "BORED" in all

    the previous examples of the same basic message.

  • 7/27/2019 Conversations With My Computer - Probert

    63/230

    The component parser decides whether a phrase is a question, or not byextracting the first word of the phrase and comparing it against a lexicon ofknown questions; WHO, WHAT, WHERE, WHEN, WHY, WOULD, SHOULD, DO, DID, IS, ARE,WILL, CAN. If a match is found then the phrase is a question. For example;

    "Where is the cat?"

    The first word is extracted, "WHERE", and upon comparing it against thelexicon of questions a match is found. So this phrase is a question.

    "Do you think Arsenal will win the match?"

    The first word is extracted, "DO", and upon comparing it against the lexiconof questions a match is found. So this phrase is a question.

    You might recognise this technique from the slot-and-frame parsers describedearlier.

    A further stage of component parsing is deciding the nature of a question orstatement. questions are of two different types; request and interrogation.

    Although both request something, the interrogation requests furtherinformation. Human's tend to communicate ambiguously, as in the question;"Have you got a light?" Which, under some circumstances actually means; "Canyou give me a source of ignition". Under other circumstances it means, "Can Ihave a source of light". Finally this phrase may actually be an interrogationasking if you have a source of light.

  • 7/27/2019 Conversations With My Computer - Probert

    64/230

    QUALIFIERS AND AMBIGUITY

    Computer's are remarkably stupid machines, when compared to the human brain. Aphrase that may appear straight forward and obvious to us, "Got a light,mate?" Will confuse any but the most sophisticated conversational computerprograms. Why? If we ignore the idea of "common phrases", and limit our selvesto syntactical parsing. Then we may start to understand the problems. Perhapseven to sympathise with the computer program trying to respond intelligently.

    Referring back to the dreadful phrase, "Have you got a light?". Let us assumethis to be a straight forward interrogation asking if you have a source oflight. The question is unclear. It may mean do you own a source of light, orit may be asking if you have a source of light in your possession. This lackof clarity is often used in comedy. So there you are, out on a dark night witha friend. You need the use of a torch and so you ask, "Have you got a light?".Your friend owns a torch and so replies quite truthfully, "Yes". Now you arestarting to get agitated. You were actually requesting a torch, and so you say"Can I borrow it please?". Another unclear phrase. Your friend being a

    generous chap, replies "Yes, you can borrow my torch." You still have notreceived what you wanted, a torch, so you say "Give me your torch please", atlast a clear statement. Your friend replies "I can't". And so it goes on untilat lat you discover that the torch, which your friend owns, is not in hispossession.

    We can illustrate this more clearly perhaps with an imaginary repetoir. Thescene is a dark, moonless night. You are with a friend standing in a wood. Thefriend is obviously obtuse. He might even be an android, because the answershe gives to seemingly obvious questions reflect very little comprehension.

    you: (Requesting a torch) Have you got a light?

    friend: Yes.you: Can I borrow it, please?

    friend: Yes, you can borrow my torch.

    you: Give me your torch! Please!

    friend: I can't.

    you: Why not?

    friend: Why not what?

  • 7/27/2019 Conversations With My Computer - Probert

    65/230

    you: Why can't you give me your torch?

    friend: Because I haven't got it with me.

    you: Where is it?

    friend: Where is what?

    you: The torch!

    friend: Which torch?

    you: The torch I want to borrow.

    friend: What about the torch you want to borrow?

    you: Where is your torch?

    friend: My torch is at home.

    This is the sort of problem that pursues you when trying to create aconversational computer program. Unlike a human, a computer does notautomatically cross reference between current and previous phrases. Thesimplest example of this the use of the word "it". We use "it" a lot. Forexample.

    "Have you got the book I lent you?""Yes""Can I have it back please"

    You understand that in the last sentence, "Can I have it back please", thatthe word "it" refers to "the book I lent you".

  • 7/27/2019 Conversations With My Computer - Probert

    66/230

    UNDERSTANDING AND RESPONDING

    At first glance, if a computer program gives an appropriate reply to an input,then the program has understood what was required of it. This would be anerroneous deduction to make. Responding in an appropriate manner does notimply an understanding. Rather it may simply be that a programmed response wasgiven to a recognisable input. Slot-and-frame parsing again. Search one tablefor the input, and give a corresponding entry from another table.

    Understanding requires intelligence. Consider a dialogue between two people.John walks into his local pub one evening. At the bar he observes his friendMartin and so he walks up to him. Following the ordering of a drink John turnsto Martin.

    John: Evening Martin!

    Martin: Hello John. Alright?

    John: A bit tired.Martin: A hard day at work?

    John: No my mother went into hospital last night.

    Martin: Nothing serious I hope?

    John: A stroke.

    Martin: I'm sorry.

    John: That's okay. It's not your fault. Still, did you see the football last

    night?Martin: Who was playing?

    And so it continues. If we look at the intercourse that took place we willlearn quite a lot about "understanding". The exchange starts with "EveningMartin". Martin recognises this as a common phrase, and responds with one ofthe possible standard responses. Perhaps John looks tired, or may be Martin isjust being polite, but he asks after John's health and well being. John knowsthat the interrogation, "alright?" does not require an indepth reply. He knowsthis because of the circumstances of the intercourse taking place. If he wastalking to his doctor, in a consultation he might give an in-depth reply. Butin this circumstance he gives a short answer. Martin's response to John's

    answer, "a bit tired", is again a standard response to a common phrase. He

  • 7/27/2019 Conversations With My Computer - Probert

    67/230

    doesn't care if he gets an answer or not. The reply that John's mother hasgone into hospital generates an intelligent response from Martin. Martinunderstands the phrase, and has been taught that;

    a) Mother's give pleasure.

    b) People go into hospital if they are sick.

    c) Being sick is not pleasant, it is suffering.

    d) Someone who gives pleasure suffering passes on suffering to the person towhom they give pleasure. Therefore, by John's mother going into hospitalJohn is also suffering.

    Martin's intelligent response is none-the-less a standard phrase for thesecircumstances. However, it is selected because it is appropriate for thecircumstances. John is taking a passive role in the conversation. He answersMartin's questions simply and honestly. He knows from experience that a strokeis a serious ailment. He knows that the common phrase "nothing serious Ihope?" Requires different answers. If the ailment is not serious, then a

    simple "no", perhaps with qualification is required. If the condition isserious, then the condition itself is described. Martin, upon receiving theanswer "stroke" remembers what was being talked about. That John's mother hadgone into hospital, and therefore deduced that "stroke" applied to hercondition. Martin has been taught that a stroke is a dangerous ailment, andreplies with a standard phrase expressing emotion that he hopes will helpJohn. Why does Martin wish to help John? Perhaps because if John is sufferingthen he, Martin, as a friend will also suffer. And suffering is not nice. Johnalso replies with an intelligent response in the same vein to stop causingMartin any suffering. He may know that Martin enjoys watching football. Or hemay deduce that as Martin is a male adult, and that male adults enjoy watchingfootball so does Martin.

    Within this exchange we notice that the understanding process requiresrecalling to memory what has been learnt about a vast range of subjects in thepast. We also notice prediction occurring. Both parties attempts to predictwhat the response will be if they supply supply certain stimuli. These stimulibeing phrases. This prediction is based upon their memories of responses tophrases, and also what their own response would be. If Martin's mother hasnever gone into hospital with a stroke. If Martin has never known of anyoneelse ever going into hospital with a stroke, he can still make judgementsbased upon similar circumstances.

  • 7/27/2019 Conversations With My Computer - Probert

    68/230

    Currently available computers do not have the memory capacity, or the speed toretain sufficient facts and be able to cross reference them as the human minddoes. With CD-ROMs the memory limitations may be overcome, the speed problemwill remain I believe until optical circuitry is implemented, with lightenergy taking the place of DC current. In the meantime the talking computer islimited to responding without very much understanding.

  • 7/27/2019 Conversations With My Computer - Probert

    69/230

    GEORGE

    George is an example of how a computer program can be written that willconverse with a human operator. As we have already established, a computercannot currently converse very intelligently. However, George makes use of afew simple techniques to ask the operator questions and respond to the answersgiven.

    The program is in two parts. The first part is the component parser listingalready described. The second is the listing that follows. You should be ableto easily add more prompts and develop your own personality slant for theprogram.

    George is not just a demonstration. It can also be used to develop ideas forcreative writing and may even help you sort out difficult decisions! Georgeechoes the entire conversation to a disk file entitled "george.con", which isdestroyed each time george is run, so make sure you make a copy if you want tokeep a particular conversation!

    ---------Listing 6---------

    /*-----------------------------------------------*//* Demonstration conversational computer program *//*-----------------------------------------------*/#include #include #include #include

    #include #include #include

    char data[1000];char last_question;

    char user[30];

    char *family[] = { "MOTHER", "FATHER", "BROTHER", "SISTER", "SON","DAUGHTER", "COUSIN", "AUNTY", "UNCLE" , "" };

  • 7/27/2019 Conversations With My Computer - Probert

    70/230

    char *names[42] = { "ALAN", "ANNE", "ANN", "BRIAN", "JAMES", "JIM", "SUE","SUSAN", "ROGER", "PETE", "PETER", "MATT", "MATTHEW","MATHEW", "IAN", "HARRY", "TONY", "ALISON", "TANYA","RICK", "RICHARD", "MICHAEL", "MARTIN", "MIKE","TIM","TIMOTHY", "GEORGE","HOWARD", "CAROLINE", "SARA", "SARAH","KATE", "KATY", "TOM", "THOMAS", "JON", "JONATHAN","CHARLES", "VINCE", "VINCENT","BILL","WILLIAM" };

    char *subjects[14] = { "THE GOVERNMENT", "FRENCH FARMERS", "THE SUN NEWSPAPER","THE SUNDAY SPORT", "INFLATION", "ABORTION", "MARRIAGE","CHILDREN", "CHANNEL FOUR", "BBC2", "MICHAEL JACKSON","WOOPI GOLDBERG", "REAL ALE","THE ENVIRONMENT" };

    char *authors[11] = { "FREUD", "ENID BLYTON", "JUNG", "LAING", "SHAKESPEAR","DICKENS", "W.E. JOHNS", "JACK HIGGINS" ,"SCIENCE FICTION" , "TOLSTOY" , "TOLKEIN" };

    /*------------------------------------*/

    /* Data we have learnt about the user *//*------------------------------------*/char known[50][100];int fkn;

    FILE *echo;char query;char fc;int ft;char cf;char more;

    /*---------------------*/

    /* Function prototypes *//*---------------------*/int DO_YOU_KNOW(void);void NEED(void);void NLP(char *, char *, char *, char *,char *, char *, char *, char *,

    char *, char *, char *, char *, char *);int READ(void);void REQUIRE(void);void WANT(void);int WHAT_ABOUT(void);void cdecl xprintf(char *, ...);char *strftos(char *);char *strrstr(char *, char *);

    void strwhy(char *);

  • 7/27/2019 Conversations With My Computer - Probert

    71/230

    void FEED(void);void BORROW(void);

    void cdecl xprintf(char *format, ...){

    /* This function echoes the output to the screen and to a disk file */

    va_list arg_ptr;static char output[1000];

    va_start(arg_ptr, format);vsprintf(output, format, arg_ptr);va_end(arg_ptr);

    fputs(output,echo);

    printf("%s",output);}

    void BORROW(){

    /* Personality code *//* What do they want to borrow ? */

    char *p;

    p = strrstr(data,"BORROW");

    if (strcmp(p,"A QUID") == 0 || strcmp(p,"A FIVER") == 0 ||strcmp(p,"A POUND") == 0)

    {

    xprintf("HOW DO I KNOW I'LL GET IT BACK?");}else

    xprintf("NOT BLOODY LIKELY!");}

  • 7/27/2019 Conversations With My Computer - Probert

    72/230

  • 7/27/2019 Conversations With My Computer - Probert

    73/230

    elseif (ft == 0){

    xprintf("DO YOU LIKE PLAYING FOOTBALL?");query = 1;ft = 1;last_question = 5;stop = 1;

    }elseif (ft == 1){

    xprintf("DO YOU LIKE WATCHING FOOTBALL?");query = 1;ft = 2;last_question = 5;stop = 1;

    }elseif (cf == 0 && !random(20))

    { xprintf("DO YOU LIKE CHINESE FOOD?");cf = 1;query = 1;last_question = 5;stop = 1;

    }elseif (cf == 1 && !random(20)){

    xprintf("DO YOU LIKE INDIAN FOOD?");cf = 2;query = 1;

    last_question = 5;stop = 1;}elseif (cf == 2 && !random(20)){

    xprintf("HAVE YOU EVER TRIED JAMAICAN CUISINE?");cf = 3;query = 1;stop = 1;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    74/230

    elseif (!query && fkn > 1){

    n = random(fkn);if (strncmp("I ",known[n],2) == 0){

    p = &known[n][2];xprintf("YOU %s",p);stop = 1;

    }}elseif (!random(15))

    stop = DO_YOU_KNOW();

    if (!stop){

    if (!random(15))stop = WHAT_ABOUT();

    }

    if (!stop){

    if (!random(10))stop = READ();

    }

    if (!stop){

    if (!random(3))xprintf("HMMMMM.");

    elsexprintf("I SEE.");

    }}}

    void WANT(){

    char *p;

    p = strrstr(data,"I");xprintf("WHY DO YOU %s?",p);query = 1;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    75/230

    void NEED(){

    /* Respond to a 'I need' statement */char *p;

    p = strrstr(data,"I NEED");xprintf("HOW WILL YOU FEEL IF YOU GET %s?",p);query = 1;

    }

    void REQUIRE(){

    /* Respond to a 'I require' statement */char *p;

    p = strrstr(data,"I REQUIRE");xprintf("HOW WILL YOU FEEL IF YOU GET %s?",p);query = 1;

    }

    int DO_YOU_KNOW(){/* Generates a random 'DO YOU KNOW.....' question *//* Once a name has been used it is destroyed to prevent the same *//* question being asked again */

    int n;n = random(42);

    if (*names[n]){

    printf("DO YOU KNOW %s?",names[n]);query = 1;

    strcpy(names[n],"");return(1);}return(0);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    76/230

    int WHAT_ABOUT(){

    /* Generates a random 'WHAT DO YOU THINK ABOUT.....' question *//* Once a subject has been used it is destroyed to prevent the same *//* question being asked again */

    int n;n = random(14);

    if (*subjects[n]){ printf("WHAT DO YOU THINK ABOUT %s?",subjects[n]);

    strcpy(subjects[n],"");query = 1;return(1);

    }else

    return(0);}

    int READ()

    { /* Generates a random 'HAVE YOU READ ANY.....' question *//* Once an author has been used it is destroyed to prevent the same *//* question being asked again */

    int n;n = random(11);

    if (*authors[n]){

    printf("HAVE YOU READ ANY %s?",authors[n]);strcpy(authors[n],"");query = 1;

    return(1);}return(0);

    }

  • 7/27/2019 Conversations With My Computer - Probert

    77/230

    void RESPOND(char *data, char *question, char *order, char *object,char *object_qualify, char *subject, char *number, char *action,char *location, char *colour, char *size, char *who, char *tense)

    {int done;int n;char *p;

    /* The 'done' flag indicates when a response has been given */done = 0;

    /* Remove trailing punctuation from data */if (strchr("!?.",data[strlen(data) - 1]))

    data[strlen(data) - 1] = 0;

    /* Remove trailing spaces */p = &data[strlen(data) - 1];while(*p == 32 && p >= data)

    *p-- = 0;

    /* Personality code *//* The operator has mentioned colours */if (*colour && fc == 0)

    fc = 1;

    /* Personality code *//* Learn about the operator */if (strstr(who,"I") && fkn < 50){

    fkn++;strcpy(known[fkn],data);

    }

    /* Personality code *//* Is the operator talking about their family? */if (!last_question){

    for(n = 0; *family[n]; n++){

    if(strstr(data,family[n])){

    last_question = 3;break;

    }}

    }

  • 7/27/2019 Conversations With My Computer - Probert

    78/230

    /* Bad phrase */if (*data == 0){

    done = 1;xprintf("I DON'T UNDERSTAND");

    }

    /* Automatic responses to standard phrases */if (strcmp(data,"THAT IS OKAY") == 0 && !done){

    done = 1;FEED();

    }if (strcmp(data,"WHERE IS SPAIN") == 0 && !done){

    done = 1;xprintf("IN EUROPE.");

    }

    if (strcmp(object,"HELLO") == 0 || strcmp(object,"HI") == 0 ||last_question == 6 && !done){

    if(strcmp(who,"I") == 0){

    p = strrstr(data,"AM");if(p){

    done = 1;if (!*user){

    strcpy(user,p);xprintf("PLEASED TO MEET YOU %s.",user);

    }elsexprintf("I DO REMEMBER YOU NAME, %.",user);

    if (last_question == 6 || last_question == 7)last_question = 0;

    }}

    }

  • 7/27/2019 Conversations With My Computer - Probert

    79/230

  • 7/27/2019 Conversations With My Computer - Probert

    80/230

    if (strcmp(data,"I HAVE ALREADY SAID") == 0 ||strcmp(data,"I HAVE ALREADY TOLD YOU") == 0 ||strcmp(data,"I HAVE ALREADY TOLD YOU THAT") == 0 ||strcmp(data,"I HAVE ALREADY SAID THAT") == 0)

    {done = 1;if (query){

    xprintf("SORRY. MY MEMORY'S NOT WHAT IT USED TO BE.");query = 0;

    }else{

    p = strstr(data,"YOU");if (p){

    memmove(p,p+1,strlen(p) + 1);strncpy(p,"ME ",2);

    }p = data;

    memmove(p+2,p,strlen(p) + 1);strncpy(data,"YOU ",3);p = strstr(data,"THAT");if (p){

    *p = 'W';strcat(data,"?");

    }else

    strcat(data," WHAT?");query = 1;xprintf("%s",data);

    }

    }if (strcmp(data,"I HAVE") == 0){

    done = 1;xprintf("YOU HAVE WHAT?");query = 1;last_question = 8;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    81/230

    if (strcmp(object,"BORED") == 0 || strcmp(subject,"BORED") == 0&& strcmp(who,"I") == 0)

    {/* Personality code */done = 1;xprintf("JOIN THE CLUB!");

    }if(strncmp("YOU HAVE ALREADY ASKED ME",data,25) == 0){

    if (query){

    xprintf("HAVE I? SORRY!");done = 1;query = 0;

    }else{

    xprintf("I DIDN'T THINK I'D ASKED YOU ANYTHING");done = 1;

    }

    }if (strcmp(question,"WHAT") == 0 && !done){

    /* Standard what questions */if (strcmp(data,"SHALL WE TALK ABOUT") == 0){

    done = 1;xprintf("I DON'T KNOW. YOU CHOOSE A SUBJECT.");

    }

    }if (strcmp(question,"WHAT DO") == 0 && !done){

    if (strcmp(data,"YOU THINK ABOUT THE ECONOMY") == 0){done = 1;xprintf("IT'S IN A BIT OF A MESS.");

    }elseif (strcmp(data,"YOU THINK ABOUT OF THE ECONOMY") == 0){

    done = 1;xprintf("IT'S IN A BIT OF A MESS.");

    }

  • 7/27/2019 Conversations With My Computer - Probert

    82/230

    elseif (strcmp(data,"YOU THINK ABOUT THE STATE OF THE ECONOMY") == 0){

    done = 1;xprintf("IT'S IN A BIT OF A MESS.");

    }elseif (strcmp(data,"YOU THINK ABOUT THE STATE OF THE COUNTRY") == 0){

    done = 1;xprintf("IT'S IN A BIT OF A MESS.");

    }elseif (strcmp(data,"YOU THINK ABOUT THE ENGLISH CRICKET TEAM") == 0){

    done = 1;xprintf("IT'S IN A BIT OF A MESS.");

    }elseif (strcmp(data,"YOU THINK OF THE ENGLISH CRICKET TEAM") == 0)

    { done = 1;xprintf("IT'S PRETTY HOPELESS!");

    }elseif (strcmp(data,"YOU THINK ABOUT RUGBY") == 0){

    done = 1;xprintf("IT'S A GREAT GAME.");

    }elseif (strcmp(data,"YOU THINK ABOUT ENGLISH RUGBY") == 0){

    done = 1;xprintf("IT'S REALLY IMPROVING.");}

    }if (strstr(data,"I WAS ONLY ASKING") && !done){

    done = 1;xprintf("OKAY.");

    }

  • 7/27/2019 Conversations With My Computer - Probert

    83/230

    if (strcmp(question,"HOW IS") == 0 && strcmp(object,"LIFE") == 0 && !done){

    done = 1;xprintf("FINE THANKS.\n");FEED();

    }

    /* Personality code *//* Operator has replied NO to a question */if (query && strcmp(data,"NO") == 0 && !done){

    done = 1;if (last_question == 5){

    last_question = 0;xprintf("NOR DO I.\nI PREFER RUGBY.\n");FEED();query = 0;

    }else

    FEED();}

    /* Parsed data. Operator is introducing them self */if (strcmp(object,"NAME") == 0 && strcmp(who,"MY") == 0 && *subject){

    done = 1;if (!*user){

    strcpy(user,subject);xprintf("PLEASED TO MEET YOU %s.\n",user);

    }else

    xprintf("I DO REMEMBER YOUR NAME, %.\n",user);FEED();}

  • 7/27/2019 Conversations With My Computer - Probert

    84/230

    if (strncmp("I AM ",data,4) == 0 && !done){

    /* Personality code *//* Operator has made a statement about them self */if (!last_question){

    if (*object_qualify){

    xprintf("WHY ARE YOU %s?",object_qualify);query = 1;done = 1;

    }else{

    if (ft == -1 && !random(10))ft = 0;

    done = 1;FEED();

    }

    }elseif (*object_qualify){

    xprintf("WHY DO YOU THINK THAT IS?");query = 1;done = 1;

    }else

    xprintf("HMMMM");}/* Automatic response to standard phrase */if (strcmp(data,"I DO NOT KNOW") == 0)

    { done = 1;if (query && last_question == 4){

    xprintf("LET'S NOT GET METAPHYSICAL!");last_question = 0;query = 0;

    }

  • 7/27/2019 Conversations With My Computer - Probert

    85/230

    elseif (query)

    xprintf("WHAT DO YOU KNOW?");else{

    xprintf("WHAT DON'T YOU KNOW?");last_question = 4;

    }query = 1;

    }

    if (strncmp("BECAUSE ",data,8) == 0 && !done){

    if (query || *action){

    done = 1;if (strcmp(action,"SEE") == 0)

    FEED();elseif (!random(3))

    xprintf("AND YOU'RE SURE THAT'S WHY?");elsexprintf("ARE YOU SURE?");

    }else{

    done = 1;xprintf("I DON'T UNDERSTAND YOU.");

    }}

    if (strcmp(object,"WANT") == 0 && *who == 'I' && !done){

    done = 1;WANT();}

    if (strcmp(object,"NEED") == 0 && *who == 'I' && !done){

    done = 1;NEED();

    }

  • 7/27/2019 Conversations With My Computer - Probert

    86/230

    if (strcmp(object,"REQUIRE") == 0 && *who == 'I' && !done){

    done = 1;REQUIRE();

    }

    /* Semi-automatic reaction to strong phrase */if (strstr(action,"HATE") && strstr(data,"HATE ")){

    done = 1;if (strstr(data,"BECAUSE"))

    xprintf("ARE YOU SURE?");else{

    data = strstr(data,"HATE ");data = strchr(data,32);data++;xprintf("WHY DO YOU FEEL SO STRONGLY ABOUT %s?",data);

    }query = 1;

    }if (strstr(action,"LOVE") && strstr(data,"LOVE ")){

    done = 1;if (strstr(data,"BECAUSE"))

    xprintf("ARE YOU SURE?");else{

    data = strstr(data,"LOVE ");data = strchr(data,32);data++;xprintf("WHY DO YOU FEEL SO STRONGLY ABOUT %s?",data);

    }query = 1;}

    /* Parsed decision requested */if (strstr(subject," OR ") && !done){

    done = 1;p = strchr(action,',');if (p)

    *p = 0;data = strstr(data,action);p = strstr(data," OR ");

    *p = 0;

  • 7/27/2019 Conversations With My Computer - Probert

    87/230

  • 7/27/2019 Conversations With My Computer - Probert

    88/230

  • 7/27/2019 Conversations With My Computer - Probert

    89/230

    if (*question == 0 && *order == 0 && strchr(data,',') == NULL && !done){

    /* Statement */if ((strstr(subject,"THOUGHT") || strstr(subject,"THINKS")) &&

    *who != 'I'){

    xprintf("AND WHAT DO YOU THINK?");done = 1;query = 1;return;

    }

    if (*number == 0){

    if (strstr(data," ARE ")){

    if (!*object && *object_qualify)strcpy(object,object_qualify);

    elseif (!*subject && *object_qualify)

    strcpy(subject,object_qualify);done = 1;if (strcmp(who,"YOU")){

    if (*subject){

    if (*who == 0)xprintf("WHY DO YOU THINK %s ARE %s?",

    object,subject);else{

    if (*object_qualify)

    xprintf("WHAT'S SO %s ABOUT IT?",object_qualify);else

    xprintf("WHY DO YOU THINK THAT?");}

    }else

    xprintf("WHAT MAKES YOU THINK THEY ARE %s?",object);}

  • 7/27/2019 Conversations With My Computer - Probert

    90/230

  • 7/27/2019 Conversations With My Computer - Probert

    91/230

    if (*who == 0){

    if (strcmp(subject,object)){

    if (strstr(data,"IT IS NOT"))xprintf("NO?");

    elsexprintf("WHY DO YOU THINK %s IS %s?",object,

    subject);}else

    done = 0;}else{

    if (strcmp(who,"MY") == 0){

    p = strstr(data,object);p += strlen(object);while (*p == 32)

    p++;if (strncmp("IS ",p,3) == 0)p += 3;

    while (*p == 32)p++;

    xprintf("WHY DO YOU THINK YOUR %s IS %s?",object,p);}elseif (strcmp(who,"YOUR") == 0)

    xprintf("WHY DO YOU THINK MY %s IS %s?",object,subject);else{

    xprintf("I SEE.\n");

    FEED();}}query = 1;

    }}

  • 7/27/2019 Conversations With My Computer - Probert

    92/230

    else{

    if (strstr(data," ARE ")){

    if (!*object && *object_qualify)strcpy(object,object_qualify);

    elseif (!*subject && *object_qualify)

    strcpy(subject,object_qualify);

    done = 1;xprintf("WHY DO YOU THINK %s %s ARE %s?",number,object,subject);query = 1;

    }elseif (strstr(data," IS A ")){

    if (!*object && *object_qualify)strcpy(object,object_qualify);

    else

    if (!*subject && *object_qualify)strcpy(subject,object_qualify);

    done = 1;xprintf("WHY DO YOU THINK %s?",data);query = 1;

    }elseif (strstr(data," IS ")){

    if (!*object && *object_qualify)strcpy(object,object_qualify);

    else

    if (!*subject && *object_qualify)strcpy(subject,object_qualify);done = 1;xprintf("WHY DO YOU THINK %s %s IS %s?",number,object,subject);query = 1;

    }}

    }

  • 7/27/2019 Conversations With My Computer - Probert

    93/230

    if (strncmp("I USED TO THINK ",data,16) == 0 && !done){

    done = 1;xprintf("WHAT MADE YOU CHANGE YOUR MIND?");query = 1;

    }

    if (strncmp("I USED TO BE ",data,13) == 0 && !done){

    done = 1;xprintf("BUT YOU'RE NOT ANY MORE?");query = 1;

    }

    if (strcmp(action,"DIED") == 0 && strcmp(who,"MY") == 0 && *object && !done){

    done = 1;if (*subject)

    xprintf("I AM SORRY TO HEAR THAT YOUR %s %s %s.",object,

    subject,action);elsexprintf("I AM SORRY TO HEAR THAT YOUR %s %s.",object,action);

    xprintf("\nWERE YOU CLOSE?");query = 1;

    }

    if (*who && *object && !*subject && !*action &&