pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / smartcn / src / java / org / apache / lucene / analysis / cn / smart / Utility.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.cn.smart;
19
20 import org.apache.lucene.analysis.cn.smart.hhmm.SegTokenFilter; // for javadoc
21
22 /**
23  * SmartChineseAnalyzer utility constants and methods
24  * @lucene.experimental
25  */
26 public class Utility {
27
28   public static final char[] STRING_CHAR_ARRAY = new String("未##串")
29       .toCharArray();
30
31   public static final char[] NUMBER_CHAR_ARRAY = new String("未##数")
32       .toCharArray();
33
34   public static final char[] START_CHAR_ARRAY = new String("始##始")
35       .toCharArray();
36
37   public static final char[] END_CHAR_ARRAY = new String("末##末").toCharArray();
38
39   /**
40    * Delimiters will be filtered to this character by {@link SegTokenFilter}
41    */
42   public static final char[] COMMON_DELIMITER = new char[] { ',' };
43
44   /**
45    * Space-like characters that need to be skipped: such as space, tab, newline, carriage return.
46    */
47   public static final String SPACES = "  \t\r\n";
48
49   /**
50    * Maximum bigram frequency (used in the smoothing function). 
51    */
52   public static final int MAX_FREQUENCE = 2079997 + 80000;
53
54   /**
55    * compare two arrays starting at the specified offsets.
56    * 
57    * @param larray left array
58    * @param lstartIndex start offset into larray
59    * @param rarray right array
60    * @param rstartIndex start offset into rarray
61    * @return 0 if the arrays are equal,1 if larray > rarray, -1 if larray < rarray
62    */
63   public static int compareArray(char[] larray, int lstartIndex, char[] rarray,
64       int rstartIndex) {
65
66     if (larray == null) {
67       if (rarray == null || rstartIndex >= rarray.length)
68         return 0;
69       else
70         return -1;
71     } else {
72       // larray != null
73       if (rarray == null) {
74         if (lstartIndex >= larray.length)
75           return 0;
76         else
77           return 1;
78       }
79     }
80
81     int li = lstartIndex, ri = rstartIndex;
82     while (li < larray.length && ri < rarray.length && larray[li] == rarray[ri]) {
83       li++;
84       ri++;
85     }
86     if (li == larray.length) {
87       if (ri == rarray.length) {
88         // Both arrays are equivalent, return 0.
89         return 0;
90       } else {
91         // larray < rarray because larray has ended first.
92         return -1;
93       }
94     } else {
95       // differing lengths
96       if (ri == rarray.length) {
97         // larray > rarray because rarray has ended first.
98         return 1;
99       } else {
100         // determine by comparison
101         if (larray[li] > rarray[ri])
102           return 1;
103         else
104           return -1;
105       }
106     }
107   }
108
109   /**
110    * Compare two arrays, starting at the specified offsets, but treating shortArray as a prefix to longArray.
111    * As long as shortArray is a prefix of longArray, return 0.
112    * Otherwise, behave as {@link Utility#compareArray(char[], int, char[], int)}
113    * 
114    * @param shortArray prefix array
115    * @param shortIndex offset into shortArray
116    * @param longArray long array (word)
117    * @param longIndex offset into longArray
118    * @return 0 if shortArray is a prefix of longArray, otherwise act as {@link Utility#compareArray(char[], int, char[], int)}
119    */
120   public static int compareArrayByPrefix(char[] shortArray, int shortIndex,
121       char[] longArray, int longIndex) {
122
123     // a null prefix is a prefix of longArray
124     if (shortArray == null)
125       return 0;
126     else if (longArray == null)
127       return (shortIndex < shortArray.length) ? 1 : 0;
128
129     int si = shortIndex, li = longIndex;
130     while (si < shortArray.length && li < longArray.length
131         && shortArray[si] == longArray[li]) {
132       si++;
133       li++;
134     }
135     if (si == shortArray.length) {
136       // shortArray is a prefix of longArray
137       return 0;
138     } else {
139       // shortArray > longArray because longArray ended first.
140       if (li == longArray.length)
141         return 1;
142       else
143         // determine by comparison
144         return (shortArray[si] > longArray[li]) ? 1 : -1;
145     }
146   }
147
148   /**
149    * Return the internal {@link CharType} constant of a given character. 
150    * @param ch input character
151    * @return constant from {@link CharType} describing the character type.
152    * 
153    * @see CharType
154    */
155   public static int getCharType(char ch) {
156     // Most (but not all!) of these are Han Ideographic Characters
157     if (ch >= 0x4E00 && ch <= 0x9FA5)
158       return CharType.HANZI;
159     if ((ch >= 0x0041 && ch <= 0x005A) || (ch >= 0x0061 && ch <= 0x007A))
160       return CharType.LETTER;
161     if (ch >= 0x0030 && ch <= 0x0039)
162       return CharType.DIGIT;
163     if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == ' ')
164       return CharType.SPACE_LIKE;
165     // Punctuation Marks
166     if ((ch >= 0x0021 && ch <= 0x00BB) || (ch >= 0x2010 && ch <= 0x2642)
167         || (ch >= 0x3001 && ch <= 0x301E))
168       return CharType.DELIMITER;
169
170     // Full-Width range
171     if ((ch >= 0xFF21 && ch <= 0xFF3A) || (ch >= 0xFF41 && ch <= 0xFF5A))
172       return CharType.FULLWIDTH_LETTER;
173     if (ch >= 0xFF10 && ch <= 0xFF19)
174       return CharType.FULLWIDTH_DIGIT;
175     if (ch >= 0xFE30 && ch <= 0xFF63)
176       return CharType.DELIMITER;
177     return CharType.OTHER;
178
179   }
180 }