The Porter Stemming Algorithm

对英文单词进行复数和过去分词的处理,原理好多~ 书上的 ,懒得打了。。

贴上源码~~呵呵~~  开源万岁~

[ANSI C]

/* This is the Porter stemming algorithm, coded up in ANSI C by the
   author. It may be be regarded as cononical, in that it follows the
   algorithm presented in

   Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
   no. 3, pp 130-137,

   only differing from it at the points maked --DEPARTURE-- below.

   See also 
http://www.tartarus.org/~martin/PorterStemmer

   The algorithm as described in the paper could be exactly replicated
   by adjusting the points of DEPARTURE, but this is barely necessary,
   because (a) the points of DEPARTURE are definitely improvements, and
   (b) no encoding of the Porter stemmer I have seen is anything like
   as exact as this version, even with the points of DEPARTURE!

   You can compile it on Unix with 'gcc -O3 -o stem stem.c' after which
   'stem' takes a list of inputs and sends the stemmed equivalent to
   stdout.

   The algorithm as encoded here is particularly fast.

   Release 1
*/


#include 
< string .h >    /* for memmove */

#define  TRUE 1
#define  FALSE 0

/* The main part of the stemming algorithm starts here. b is a buffer
   holding a word to be stemmed. The letters are in b[k0], b[k0+1] ...
   ending at b[k]. In fact k0 = 0 in this demo program. k is readjusted
   downwards as the stemming progresses. Zero termination is not in fact
   used in the algorithm.

   Note that only lower case sequences are stemmed. Forcing to lower case
   should be done before stem(...) is called.
*/


static   char   *  b;        /* buffer for word to be stemmed */
static   int  k,k0,j;      /* j is a general offset into the string */

/* cons(i) is TRUE <=> b[i] is a consonant. */

static   int  cons( int  i)
{  switch (b[i])
   
{  case 'a'case 'e'case 'i'case 'o'case 'u'return FALSE;
      
case 'y'return (i==k0) ? TRUE : !cons(i-1);
      
defaultreturn TRUE;
   }

}


/* m() measures the number of consonant sequences between k0 and j. if c is
   a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
   presence,

      <c><v>       gives 0
      <c>vc<v>     gives 1
      <c>vcvc<v>   gives 2
      <c>vcvcvc<v> gives 3
      ....
*/


static   int  m()
{  int n = 0;
   
int i = k0;
   
while(TRUE)
   
{  if (i > j) return n;
      
if (! cons(i)) break; i++;
   }

   i
++;
   
while(TRUE)
   
{  while(TRUE)
      
{  if (i > j) return n;
            
if (cons(i)) break;
            i
++;
      }

      i
++;
      n
++;
      
while(TRUE)
      
{  if (i > j) return n;
         
if (! cons(i)) break;
         i
++;
      }

      i
++;
   }

}


/* vowelinstem() is TRUE <=> k0,...j contains a vowel */

static   int  vowelinstem()
{  int i; for (i = k0; i <= j; i++if (! cons(i)) return TRUE;
   
return FALSE;
}


/* doublec(j) is TRUE <=> j,(j-1) contain a double consonant. */

static   int  doublec( int  j)
{  if (j < k0+1return FALSE;
   
if (b[j] != b[j-1]) return FALSE;
   
return cons(j);
}


/* cvc(i) is TRUE <=> i-2,i-1,i has the form consonant - vowel - consonant
   and also if the second c is not w,x or y. this is used when trying to
   restore an e at the end of a short word. e.g.

      cav(e), lov(e), hop(e), crim(e), but
      snow, box, tray.

*/


static   int  cvc( int  i)
{  if (i < k0+2 || !cons(i) || cons(i-1|| !cons(i-2)) return FALSE;
   
{  int ch = b[i];
      
if (ch == 'w' || ch == 'x' || ch == 'y'return FALSE;
   }

   
return TRUE;
}


/* ends(s) is TRUE <=> k0,...k ends with the string s. */

static   int  ends( char   *  s)
{  int length = s[0];
   
if (s[length] != b[k]) return FALSE; /* tiny speed-up */
   
if (length > k-k0+1return FALSE;
   
if (memcmp(b+k-length+1,s+1,length) != 0return FALSE;
   j 
= k-length;
   
return TRUE;
}


/* setto(s) sets (j+1),...k to the characters in the string s, readjusting
   k. 
*/


static   void  setto( char   *  s)
{  int length = s[0];
   memmove(b
+j+1,s+1,length);
   k 
= j+length;
}


/* r(s) is used further down. */

static   void  r( char   *  s)  if (m() > 0) setto(s); }

/* step1ab() gets rid of plurals and -ed or -ing. e.g.

       caresses  ->  caress
       ponies    ->  poni
       ties      ->  ti
       caress    ->  caress
       cats      ->  cat

       feed      ->  feed
       agreed    ->  agree
       disabled  ->  disable

       matting   ->  mat
       mating    ->  mate
       meeting   ->  meet
       milling   ->  mill
       messing   ->  mess

       meetings  ->  meet

*/


static   void  step1ab()
{  if (b[k] == 's')
   
{  if (ends("" "sses")) k -= 2else
      
if (ends("" "ies")) setto("" "i"); else
      
if (b[k-1!= 's') k--;
   }

   
if (ends("" "eed")) if (m() > 0) k--; } else
   
if ((ends("" "ed"|| ends("" "ing")) && vowelinstem())
   
{  k = j;
      
if (ends("" "at")) setto("" "ate"); else
      
if (ends("" "bl")) setto("" "ble"); else
      
if (ends("" "iz")) setto("" "ize"); else
      
if (doublec(k))
      
{  k--;
         
{  int ch = b[k];
            
if (ch == 'l' || ch == 's' || ch == 'z') k++;
         }

      }

      
else if (m() == 1 && cvc(k)) setto("" "e");
   }

}


/* step1c() turns terminal y to i when there is another vowel in the stem. */

static   void  step1c()  if (ends("" "y"&& vowelinstem()) b[k] = 'i'; }


/* step2() maps double suffices to single ones. so -ization ( = -ize plus
   -ation) maps to -ize etc. note that the string before the suffix must give
   m() > 0. 
*/


static   void  step2()  switch (b[k-1])
{
    
case 'a'if (ends("" "ational")) { r("" "ate"); break; }
              
if (ends("" "tional")) { r("" "tion"); break; }
              
break;
    
case 'c'if (ends("" "enci")) { r("" "ence"); break; }
              
if (ends("" "anci")) { r("" "ance"); break; }
              
break;
    
case 'e'if (ends("" "izer")) { r("" "ize"); break; }
              
break;
    
case 'l'if (ends("" "bli")) { r("" "ble"); break; } /*-DEPARTURE-*/

 
/* To match the published algorithm, replace this line with
    case 'l': if (ends("" "abli")) { r("" "able"); break; } 
*/


              
if (ends("" "alli")) { r("" "al"); break; }
              
if (ends("" "entli")) { r("" "ent"); break; }
              
if (ends("" "eli")) { r("" "e"); break; }
              
if (ends("" "ousli")) { r("" "ous"); break; }
              
break;
    
case 'o'if (ends("" "ization")) { r("" "ize"); break; }
              
if (ends("" "ation")) { r("" "ate"); break; }
              
if (ends("" "ator")) { r("" "ate"); break; }
              
break;
    
case 's'if (ends("" "alism")) { r("" "al"); break; }
              
if (ends("" "iveness")) { r("" "ive"); break; }
              
if (ends("" "fulness")) { r("" "ful"); break; }
              
if (ends("" "ousness")) { r("" "ous"); break; }
              
break;
    
case 't'if (ends("" "aliti")) { r("" "al"); break; }
              
if (ends("" "iviti")) { r("" "ive"); break; }
              
if (ends("" "biliti")) { r("" "ble"); break; }
              
break;
    
case 'g'if (ends("" "logi")) { r("" "log"); break; } /*-DEPARTURE-*/

 
/* To match the published algorithm, delete this line */

}
 }


/* step3() deals with -ic-, -full, -ness etc. similar strategy to step2. */

static   void  step3()  switch (b[k])
{
    
case 'e'if (ends("" "icate")) { r("" "ic"); break; }
              
if (ends("" "ative")) { r("

[Csharp ]

 

using  System;
using  System.IO;
using  System.Reflection;
using  System.Runtime.InteropServices;
using  System.Windows.Forms;

[assembly: AssemblyTitle(
"" )]
[assembly: AssemblyDescription(
" Porter stemmer in CSharp " )]
[assembly: AssemblyConfiguration(
"" )]
[assembly: AssemblyCompany(
"" )]
[assembly: AssemblyProduct(
"" )]
[assembly: AssemblyCopyright(
"" )]
[assembly: AssemblyTrademark(
"" )]
[assembly: AssemblyCulture(
"" )]        
[assembly: AssemblyVersion(
" 1.4 " )]
[assembly: AssemblyKeyFile(
" keyfile.snk " )]
[assembly: AssemblyDelaySign(
false )]
[assembly: AssemblyKeyName(
"" )]

namespace  PorterStemmerAlgorithm 
{

    
/*

       Porter stemmer in CSharp, based on the Java port. The original paper is in

           Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
           no. 3, pp 130-137,

       See also 
http://www.tartarus.org/~martin/PorterStemmer

       History:

       Release 1

       Bug 1 (reported by Gonzalo Parra 16/10/99) fixed as marked below.
       The words 'aed', 'eed', 'oed' leave k at 'a' for step 3, and b[k-1]
       is then out outside the bounds of b.

       Release 2

       Similarly,

       Bug 2 (reported by Steve Dyrdahl 22/2/00) fixed as marked below.
       'ion' by itself leaves j = -1 in the test for 'ion' in step 5, and
       b[j] is then outside the bounds of b.

       Release 3

       Considerably revised 4/9/00 in the light of many helpful suggestions
       from Brian Goetz of Quiotix Corporation (brian@quiotix.com).

       Release 4
       
       This revision allows the Porter Stemmer Algorithm to be exported via the
       .NET Framework. To facilate its use via .NET, the following commands need to be
       issued to the operating system to register the component so that it can be
       imported into .Net compatible languages, such as Delphi.NET, Visual Basic.NET,
       Visual C++.NET, etc. 
       
       1. Create a stong name:         
            sn -k Keyfile.snk  
       2. Compile the C# class, which creates an assembly PorterStemmerAlgorithm.dll
            csc /t:library PorterStemmerAlgorithm.cs
       3. Register the dll with the Windows Registry 
          and so expose the interface to COM Clients via the type library 
          ( PorterStemmerAlgorithm.tlb will be created)
            regasm /tlb PorterStemmerAlgorithm.dll
       4. Load the component in the Global Assembly Cache
            gacutil -i PorterStemmerAlgorithm.dll
        
       Note: You must have the .Net Studio installed.
       
       Once this process is performed you should be able to import the class 
       via the appropiate mechanism in the language that you are using.
       
       i.e in Delphi 7 .NET this is simply a matter of selecting: 
            Project | Import Type Libary
       And then selecting Porter stemmer in CSharp Version 1.4"!
       
       Cheers Leif
    
    
*/


    
/**
      * Stemmer, implementing the Porter Stemming Algorithm
      *
      * The Stemmer class transforms a word into its root form.  The input
      * word can be provided a character at time (by calling add()), or at once
      * by calling one of the various stem(something) methods.
      
*/


    
public interface StemmerInterface 
    
{
        
string stemTerm( string s );
    }




    
    [ClassInterface( ClassInterfaceType.None )]
    
public class PorterStemmer : StemmerInterface
    
{
        
private char[] b;
        
private int i,     /* offset into b */
            i_end, 
/* offset to end of stemmed word */
            j, k;
        
private static int INC = 200;
        
/* unit of size whereby b is increased */
        
        
public PorterStemmer() 
        
{
            b 
= new char[INC];
            i 
= 0;
            i_end 
= 0;
        }


        
/* Implementation of the .NET interface - added as part of realease 4 (Leif) */
        
public string stemTerm( string s )
        
{
            setTerm( s );
            stem();
            
return getTerm();
        }


        
/*
            SetTerm and GetTerm have been simply added to ease the 
            interface with other lanaguages. They replace the add functions 
            and toString function. This was done because the original functions stored
            all stemmed words (and each time a new woprd was added, the buffer would be
            re-copied each time, making it quite slow). Now, The class interface 
            that is provided simply accepts a term and returns its stem, 
            instead of storing all stemmed words.
            (Leif)
        
*/


        
        
        
void setTerm( string s)
        
{
            i 
= s.Length;
            
char[] new_b = new char[i];
            
for (int c = 0; c < i; c++)
            new_b[c] 
= s[c];

            b  
= new_b;        

        }


        
public string getTerm()
        
{
            
return new String(b, 0, i_end);
        }



        
/* Old interface to the class - left for posterity. However, it is not
         * used when accessing the class via .NET (Leif)
*/


        
/**
         * Add a character to the word being stemmed.  When you are finished
         * adding characters, you can call stem(void) to stem the word.
         
*/


        
public void add(char ch) 
        
{
            
if (i == b.Length) 
            
{
                
char[] new_b = new char[i+INC];
                
for (int c = 0; c < i; c++)
                    new_b[c] 
= b[c];
                b 
= new_b;
            }

            b[i
++= ch;
        }



        
/** Adds wLen characters to the word being stemmed contained in a portion
         * of a char[] array. This is like repeated calls of add(char ch), but
         * faster.
         
*/


        
public void add(char[] w, int wLen) 
        
{
            
if (i+wLen >= b.Length) 
            
{
                
char[] new_b = new char[i+wLen+INC];
                
for (int c = 0; c < i; c++)
                    new_b[c] 
= b[c];
                b 
= new_b;
            }

            
for (int c = 0; c < wLen; c++)
                b[i
++= w[c];
        }


        
/**
         * After a word has been stemmed, it can be retrieved by toString(),
         * or a reference to the internal buffer can be retrieved by getResultBuffer
         * and getResultLength (which is generally more efficient.)
         
*/

        
public override string ToString() 
        
{
            
return new String(b,0,i_end);
        }


        
/**
         * Returns the length of the word resulting from the stemming process.
         
*/

        
public int getResultLength() 
        
{
            
return i_end;
        }


        
/**
         * Returns a reference to a character buffer containing the results of
         * the stemming process.  You also need to consult getResultLength()
         * to determine the length of the result.
         
*/

        
public char[] getResultBuffer() 
        
{
            
return b;
        }


        
/* cons(i) is true <=> b[i] is a consonant. */
        
private bool cons(int i) 
        
{
            
switch (b[i]) 
            
{
                
case 'a'case 'e'case 'i'case 'o'case 'u'return false;
                
case 'y'return (i==0? true : !cons(i-1);
                
defaultreturn true;
            }

        }


        
/* m() measures the number of consonant sequences between 0 and j. if c is
           a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
           presence,

              <c><v>       gives 0
              <c>vc<v>     gives 1
              <c>vcvc<v>   gives 2
              <c>vcvcvc<v> gives 3
              ....
        
*/

        
private int m() 
        
{
            
int n = 0;
            
int i = 0;
            
while(true
            
{
                
if (i > j) return n;
                
if (! cons(i)) break; i++;
            }

            i
++;
            
while(true
            
{
                
while(true
                
{
                    
if (i > j) return n;
                    
if (cons(i)) break;
                    i
++;
                }

                i
++;
                n
++;
                
while(true
                
{
                    
if (i > j) return n;
                    
if (! cons(i)) break;
                    i
++;
                }

                i
++;
            }

        }


        
/* vowelinstem() is true <=> 0,...j contains a vowel */
        
private bool vowelinstem() 
        
{
            
int i;
            
for (i = 0; i <= j; i++)
                
if (! cons(i))
                    
return true;
            
return false;
        }


        
/* doublec(j) is true <=> j,(j-1) contain a double consonant. */
        
private bool doublec(int j) 
        
{
            
if (j < 1)
                
return false;
            
if (b[j] != b[j-1])
                
return false;
            
return cons(j);
        }


        
/* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
           and also if the second c is not w,x or y. this is used when trying to
           restore an e at the end of a short word. e.g.

              cav(e), lov(e), hop(e), crim(e), but
              snow, box, tray.

        
*/

        
private bool cvc(int i) 
        
{
            
if (i < 2 || !cons(i) || cons(i-1|| !cons(i-2))
                
return false;
            
int ch = b[i];
            
if (ch == 'w' || ch == 'x' || ch == 'y')
                
return false;
            
return true;
        }


        
private bool ends(String s) 
        
{
            
int l = s.Length;
            
int o = k-l+1;
            
if (o < 0)
                
return false;
            
char[] sc = s.ToCharArray();
            
for (int i = 0; i < l; i++)
                
if (b[o+i] != sc[i])
                    
return false;
            j 
= k-l;
            
return true;
        }


        
/* setto(s) sets (j+1),...k to the characters in the string s, readjusting
           k. 
*/

        
private void setto(String s) 
        
{
            
int l = s.Length;
            
int o = j+1;
            
char[] sc = s.ToCharArray();
            
for (int i = 0; i < l; i++)
                b[o
+i] = sc[i];
            k 
= j+l;
        }


        
/* r(s) is used further down. */
        
private void r(String s) 
        
{
            
if (m() > 0)
                setto(s);
        }


        
/* step1() gets rid of plurals and -ed or -ing. e.g.
               caresses  ->  caress
               ponies    ->  poni
               ties      ->  ti
               caress    ->  caress
               cats      ->  cat

               feed      ->  feed
               agreed    ->  agree
               disabled  ->  disable

               matting   ->  mat
               mating    ->  mate
               meeting   ->  meet
               milling   ->  mill
               messing   ->  mess

               meetings  ->  meet

        
*/


        
private void step1() 
        
{
            
if (b[k] == 's'
            
{
                
if (ends("sses"))
                    k 
-= 2;
                
else if (ends("ies"))
                    setto(
"i");
                
else if (b[k-1!= 's')
                    k
--;
            }

            
if (ends("eed")) 
            
{
                
if (m() > 0)
                    k
--;
            }
 
            
else if ((ends("ed"|| ends("ing")) && vowelinstem()) 
            
{
                k 
= j;
                
if (ends("at"))
                    setto(
"ate");
                
else if (ends("bl"))
                    setto(
"ble");
                
else if (ends("iz"))
                    setto(
"ize");
                
else if (doublec(k)) 
                
{
                    k
--;
                    
int ch = b[k];
                    
if (ch == 'l' || ch == 's' || ch == 'z')
                        k
++;
                }

                
else if (m() == 1 && cvc(k)) setto("e");
            }

        }


        
/* step2() turns terminal y to i when there is another vowel in the stem. */
        
private void step2() 
        
{
            
if (ends("y"&& vowelinstem())
                b[k] 
= 'i';
        }


        
/* step3() maps double suffices to single ones. so -ization ( = -ize plus
           -ation) maps to -ize etc. note that the string before the suffix must give
           m() > 0. 
*/

        
private void step3() 
        
{
            
if (k == 0)
                
return;
            
            
/* For Bug 1 */
            
switch (b[k-1]) 
            
{
                
case 'a':
                    
if (ends("ational")) { r("ate"); break; }
                    
if (ends("tional")) { r("tion"); break; }
                    
break;
                
case 'c':
                    
if (ends("enci")) { r("ence"); break; }
                    
if (ends("anci")) { r("ance"); break; }
                    
break;
                
case 'e':
                    
if (ends("izer")) { r("ize"); break; }
                    
break;
                
case 'l':
                    
if (ends("bli")) { r("ble"); break; }
                    
if (ends("alli")) { r("al"); break; }
                    
if (ends("entli")) { r("ent"); break; }
                    
if (ends("eli")) { r("e"); break; }
                    
if (ends("ousli")) { r("ous"); break; }
                    
break;
                
case 'o':
                    
if (ends("ization")) { r("ize"); break; }
                    
if (ends("ation")) { r("ate"); break; }
                    
if (ends("ator")) { r("ate"); break; }
                    
break;
                
case 's':
                    
if (ends("alism")) { r("al"); break; }
                    
if (ends("iveness")) { r("ive"); break; }
                    
if (ends("fulness")) { r("ful"); break; }
                    
if (ends("ousness")) { r("ous"); break; }
                    
break;
                
case 't':
                    
if (ends("aliti")) { r("al"); break; }
                    
if (ends("iviti")) { r("ive"); break; }
                    
if (ends("biliti")) { r("ble"); break; }
                    
break;
                
case 'g':
                    
if (ends("logi")) { r("log"); break; }
                    
break;
                
default :
                    
break;
            }

        }


        
/* step4() deals with -ic-, -full, -ness etc. similar strategy to step3. */
        
private void step4() 
        
{
            
switch (b[k]) 
            
{
                
case 'e':
                    
if (ends("icate")) { r("ic"); break; }
                    
if (ends("ative")) { r(""); break; }
                    
if (ends("alize")) { r("al"); break; }
                    
break;
                
case 'i':
                    
if (ends("iciti")) { r("ic"); break; }
                    
break;
                
case 'l':
                    
if (ends("ical")) { r("ic"); break; }
                    
if (ends("ful")) { r(""); break; }
                    
break;
                
case 's':
                    
if (ends("ness")) { r(""); break; }
                    
break;
            }

        }


        
/* step5() takes off -ant, -ence etc., in context <c>vcvc<v>. */
        
private void step5() 
        
{
            
if (k == 0)
                
return;

            
/* for Bug 1 */
            
switch ( b[k-1] ) 
            
{
                
case 'a':
                    
if (ends("al")) breakreturn;
                
case 'c':
                    
if (ends("ance")) break;
                    
if (ends("ence")) breakreturn;
                
case 'e':
                    
if (ends("er")) breakreturn;
                
case 'i':
                    
if (ends("ic")) breakreturn;
                
case 'l':
                    
if (ends("able")) break;
                    
if (ends("ible")) breakreturn;
                
case 'n':
                    
if (ends("ant")) break;
                    
if (ends("ement")) break;
                    
if (ends("ment")) break;
                    
/* element etc. not stripped before the m */
                    
if (ends("ent")) breakreturn;
                
case 'o':
                    
if (ends("ion"&& j >= 0 && (b[j] == 's' || b[j] == 't')) break;
                    
/* j >= 0 fixes Bug 2 */
                    
if (ends("ou")) breakreturn;
                    
/* takes care of -ous */
                
case 's':
                    
if (ends("ism")) breakreturn;
                
case 't':
                    
if (ends("ate")) break;
                    
if (ends("iti")) breakreturn;
                
case 'u':
                    
if (ends("ous")) breakreturn;
                
case 'v':
                    
if (ends("ive")) breakreturn;
                
case 'z':
                    
if (ends("ize")) breakreturn;
                
default:
                    
return;
            }

            
if (m() > 1)
                k 
= j;
        }


        
/* step6() removes a final -e if m() > 1. */
        
private void step6() 
        
{
            j 
= k;
            
            
if (b[k] == 'e'
            
{
                
int a = m();
                
if (a > 1 || a == 1 && !cvc(k-1))
                    k
--;
            }

            
if (b[k] == 'l' && doublec(k) && m() > 1)
                k
--;
        }


        
/** Stem the word placed into the Stemmer buffer through calls to add().
         * Returns true if the stemming process resulted in a word different
         * from the input.  You can retrieve the result with
         * getResultLength()/getResultBuffer() or toString().
         
*/

        
public void stem() 
        
{
            k 
= i - 1;
            
if (k > 1
            
{
                step1();
                step2();
                step3();
                step4();
                step5();
                step6();
            }

            i_end 
= k+1;
            i 
= 0;
        }



    }

}



 

[T-SQL]

 

/* Copyright (c)2006 , Keith Lubell All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */   /****** Object: Table [dbo].[tblPorterStemming] Script Date: 5/16/2006 3:52:41 PM ******/   CREATE   TABLE   [ dbo ] . [ tblPorterStemming ]  (  [ Step ]   [ int ]   NOT   NULL  ,  [ Ordering ]   [ int ]   NOT   NULL  ,  [ phrase1 ]   [ nvarchar ]  ( 15 ) COLLATE SQL_Latin1_General_CP1_CI_AS  NOT   NULL  ,  [ phrase2 ]   [ nvarchar ]  ( 15 ) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL  )  ON   [ PRIMARY ]   GO   --  INSERT THE DATA INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(1,0,'sses','ss') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(1,1,'ies','i') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(1,2,'ss','ss') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(1,3,'s','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,0,'ational','ate') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,1,'tional','tion') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,2,'enci','ence') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,3,'anci','ance') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,4,'izer','ize') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,5,'bli','ble') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,6,'alli','al') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,7,'entli','ent') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,8,'eli','e') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,9,'ousli','ous') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,10,'ization','ize') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,11,'ation','ate') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,12,'ator','ate') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,13,'alism','al') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,14,'iveness','ive') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,15,'fulness','ful') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,16,'ousness','ous') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,17,'aliti','al') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,18,'iviti','ive') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,19,'biliti','ble') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(2,20,'logi','log') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,0,'icate','ic') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,1,'ative','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,2,'alize','al') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,3,'iciti','ic') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,4,'ical','ic') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,5,'ful','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(3,6,'ness','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,0,'al','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,1,'ance','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,2,'ence','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,3,'er','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,4,'ic','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,5,'able','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,6,'ible','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,7,'ant','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,8,'ement','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,9,'ment','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,10,'ent','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,11,'ion','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,12,'ou','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,13,'ism','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,14,'ate','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,15,'iti','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,16,'ous','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,17,'ive','') INSERT INTO [tblPorterStemming]([Step], [Ordering], [phrase1], [phrase2]) VALUES(4,18,'ize','') GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterAlgorithm ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN DECLARE @Ret nvarchar(4000), @Temp nvarchar(4000) -- DO some initial cleanup SELECT @Ret = LOWER(ISNULL(RTRIM(LTRIM(@InWord)),N'')) -- only strings greater than 2 are stemmed IF LEN(@Ret) > 2 BEGIN SELECT @Ret = dbo.fnPorterStep1(@Ret) SELECT @Ret = dbo.fnPorterStep2(@Ret) SELECT @Ret = dbo.fnPorterStep3(@Ret) SELECT @Ret = dbo.fnPorterStep4(@Ret) SELECT @Ret = dbo.fnPorterStep5(@Ret) END --End of Porter's algorithm.........returning the word RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterCVCpattern ( @Word nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN --local variables DECLARE @Ret nvarchar(4000), @i int --checking each character to see if it is a consonent or a vowel. also inputs the information in const_vowel SELECT @i = 1, @Ret = N'' WHILE @i <= LEN(@Word) BEGIN IF CHARINDEX(SUBSTRING(@Word,@i,1), N'aeiou') > 0 BEGIN SELECT @Ret = @Ret + N'v' END -- if y is not the first character, only then check the previous character ELSE IF SUBSTRING(@Word,@i,1) = N'y' AND @i > 1 BEGIN --check to see if previous character is a consonent IF CHARINDEX(SUBSTRING(@Word,@i-1,1), N'aeiou') = 0 SELECT @Ret = @Ret + N'v' ELSE SELECT @Ret = @Ret + N'c' END Else BEGIN SELECT @Ret = @Ret + N'c' END SELECT @i = @i + 1 END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterContainsVowel ( @Word nvarchar(4000) ) RETURNS bit AS BEGIN --checking word to see if vowels are present DECLARE @pattern nvarchar(4000), @ret bit SET @ret = 0 IF LEN(@Word) > 0 BEGIN --find out the CVC pattern SELECT @pattern = dbo.fnPorterCVCpattern(@Word) --check to see if the return pattern contains a vowel IF CHARINDEX( N'v',@pattern) > 0 SELECT @ret = 1 END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterCountm ( @Word nvarchar(4000) ) RETURNS tinyint AS BEGIN --A consonant in a word is a letter other than A, E, I, O or U, and other --than Y preceded by a consonant. (The fact that the term `consonant' is --defined to some extent in terms of itself does not make it ambiguous.) So in --TOY the consonants are T and Y, and in SYZYGY they are S, Z and G. If a --letter is not a consonant it is a vowel. --declaring local variables DECLARE @pattern nvarchar(4000), @ret tinyint, @i int, @flag bit --initializing SELECT @ret = 0, @flag = 0, @i = 1 If Len(@Word) > 0 BEGIN --find out the CVC pattern SELECT @pattern = dbo.fnPorterCVCpattern(@Word) --counting the number of m's... WHILE @i <= LEN(@pattern) BEGIN IF SUBSTRING(@pattern,@i,1) = N'v' OR @flag = 1 BEGIN SELECT @flag = 1 IF SUBSTRING(@pattern,@i,1) = N'c' SELECT @ret = @ret + 1, @flag = 0 END SELECT @i = @i + 1 END END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterEndsCVC ( @Word nvarchar(4000) ) RETURNS bit AS BEGIN --*o - the stem ends cvc, where the second c is not W, X or Y (e.g. -WIL, -HOP). --declaring local variables DECLARE @pattern NVARCHAR(3), @ret bit SELECT @ret = 0 --'check to see if atleast 3 characters are present If LEN(@Word) >= 3 BEGIN -- find out the CVC pattern -- we need to check only the last three characters SELECT @pattern = RIGHT(dbo.fnPorterCVCpattern(@Word),3) -- check to see if the letters in str match the sequence cvc IF @pattern = N'cvc' AND CHARINDEX(RIGHT(@Word,1), N'wxy') = 0 SELECT @ret = 1 END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterEndsDoubleCVC ( @Word nvarchar(4000) ) RETURNS bit AS BEGIN --*o - the stem ends cvc, where the second c is not W, X or Y (e.g. -WIL, -HOP). --declaring local variables DECLARE @pattern NVARCHAR(3), @ret bit SET @ret = 0 --check to see if atleast 3 characters are present IF Len(@Word) >= 3 BEGIN -- find out the CVC pattern -- we need to check only the last three characters SELECT @pattern = RIGHT(dbo.fnPorterCVCpattern(@Word),3) -- check to see if the letters in str match the sequence cvc IF @pattern = N'cvc' AND CHARINDEX(RIGHT(@Word,1), N'wxy') = 0 SELECT @ret = 1 END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterEndsDoubleConsonant ( @Word nvarchar(4000) ) RETURNS bit AS BEGIN --checking whether word ends with a double consonant (e.g. -TT, -SS). --declaring local variables DECLARE @holds_ends NVARCHAR(2), @ret bit, @hold_third_last NCHAR(1) SET @ret = 0 --first check whether the size of the word is >= 2 If Len(@Word) >= 2 BEGIN -- extract 2 characters from right of str SELECT @holds_ends = Right(@Word, 2) -- checking if both the characters are same and not double vowel IF SUBSTRING(@holds_ends,1,1) = SUBSTRING(@holds_ends,2,1) AND CHARINDEX(@holds_ends, N'aaeeiioouu') = 0 BEGIN --if the second last character is y, and there are atleast three letters in str If @holds_ends = N'yy' AND Len(@Word) > 2 BEGIN -- extracting the third last character SELECT @hold_third_last = LEFT(Right(@Word, 3),1) IF CHARINDEX(@hold_third_last, N'aaeeiioouu') > 0 SET @ret = 1 END ELSE SET @ret = 1 END END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterStep1 ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN DECLARE @Ret nvarchar(4000) DECLARE @Phrase1 NVARCHAR(15), @Phrase2 NVARCHAR(15) DECLARE @CursorName CURSOR -- DO some initial cleanup SELECT @Ret = @InWord /*STEP 1A SSES -> SS caresses -> caress IES -> I ponies -> poni ties -> ti SS -> SS caress -> caress S -> cats -> cat */ -- Create Cursor for Porter Step 1 SET @CursorName = CURSOR FOR SELECT phrase1, phrase2 FROM tblPorterStemming WHERE Step = 1 AND RIGHT(@Ret ,LEN(Phrase1)) = Phrase1 ORDER BY Ordering OPEN @CursorName -- Do Step 1 FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 WHILE @@FETCH_STATUS = 0 BEGIN --IF RIGHT(@Ret ,LEN(@Phrase1)) = @Phrase1 BEGIN SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) + @Phrase2 BREAK END FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 END -- Free Resources CLOSE @CursorName DEALLOCATE @CursorName --STEP 1B -- -- If -- (m>0) EED -> EE feed -> feed -- agreed -> agree -- Else -- (*v*) ED -> plastered -> plaster -- bled -> bled -- (*v*) ING -> motoring -> motor -- sing -> sing -- --If the second or third of the rules in Step 1b is successful, the following --is done: -- -- AT -> ATE conflat(ed) -> conflate -- BL -> BLE troubl(ed) -> trouble -- IZ -> IZE siz(ed) -> size -- (*d and not (*L or *S or *Z)) -- -> single letter -- hopp(ing) -> hop -- tann(ed) -> tan -- fall(ing) -> fall -- hiss(ing) -> hiss -- fizz(ed) -> fizz -- (m=1 and *o) -> E fail(ing) -> fail -- fil(ing) -> file -- --The rule to map to a single letter causes the removal of one of the double --letter pair. The -E is put back on -AT, -BL and -IZ, so that the suffixes ---ATE, -BLE and -IZE can be recognised later. This E may be removed in step --4. --declaring local variables DECLARE @m tinyint, @Temp nvarchar(4000),@second_third_success bit --initializing SELECT @second_third_success = 0 --(m>0) EED -> EE..else..(*v*) ED ->(*v*) ING -> IF RIGHT(@Ret ,LEN(N'eed')) = N'eed' BEGIN --counting the number of m--s SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(N'eed')) SELECT @m = dbo.fnPorterCountm(@temp) If @m > 0 SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(N'eed')) + N'ee' END ELSE IF RIGHT(@Ret ,LEN(N'ed')) = N'ed' BEGIN --trim and check for vowel SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(N'ed')) If dbo.fnPorterContainsVowel(@temp) = 1 SELECT @ret = LEFT(@Ret, LEN(@Ret) - LEN(N'ed')), @second_third_success = 1 END ELSE IF RIGHT(@Ret ,LEN(N'ing')) = N'ing' BEGIN --trim and check for vowel SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(N'ing')) If dbo.fnPorterContainsVowel(@temp) = 1 SELECT @ret = LEFT(@Ret, LEN(@Ret) - LEN(N'ing')), @second_third_success = 1 END --If the second or third of the rules in Step 1b is SUCCESSFUL, the following --is done: -- -- AT -> ATE conflat(ed) -> conflate -- BL -> BLE troubl(ed) -> trouble -- IZ -> IZE siz(ed) -> size -- (*d and not (*L or *S or *Z)) -- -> single letter -- hopp(ing) -> hop -- tann(ed) -> tan -- fall(ing) -> fall -- hiss(ing) -> hiss -- fizz(ed) -> fizz -- (m=1 and *o) -> E fail(ing) -> fail -- fil(ing) -> file IF @second_third_success = 1 --If the second or third of the rules in Step 1b is SUCCESSFUL BEGIN IF RIGHT(@Ret ,LEN(N'at')) = N'at' --AT -> ATE SELECT @ret = LEFT(@Ret, LEN(@Ret) - LEN(N'at')) + N'ate' ELSE IF RIGHT(@Ret ,LEN(N'bl')) = N'bl' --BL -> BLE SELECT @ret = LEFT(@Ret, LEN(@Ret) - LEN(N'bl')) + N'ble' ELSE IF RIGHT(@Ret ,LEN(N'iz')) = N'iz' --IZ -> IZE SELECT @ret = LEFT(@Ret, LEN(@Ret) - LEN(N'iz')) + N'ize' ELSE IF dbo.fnPorterEndsDoubleConsonant(@Ret) = 1 /*(*d and not (*L or *S or *Z))-> single letter*/ BEGIN IF CHARINDEX(RIGHT(@Ret,1), N'lsz') = 0 SELECT @ret = LEFT(@Ret, LEN(@Ret) - 1) END ELSE IF dbo.fnPorterCountm(@Ret) = 1 /*(m=1 and *o) -> E */ BEGIN IF dbo.fnPorterEndsDoubleCVC(@Ret) = 1 SELECT @ret = @Ret + N'e' END END ---------------------------------------------------------------------------------------------------------- -- --STEP 1C -- -- (*v*) Y -> I happy -> happi -- sky -> sky IF RIGHT(@Ret ,LEN(N'y')) = N'y' BEGIN --trim and check for vowel SELECT @temp = LEFT(@Ret, LEN(@Ret)-1) IF dbo.fnPorterContainsVowel(@temp) = 1 SELECT @ret = LEFT(@Ret, LEN(@Ret) - 1) + N'i' END RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterStep2 ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN /*STEP 2 (m>0) ATIONAL -> ATE relational -> relate (m>0) TIONAL -> TION conditional -> condition rational -> rational (m>0) ENCI -> ENCE valenci -> valence (m>0) ANCI -> ANCE hesitanci -> hesitance (m>0) IZER -> IZE digitizer -> digitize Also, (m>0) BLI -> BLE conformabli -> conformable (m>0) ALLI -> AL radicalli -> radical (m>0) ENTLI -> ENT differentli -> different (m>0) ELI -> E vileli - > vile (m>0) OUSLI -> OUS analogousli -> analogous (m>0) IZATION -> IZE vietnamization -> vietnamize (m>0) ATION -> ATE predication -> predicate (m>0) ATOR -> ATE operator -> operate (m>0) ALISM -> AL feudalism -> feudal (m>0) IVENESS -> IVE decisiveness -> decisive (m>0) FULNESS -> FUL hopefulness -> hopeful (m>0) OUSNESS -> OUS callousness -> callous (m>0) ALITI -> AL formaliti -> formal (m>0) IVITI -> IVE sensitiviti -> sensitive (m>0) BILITI -> BLE sensibiliti -> sensible Also, (m>0) LOGI -> LOG apologi -> apolog The test for the string S1 can be made fast by doing a program switch on the penultimate letter of the word being tested. This gives a fairly even breakdown of the possible values of the string S1. It will be seen in fact that the S1-strings in step 2 are presented here in the alphabetical order of their penultimate letter. Similar techniques may be applied in the other steps. */ --declaring local variables DECLARE @Ret nvarchar(4000), @Temp nvarchar(4000) DECLARE @Phrase1 NVARCHAR(15), @Phrase2 NVARCHAR(15) DECLARE @CursorName CURSOR --, @i int --checking word SET @Ret = @InWord SET @CursorName = CURSOR FOR SELECT phrase1, phrase2 FROM tblPorterStemming WHERE Step = 2 AND RIGHT(@Ret ,LEN(Phrase1)) = Phrase1 ORDER BY Ordering OPEN @CursorName -- Do Step 2 FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 WHILE @@FETCH_STATUS = 0 BEGIN --IF RIGHT(@Ret ,LEN(@Phrase1)) = @Phrase1 BEGIN SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) IF dbo.fnPorterCountm(@temp) > 0 SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) + @Phrase2 BREAK END FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 END -- Free Resources CLOSE @CursorName DEALLOCATE @CursorName --retuning the word RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterStep3 ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN /*STEP 3 (m>0) ICATE -> IC triplicate -> triplic (m>0) ATIVE -> formative -> form (m>0) ALIZE -> AL formalize -> formal (m>0) ICITI -> IC electriciti -> electric (m>0) ICAL -> IC electrical -> electric (m>0) FUL -> hopeful -> hope (m>0) NESS -> goodness -> good */ --declaring local variables DECLARE @Ret nvarchar(4000), @Temp nvarchar(4000) DECLARE @Phrase1 NVARCHAR(15), @Phrase2 NVARCHAR(15) DECLARE @CursorName CURSOR, @i int --checking word SET @Ret = @InWord SET @CursorName = CURSOR FOR SELECT phrase1, phrase2 FROM tblPorterStemming WHERE Step = 3 AND RIGHT(@Ret ,LEN(Phrase1)) = Phrase1 ORDER BY Ordering OPEN @CursorName -- Do Step 2 FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 WHILE @@FETCH_STATUS = 0 BEGIN --IF RIGHT(@Ret ,LEN(@Phrase1)) = @Phrase1 BEGIN SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) IF dbo.fnPorterCountm(@temp) > 0 SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) + @Phrase2 BREAK END FETCH NEXT FROM @CursorName INTO @Phrase1, @Phrase2 END -- Free Resources CLOSE @CursorName DEALLOCATE @CursorName --retuning the word RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterStep4 ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN --STEP 4 -- -- (m>1) AL -> revival -> reviv -- (m>1) ANCE -> allowance -> allow -- (m>1) ENCE -> inference -> infer -- (m>1) ER -> airliner -> airlin -- (m>1) IC -> gyroscopic -> gyroscop -- (m>1) ABLE -> adjustable -> adjust -- (m>1) IBLE -> defensible -> defens -- (m>1) ANT -> irritant -> irrit -- (m>1) EMENT -> replacement -> replac -- (m>1) MENT -> adjustment -> adjust -- (m>1) ENT -> dependent -> depend -- (m>1 and (*S or *T)) ION -> adoption -> adopt -- (m>1) OU -> homologou -> homolog -- (m>1) ISM -> communism -> commun -- (m>1) ATE -> activate -> activ -- (m>1) ITI -> angulariti -> angular -- (m>1) OUS -> homologous -> homolog -- (m>1) IVE -> effective -> effect -- (m>1) IZE -> bowdlerize -> bowdler -- --The suffixes are now removed. All that remains is a little tidying up. DECLARE @Ret nvarchar(4000), @Temp nvarchar(4000) DECLARE @Phrase1 NVARCHAR(15) DECLARE @CursorName CURSOR --checking word SELECT @Ret = @InWord SET @CursorName = CURSOR FOR SELECT phrase1 FROM tblPorterStemming WHERE Step = 4 AND RIGHT(@Ret ,LEN(Phrase1)) = Phrase1 ORDER BY Ordering OPEN @CursorName -- Do Step 4 FETCH NEXT FROM @CursorName INTO @Phrase1 WHILE @@FETCH_STATUS = 0 BEGIN --IF RIGHT(@Ret ,LEN(@Phrase1)) = @Phrase1 BEGIN SELECT @temp = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) IF dbo.fnPorterCountm(@temp) > 1 BEGIN IF RIGHT(@Ret ,LEN(N'ion')) = N'ion' BEGIN IF RIGHT(@temp ,1) = N's' OR RIGHT(@temp ,1) = N't' SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) END ELSE SELECT @Ret = LEFT(@Ret, LEN(@Ret) - LEN(@Phrase1)) END BREAK END FETCH NEXT FROM @CursorName INTO @Phrase1 END -- Free Resources CLOSE @CursorName DEALLOCATE @CursorName --retuning the word RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION fnPorterStep5 ( @InWord nvarchar(4000) ) RETURNS nvarchar(4000) AS BEGIN --STEP 5a -- -- (m>1) E -> probate -> probat -- rate -> rate -- (m=1 and not *o) E -> cease -> ceas -- --STEP 5b -- -- (m>1 and *d and *L) -> single letter -- controll -> control -- roll -> roll --declaring local variables DECLARE @Ret nvarchar(4000), @Temp nvarchar(4000), @m tinyint SET @Ret = @InWord --Step5a IF RIGHT(@Ret , 1) = N'e' --word ends with e BEGIN SELECT @temp = LEFT(@Ret, LEN(@Ret) - 1) SELECT @m = dbo.fnPorterCountm(@temp) IF @m > 1 --m>1 SELECT @Ret = LEFT(@Ret, LEN(@Ret) - 1) ELSE IF @m = 1 --m=1 BEGIN IF dbo.fnPorterEndsCVC(@temp) = 0 --not *o SELECT @Ret = LEFT(@Ret, LEN(@Ret) - 1) END END ---------------------------------------------------------------------------------------------------------- -- --Step5b IF dbo.fnPorterCountm(@Ret) > 1 BEGIN IF dbo.fnPorterEndsDoubleConsonant(@Ret) = 1 AND RIGHT(@Ret, 1) = N'l' SELECT @Ret = LEFT(@Ret, LEN(@Ret) - 1) END --retuning the word RETURN @Ret END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值