Slimelite BBS

NLP - Syntatical Structs

Back to DRIVING

From Matthew Probert · 1996-04-07 01:18 · #69256 in DRIVING

 * Carbons Sent to: ALL

                     SYNTACTICAL STRUCTURES IN NLP

                            Matthew Probert
                           Servile  Software


Sentences, if constructed properly, are comprised of two parts: 
subject and predicate. For example. The sentence "many hands make 
light work." Splits into "many hands" and "make light work". The 
predicative qualifies or describes the subject. 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 to 
misunderstandings! 

Within the subject, one word is the "subject word". In the sentence 
"many hands make light work.", the subject word is "hands" because it 
is a noun following an adjective. Whereas in the sentence, "most of 
the audience enjoyed the concert." The subject word is "most", denoted 
by an adjective phrase following 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. In the sentence "the cat chased the mouse." The object is 
"mouse", because it is the noun acted upon by the verb, "chased".  

Often a sentence contains phrases and clauses. Clauses are sub-
sections of the sentence with their own verb (action word), a phrase 
does not have a verb. For example. "I saw Mrs Brown drive her car 
along the motorway." This sentence contains the subject, "I" and the 
predicative "saw Mrs Brown drive her car along 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 
declares that 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 box fell off the kitchen table." First we 
can split it into the subject and predicate; 

Subject       Predicate
I believe     that the box fell off the kitchen table


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 Conjunction
DET  Determiner
N    Noun
NP   Noun Phrase
P    Preposition
S    Sentence
VP   Verb Phrase
V    Verb


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


---------
Listing 3
---------

/* PREDICATE PARSER */

/*---------------*/
/* Include files */
/*---------------*/
#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <ctype.h>

/*-----------*/
/* 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" };

/* 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;

/*---------------------*/
/* 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;
                    }
                    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,"");
        }
        else
        if(!ignore)
        {
            if (*object)
                strcat(object," ");
            strcat(object,p);
        }

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

        p = strtok(NULL," ,.;:?!");
    }
    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;
                }
            }
        }
        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," ,.;:?!");
    }
    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;
}

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;
            }

            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;
                        *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 = s
--- WILDMAIL!/WC v4.12 
 * Origin: Ooh! BBS, London [MULTILINES]  0181-395-3108  (2:254/233.0)