add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / util / StemmerUtil.java
1 package org.apache.lucene.analysis.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /** Some commonly-used stemming functions */
21 public class StemmerUtil {
22   /**
23    * Returns true if the character array starts with the suffix.
24    * 
25    * @param s Input Buffer
26    * @param len length of input buffer
27    * @param prefix Prefix string to test
28    * @return true if <code>s</code> starts with <code>prefix</code>
29    */
30   public static boolean startsWith(char s[], int len, String prefix) {
31     final int prefixLen = prefix.length();
32     if (prefixLen > len)
33       return false;
34     for (int i = 0; i < prefixLen; i++)
35       if (s[i] != prefix.charAt(i)) 
36         return false;
37     return true;
38   }
39   
40   /**
41    * Returns true if the character array ends with the suffix.
42    * 
43    * @param s Input Buffer
44    * @param len length of input buffer
45    * @param suffix Suffix string to test
46    * @return true if <code>s</code> ends with <code>suffix</code>
47    */
48   public static boolean endsWith(char s[], int len, String suffix) {
49     final int suffixLen = suffix.length();
50     if (suffixLen > len)
51       return false;
52     for (int i = suffixLen - 1; i >= 0; i--)
53       if (s[len -(suffixLen - i)] != suffix.charAt(i))
54         return false;
55     
56     return true;
57   }
58   
59   /**
60    * Returns true if the character array ends with the suffix.
61    * 
62    * @param s Input Buffer
63    * @param len length of input buffer
64    * @param suffix Suffix string to test
65    * @return true if <code>s</code> ends with <code>suffix</code>
66    */
67   public static boolean endsWith(char s[], int len, char suffix[]) {
68     final int suffixLen = suffix.length;
69     if (suffixLen > len)
70       return false;
71     for (int i = suffixLen - 1; i >= 0; i--)
72       if (s[len -(suffixLen - i)] != suffix[i])
73         return false;
74     
75     return true;
76   }
77   
78   /**
79    * Delete a character in-place
80    * 
81    * @param s Input Buffer
82    * @param pos Position of character to delete
83    * @param len length of input buffer
84    * @return length of input buffer after deletion
85    */
86   public static int delete(char s[], int pos, int len) {
87     if (pos < len) 
88       System.arraycopy(s, pos + 1, s, pos, len - pos - 1);
89     
90     return len - 1;
91   }
92   
93   /**
94    * Delete n characters in-place
95    * 
96    * @param s Input Buffer
97    * @param pos Position of character to delete
98    * @param len Length of input buffer
99    * @param nChars number of characters to delete
100    * @return length of input buffer after deletion
101    */
102   public static int deleteN(char s[], int pos, int len, int nChars) {
103     // TODO: speed up, this is silly
104     for (int i = 0; i < nChars; i++)
105       len = delete(s, pos, len);
106     return len;
107   }
108 }