home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / stemmer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  18.8 KB  |  554 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    francois@welchgate.welch.jhu.edu
  6. */
  7.  
  8. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  9.  
  10.  
  11.  
  12. /* 
  13.  * stems a word.
  14.  * 
  15.  */
  16.  
  17. /* the main functions are:
  18.  *   stemmer
  19.  *
  20.  */
  21.  
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include "panic.h"  
  26. #include "cutil.h"
  27. #include "futil.h"
  28. #include "stemmer.h"
  29.  
  30.  
  31.  
  32.  
  33. /* list words and their stemmed version in the log*/
  34. /* #define LIST_STEMS */
  35.  
  36.  
  37.  
  38.  
  39. char *
  40. stemmer (word)
  41. char *word;
  42. {
  43.  
  44. #ifdef LIST_STEMS
  45. /*
  46. ** - duplicate our word so that 
  47. **   we can provide a better listing
  48. */
  49.     char *word_copy;
  50.     
  51.     word_copy = s_malloc(strlen(word+5));
  52.     
  53.     strcpy(word_copy,word);
  54. #endif
  55.  
  56.  
  57. #ifdef STEM_WORDS
  58. Stem(word);
  59. #endif
  60. #ifdef LIST_STEMS
  61. /*
  62. ** - put up the original word and the stemmed version
  63. */
  64. waislog(WLOG_LOW, WLOG_INFO,"word: %s,  stemmed word: %s", word_copy, word); 
  65. s_free(word_copy);
  66. #endif
  67.  
  68. return(word);
  69.  
  70. }
  71.  
  72.  
  73. /*******************************   stem.c   ***********************************
  74.  
  75.    Purpose:    Implementation of the Porter stemming algorithm documented 
  76.                in: Porter, M.F., "An Algorithm For Suffix Stripping," 
  77.                Program 14 (3), July 1980, pp. 130-137.
  78.  
  79.    Provenance: Written by B. Frakes and C. Cox, 1986.
  80.                Changed by C. Fox, 1990.
  81.                   - made measure function a DFA
  82.                   - restructured structs
  83.                   - renamed functions and variables
  84.                   - restricted function and variable scopes
  85.                Changed by C. Fox, July, 1991.
  86.                   - added ANSI C declarations 
  87.                   - branch tested to 90% coverage
  88.  
  89.    Notes:      This code will make little sense without the the Porter
  90.                article.  The stemming function converts its input to
  91.                lower case.
  92. **/
  93.  
  94. /************************   Standard Include Files   *************************/
  95.  
  96. #include <stdio.h>
  97.  
  98. /*****************************************************************************/
  99. /*****************   Private Defines and Data Structures   *******************/
  100.  
  101. #define EOS                         '\0'
  102.  
  103. #define IsVowel(c)        ('a'==(c)||'e'==(c)||'i'==(c)||'o'==(c)||'u'==(c))
  104.  
  105. typedef struct {
  106.            int id;                 /* returned if rule fired */
  107.            char *old_end;          /* suffix replaced */
  108.            char *new_end;          /* suffix replacement */
  109.            int old_offset;         /* from end of word to start of suffix */
  110.            int new_offset;         /* from beginning to end of new suffix */
  111.            int min_root_size;      /* min root word size for replacement */
  112.            int (*condition)();     /* the replacement test function */
  113.            } RuleList;
  114.  
  115. static char LAMBDA[1] = "";        /* the constant empty string */
  116. static char *end;                  /* pointer to the end of the word */
  117.  
  118. /*****************************************************************************/
  119. /********************   Private Function Declarations   **********************/
  120.  
  121. #ifdef __STDC__
  122.  
  123. static int WordSize( char *word );
  124. static int ContainsVowel( char *word );
  125. static int EndsWithCVC( char *word );
  126. static int AddAnE( char *word );
  127. static int RemoveAnE( char *word );
  128. static int ReplaceEnd( char *word, RuleList *rule );
  129.  
  130. #else
  131.  
  132. static int WordSize( /* word */ );
  133. static int ContainsVowel( /* word */ );
  134. static int EndsWithCVC( /* word */ );
  135. static int AddAnE( /* word */ );
  136. static int RemoveAnE( /* word */ );
  137. static int ReplaceEnd( /* word, rule */ );
  138.  
  139. #endif
  140.  
  141. /******************************************************************************/
  142. /*****************   Initialized Private Data Structures   ********************/
  143.  
  144. static RuleList step1a_rules[] =
  145.            {
  146.              101,  "sses",      "ss",    3,  1, -1,  NULL,
  147.              102,  "ies",       "i",     2,  0, -1,  NULL,
  148.              103,  "ss",        "ss",    1,  1, -1,  NULL,
  149.              104,  "s",         LAMBDA,  0, -1, -1,  NULL,
  150.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  151.            };
  152.  
  153. static RuleList step1b_rules[] =
  154.            {
  155.              105,  "eed",       "ee",    2,  1,  0,  NULL,
  156.              106,  "ed",        LAMBDA,  1, -1, -1,  ContainsVowel,
  157.              107,  "ing",       LAMBDA,  2, -1, -1,  ContainsVowel,
  158.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  159.            };
  160.  
  161. static RuleList step1b1_rules[] =
  162.            {
  163.              108,  "at",        "ate",   1,  2, -1,  NULL,
  164.              109,  "bl",        "ble",   1,  2, -1,  NULL,
  165.              110,  "iz",        "ize",   1,  2, -1,  NULL,
  166.              111,  "bb",        "b",     1,  0, -1,  NULL,
  167.              112,  "dd",        "d",     1,  0, -1,  NULL,
  168.              113,  "ff",        "f",     1,  0, -1,  NULL,
  169.              114,  "gg",        "g",     1,  0, -1,  NULL,
  170.              115,  "mm",        "m",     1,  0, -1,  NULL,
  171.              116,  "nn",        "n",     1,  0, -1,  NULL,
  172.              117,  "pp",        "p",     1,  0, -1,  NULL,
  173.              118,  "rr",        "r",     1,  0, -1,  NULL,
  174.              119,  "tt",        "t",     1,  0, -1,  NULL,
  175.              120,  "ww",        "w",     1,  0, -1,  NULL,
  176.              121,  "xx",        "x",     1,  0, -1,  NULL,
  177.              122,  LAMBDA,      "e",    -1,  0, -1,  AddAnE,
  178.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  179.              };
  180.  
  181. static RuleList step1c_rules[] =
  182.            {
  183.              123,  "y",         "i",      0,  0, -1,  ContainsVowel,
  184.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  185.            };
  186.  
  187. static RuleList step2_rules[] =
  188.            {
  189.              203,  "ational",   "ate",   6,  2,  0,  NULL,
  190.              204,  "tional",    "tion",  5,  3,  0,  NULL,
  191.              205,  "enci",      "ence",  3,  3,  0,  NULL,
  192.              206,  "anci",      "ance",  3,  3,  0,  NULL,
  193.              207,  "izer",      "ize",   3,  2,  0,  NULL,
  194.              208,  "abli",      "able",  3,  3,  0,  NULL,
  195.              209,  "alli",      "al",    3,  1,  0,  NULL,
  196.              210,  "entli",     "ent",   4,  2,  0,  NULL,
  197.              211,  "eli",       "e",     2,  0,  0,  NULL,
  198.              213,  "ousli",     "ous",   4,  2,  0,  NULL,
  199.              214,  "ization",   "ize",   6,  2,  0,  NULL,
  200.              215,  "ation",     "ate",   4,  2,  0,  NULL,
  201.              216,  "ator",      "ate",   3,  2,  0,  NULL,
  202.              217,  "alism",     "al",    4,  1,  0,  NULL,
  203.              218,  "iveness",   "ive",   6,  2,  0,  NULL,
  204.              219,  "fulnes",    "ful",   5,  2,  0,  NULL,
  205.              220,  "ousness",   "ous",   6,  2,  0,  NULL,
  206.              221,  "aliti",     "al",    4,  1,  0,  NULL,
  207.              222,  "iviti",     "ive",   4,  2,  0,  NULL,
  208.              223,  "biliti",    "ble",   5,  2,  0,  NULL,
  209.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  210.            };
  211.  
  212. static RuleList step3_rules[] =
  213.            {
  214.              301,  "icate",     "ic",    4,  1,  0,  NULL,
  215.              302,  "ative",     LAMBDA,  4, -1,  0,  NULL,
  216.              303,  "alize",     "al",    4,  1,  0,  NULL,
  217.              304,  "iciti",     "ic",    4,  1,  0,  NULL,
  218.              305,  "ical",      "ic",    3,  1,  0,  NULL,
  219.              308,  "ful",       LAMBDA,  2, -1,  0,  NULL,
  220.              309,  "ness",      LAMBDA,  3, -1,  0,  NULL,
  221.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  222.            };
  223.  
  224. static RuleList step4_rules[] =
  225.            {
  226.              401,  "al",        LAMBDA,  1, -1,  1,  NULL,
  227.              402,  "ance",      LAMBDA,  3, -1,  1,  NULL,
  228.              403,  "ence",      LAMBDA,  3, -1,  1,  NULL,
  229.              405,  "er",        LAMBDA,  1, -1,  1,  NULL,
  230.              406,  "ic",        LAMBDA,  1, -1,  1,  NULL,
  231.              407,  "able",      LAMBDA,  3, -1,  1,  NULL,
  232.              408,  "ible",      LAMBDA,  3, -1,  1,  NULL,
  233.              409,  "ant",       LAMBDA,  2, -1,  1,  NULL,
  234.              410,  "ement",     LAMBDA,  4, -1,  1,  NULL,
  235.              411,  "ment",      LAMBDA,  3, -1,  1,  NULL,
  236.              412,  "ent",       LAMBDA,  2, -1,  1,  NULL,
  237.              423,  "sion",      "s",     3,  0,  1,  NULL,
  238.              424,  "tion",      "t",     3,  0,  1,  NULL,
  239.              415,  "ou",        LAMBDA,  1, -1,  1,  NULL,
  240.              416,  "ism",       LAMBDA,  2, -1,  1,  NULL,
  241.              417,  "ate",       LAMBDA,  2, -1,  1,  NULL,
  242.              418,  "iti",       LAMBDA,  2, -1,  1,  NULL,
  243.              419,  "ous",       LAMBDA,  2, -1,  1,  NULL,
  244.              420,  "ive",       LAMBDA,  2, -1,  1,  NULL,
  245.              421,  "ize",       LAMBDA,  2, -1,  1,  NULL,
  246.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  247.            };
  248.  
  249. static RuleList step5a_rules[] =
  250.            {
  251.              501,  "e",         LAMBDA,  0, -1,  1,  NULL,
  252.              502,  "e",         LAMBDA,  0, -1, -1,  RemoveAnE,
  253.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  254.            };
  255.  
  256. static RuleList step5b_rules[] =
  257.            {
  258.              503,  "ll",        "l",     1,  0,  1,  NULL,
  259.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  260.            };
  261.  
  262. /*****************************************************************************/
  263. /********************   Private Function Declarations   **********************/
  264.  
  265. /*FN***************************************************************************
  266.  
  267.        WordSize( word )
  268.  
  269.    Returns: int -- a weird count of word size in adjusted syllables
  270.  
  271.    Purpose: Count syllables in a special way:  count the number 
  272.             vowel-consonant pairs in a word, disregarding initial 
  273.             consonants and final vowels.  The letter "y" counts as a
  274.             consonant at the beginning of a word and when it has a vowel
  275.             in front of it; otherwise (when it follows a consonant) it
  276.             is treated as a vowel.  For example, the WordSize of "cat" 
  277.             is 1, of "any" is 1, of "amount" is 2, of "anything" is 3.
  278.  
  279.    Plan:    Run a DFA to compute the word size
  280.  
  281.    Notes:   The easiest and fastest way to compute this funny measure is
  282.             with a finite state machine.  The initial state 0 checks
  283.             the first letter.  If it is a vowel, then the machine changes
  284.             to state 1, which is the "last letter was a vowel" state.
  285.             If the first letter is a consonant or y, then it changes
  286.             to state 2, the "last letter was a consonant state".  In
  287.             state 1, a y is treated as a consonant (since it follows
  288.             a vowel), but in state 2, y is treated as a vowel (since
  289.             it follows a consonant.  The result counter is incremented
  290.             on the transition from state 1 to state 2, since this
  291.             transition only occurs after a vowel-consonant pair, which
  292.             is what we are counting.
  293. **/
  294.  
  295. static int
  296. WordSize( word )
  297.    char *word;   /* in: word having its WordSize taken */
  298.    {
  299.    register int result;   /* WordSize of the word */
  300.    register int state;    /* current state in machine */
  301.  
  302.    result = 0;
  303.    state = 0;
  304.  
  305.                  /* Run a DFA to compute the word size */
  306.    while ( EOS != *word )
  307.       {
  308.       switch ( state )
  309.          {
  310.          case 0: state = (IsVowel(*word)) ? 1 : 2;
  311.                  break;
  312.          case 1: state = (IsVowel(*word)) ? 1 : 2;
  313.                  if ( 2 == state ) result++;
  314.                  break;
  315.          case 2: state = (IsVowel(*word) || ('y' == *word)) ? 1 : 2;
  316.                  break;
  317.          }
  318.       word++;
  319.       }
  320.  
  321.    return( result );
  322.  
  323.    } /* WordSize */
  324.  
  325. /*FN**************************************************************************
  326.  
  327.        ContainsVowel( word )
  328.  
  329.    Returns: int -- TRUE (1) if the word parameter contains a vowel,
  330.             FALSE (0) otherwise.
  331.  
  332.    Purpose: Some of the rewrite rules apply only to a root containing
  333.             a vowel, where a vowel is one of "aeiou" or y with a
  334.             consonant in front of it.
  335.  
  336.    Plan:    Obviously, under the definition of a vowel, a word contains
  337.             a vowel iff either its first letter is one of "aeiou", or
  338.             any of its other letters are "aeiouy".  The plan is to
  339.             test this condition.
  340.  
  341.    Notes:   None
  342. **/
  343.  
  344. static int
  345. ContainsVowel( word )
  346.    char *word;   /* in: buffer with word checked */
  347.    {
  348.  
  349.    if ( EOS == *word )
  350.       return( FALSE );
  351.    else
  352.       return( IsVowel(*word) || (NULL != strpbrk(word+1,"aeiouy")) );
  353.  
  354.  
  355.    } /* ContainsVowel */
  356.  
  357. /*FN**************************************************************************
  358.  
  359.        EndsWithCVC( word )
  360.  
  361.    Returns: int -- TRUE (1) if the current word ends with a
  362.             consonant-vowel-consonant combination, and the second
  363.             consonant is not w, x, or y, FALSE (0) otherwise.
  364.  
  365.    Purpose: Some of the rewrite rules apply only to a root with
  366.             this characteristic.
  367.  
  368.    Plan:    Look at the last three characters.
  369.  
  370.    Notes:   None
  371. **/
  372.  
  373. static int
  374. EndsWithCVC( word )
  375.    char *word;   /* in: buffer with the word checked */
  376.    {
  377.    int length;         /* for finding the last three characters */
  378.  
  379.    if ( (length = strlen(word)) < 2 )
  380.       return( FALSE );
  381.    else
  382.       {
  383.       end = word + length - 1;
  384.       return(    (NULL == strchr("aeiouwxy",*end--))      /* consonant */
  385.               && (NULL != strchr("aeiouy",  *end--))        /* vowel */
  386.               && (NULL == strchr("aeiou",   *end  )) );   /* consonant */
  387.       }
  388.  
  389.    } /* EndsWithCVC */
  390.  
  391. /*FN**************************************************************************
  392.  
  393.        AddAnE( word )
  394.  
  395.    Returns: int -- TRUE (1) if the current word meets special conditions
  396.             for adding an e.
  397.  
  398.    Purpose: Rule 122 applies only to a root with this characteristic.
  399.  
  400.    Plan:    Check for size of 1 and a consonant-vowel-consonant ending.
  401.  
  402.    Notes:   None
  403. **/
  404.  
  405. static int
  406. AddAnE( word )
  407.    char *word;
  408.    {
  409.  
  410.    return( (1 == WordSize(word)) && EndsWithCVC(word) );
  411.  
  412.    } /* AddAnE */
  413.  
  414. /*FN**************************************************************************
  415.  
  416.        RemoveAnE( word )
  417.  
  418.    Returns: int -- TRUE (1) if the current word meets special conditions
  419.             for removing an e.
  420.  
  421.    Purpose: Rule 502 applies only to a root with this characteristic.
  422.  
  423.    Plan:    Check for size of 1 and no consonant-vowel-consonant ending.
  424.  
  425.    Notes:   None
  426. **/
  427.  
  428. static int
  429. RemoveAnE( word )
  430.    char *word;
  431.    {
  432.  
  433.    return( (1 == WordSize(word)) && !EndsWithCVC(word) );
  434.  
  435.    } /* RemoveAnE */
  436.  
  437. /*FN**************************************************************************
  438.  
  439.        ReplaceEnd( word, rule )
  440.  
  441.    Returns: int -- the id for the rule fired, 0 is none is fired
  442.  
  443.    Purpose: Apply a set of rules to replace the suffix of a word
  444.  
  445.    Plan:    Loop through the rule set until a match meeting all conditions
  446.             is found.  If a rule fires, return its id, otherwise return 0.
  447.             Connditions on the length of the root are checked as part of this
  448.             function's processing because this check is so often made.
  449.  
  450.    Notes:   This is the main routine driving the stemmer.  It goes through
  451.             a set of suffix replacement rules looking for a match on the
  452.             current suffix.  When it finds one, if the root of the word
  453.             is long enough, and it meets whatever other conditions are
  454.             required, then the suffix is replaced, and the function returns.
  455. **/
  456.  
  457. static int
  458. ReplaceEnd( word, rule )
  459.    char *word;        /* in/out: buffer with the stemmed word */
  460.    RuleList *rule;    /* in: data structure with replacement rules */
  461.    {
  462.    register char *ending;   /* set to start of possible stemmed suffix */
  463.    char tmp_ch;             /* save replaced character when testing */
  464.  
  465.    while ( 0 != rule->id )
  466.       {
  467.       ending = end - rule->old_offset;
  468.       if ( word <= ending )
  469.          if ( 0 == strcmp(ending,rule->old_end) )
  470.             {
  471.             tmp_ch = *ending;
  472.             *ending = EOS;
  473.             if ( rule->min_root_size < WordSize(word) )
  474.                if ( !rule->condition || (*rule->condition)(word) )
  475.                   {
  476.                   (void)strcat( word, rule->new_end );
  477.                   end = ending + rule->new_offset;
  478.                   break;
  479.                   }
  480.             *ending = tmp_ch;
  481.             }
  482.       rule++;
  483.       }
  484.  
  485.    return( rule->id );
  486.  
  487.    } /* ReplaceEnd */
  488.  
  489. /*****************************************************************************/
  490. /*********************   Public Function Declarations   **********************/
  491.  
  492. /*FN***************************************************************************
  493.  
  494.        Stem( word )
  495.  
  496.    Returns: int -- FALSE (0) if the word contains non-alphabetic characters
  497.             and hence is not stemmed, TRUE (1) otherwise
  498.  
  499.    Purpose: Stem a word
  500.  
  501.    Plan:    Part 1: Check to ensure the word is all alphabetic
  502.             Part 2: Run through the Porter algorithm
  503.             Part 3: Return an indication of successful stemming
  504.  
  505.    Notes:   This function implements the Porter stemming algorithm, with
  506.             a few additions here and there.  See:
  507.  
  508.                Porter, M.F., "An Algorithm For Suffix Stripping,"
  509.                Program 14 (3), July 1980, pp. 130-137.
  510.  
  511.             Porter's algorithm is an ad hoc set of rewrite rules with
  512.             various conditions on rule firing.  The terminology of
  513.             "step 1a" and so on, is taken directly from Porter's
  514.             article, which unfortunately gives almost no justification
  515.             for the various steps.  Thus this function more or less
  516.             faithfully refects the opaque presentation in the article.
  517.             Changes from the article amount to a few additions to the
  518.             rewrite rules;  these are marked in the RuleList data
  519.             structures with comments.
  520. **/
  521.  
  522. int
  523. Stem( word )
  524.    char *word;  /* in/out: the word stemmed */
  525.    {
  526.    int rule;    /* which rule is fired in replacing an end */
  527.  
  528.             /* Part 1: Check to ensure the word is all alphabetic */
  529.    for ( end = word; *end != EOS; end++ )
  530.       if ( !isalpha(*end) ) return( FALSE );
  531.       else *end = tolower( *end );
  532.    end--;
  533.  
  534.                 /*  Part 2: Run through the Porter algorithm */
  535.    (void)ReplaceEnd( word, step1a_rules );
  536.    rule = ReplaceEnd( word, step1b_rules );
  537.    if ( (106 == rule) || (107 == rule) )
  538.       (void)ReplaceEnd( word, step1b1_rules );
  539.    (void)ReplaceEnd( word, step1c_rules );
  540.  
  541.    (void)ReplaceEnd( word, step2_rules );
  542.  
  543.    (void)ReplaceEnd( word, step3_rules );
  544.  
  545.    (void)ReplaceEnd( word, step4_rules );
  546.  
  547.    (void)ReplaceEnd( word, step5a_rules );
  548.    (void)ReplaceEnd( word, step5b_rules );
  549.  
  550.            /* Part 3: Return an indication of successful stemming */
  551.    return( TRUE );
  552.  
  553.    } /* Stem */
  554.