home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07031a < prev    next >
Text File  |  1991-05-10  |  6KB  |  169 lines

  1.  
  2. /******************************************
  3. *       PHRASE.H v1.0
  4. *       phrase package header
  5. *       Richard Rathe 8/90
  6. */
  7.  
  8. #ifndef PHRASE_H
  9.  
  10. #define PHRASE_H
  11.  
  12. #define EOS             '\0'    /* end of string character */
  13.  
  14. #define MAXCHAR         64      /* length of largest string     */
  15. #define MINCHAR         2       /* length of smallest string    */
  16. #define MAXWORD         3       /* length of largest phrase     */
  17. #define LISTMARK        "\r"    /* delimiter for phrase list */
  18.  
  19. extern void phrase(char *in,char *out,char *stop);
  20. extern char *getword(char *str,char *word,char *delim);
  21. extern void addword(char *str,char *word,char *stop,char delim);
  22. extern int      skipword(char *str,char *stop);
  23. extern char *getstopword(char *str,char *stopword);
  24. extern void strlwr(char *str);
  25.  
  26. #endif
  27.  
  28. /************************************************
  29. *       phrase.c v1.0
  30. *       chunk words into phrases
  31. *       THINK C v4.0
  32. *       Richard Rathe 8/90
  33. */
  34.  
  35. #include <string.h>             /* standard headers */
  36. #include <ctype.h>
  37.  
  38. #include "phrase.h"             /* for #defines and prototypes */
  39.  
  40. void phrase(char *in,char *out,char *stop)
  41. {
  42.         char flag;
  43.         char buf[MAXCHAR];
  44.         
  45.         *out = EOS;             /* just to be sure */
  46.  
  47.         while(*(in = getword(in,buf,&flag)) != EOS)     /* get a word */
  48.                 addword(out,buf,stop,flag);             /* add it */
  49.  
  50.         addword(out,buf,stop,flag);                     /* last word */
  51. }
  52.  
  53. char *getword(char *str,char *word,char *delim)
  54. {
  55.         while(isalnum(*str) || *str == '-' && *str != EOS)
  56.         {
  57.                 *word = *str;   /* load chars into word */
  58.                 word++;         /* advance pointers */
  59.                 str++;
  60.         }
  61.  
  62.         *delim = *str;          /* save first delimiter */
  63.  
  64.         while(!isalnum(*str) && *str != EOS)    /* eat extra chars */
  65.         {
  66.                 if(!isspace(*str))      /* if not white space */
  67.                         *delim = *str;  /* remember delimiter */
  68.                 str++;
  69.         }
  70.         
  71.         *word = EOS;            /* add EOS to word */
  72.         return str;             /* return current position */
  73. }
  74.  
  75. void addword(char *str,char *word,char *stop,char delim)
  76. {
  77.         int i,j;
  78.         static char word_array[MAXWORD][MAXCHAR];
  79.         
  80.         for(i = 0;i < MAXWORD - 1;i++)                  /* age array */
  81.                 strcpy(word_array[i],word_array[i + 1]);
  82.  
  83.         strlwr(word);                                   /* strip caps */
  84.  
  85.         strcpy(word_array[MAXWORD - 1],word);           /* add word */
  86.  
  87.         for(i = 0;i < MAXWORD;i++)              /* for each array position */
  88.         {
  89.                 if(!word_array[i][0])           /* empty position */
  90.                         continue;               /* continue */
  91.                         
  92.                 if(skipword(word_array[i],stop))        /* stopword first */
  93.                         continue;                       /* continue */
  94.  
  95.                 if(skipword(word_array[MAXWORD - 1],stop)) /* stopword last */
  96.                         continue;                               /* continue */
  97.  
  98.                 strcat(str,word_array[i]);      /* add array position */
  99.  
  100.                 for(j = i + 1;j < MAXWORD;j++)  /* add subsequent positions */
  101.                 {
  102.                         strcat(str," ");
  103.                         strcat(str,word_array[j]);
  104.                 }
  105.  
  106.                 strcat(str,LISTMARK);           /* add list delimiter */
  107.         }
  108.                 
  109.         if(!isspace(delim))                     /* if delimiter not space */
  110.         {
  111.                 for(i = 0;i < MAXWORD;i++)      /* clear array */
  112.                 {
  113.                         word_array[i][0] = EOS;
  114.                 }
  115.         }
  116. }
  117.  
  118. int skipword(char *word,char *stoplist)
  119. {
  120.         int i,j,x;
  121.         char stopword[MAXCHAR];
  122.  
  123.         if(strlen(word) < MINCHAR)      /* if short word */
  124.                         return 1;       /* skip (true) */
  125.  
  126.         if(isdigit(*word))              /* if leading digit */
  127.                         return 1;       /* skip (true) */
  128.  
  129.                                         /* get a stopword */
  130.         while(*(stoplist = getstopword(stoplist,stopword)) != EOS)
  131.         {
  132.                 if((x = strcmp(word,stopword)) == 0)    /* word in stoplist */
  133.                         return 1;                       /* skip (true) */
  134.         
  135.                 if(x < 0)               /* if str > list */
  136.                         break;          /* we're done */
  137.         }
  138.  
  139.                                         /* last stop word */
  140.         if(*stoplist == EOS && strcmp(word,stopword) == 0)
  141.                 return 1;               /* skip (true) */
  142.         
  143.         return 0;                       /* otherwise add (false) */
  144. }
  145.  
  146. char *getstopword(char *str,char *stopword)
  147. {
  148.                                         /* step over commas and spaces */
  149.         while(isspace(*str) || *str == ',' && *str != EOS)
  150.                 str++;                                                                  
  151.  
  152.         while(*str != ',' && *str != EOS)
  153.         {
  154.                 *stopword = *str;       /* load chars into stopword */
  155.                 stopword++;             /* advance pointers */
  156.                 str++;
  157.         }
  158.  
  159.         *stopword = EOS;                /* add EOS to word */
  160.         return str;                     /* return current position */
  161. }
  162.  
  163. void strlwr(char *str)
  164. {
  165.         for(;*str != EOS;str++)         /* convert string to lower case */
  166.                 *str = tolower(*str);
  167. }
  168.  
  169.