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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.analysis.cn.smart;
20 import org.apache.lucene.analysis.cn.smart.hhmm.SegTokenFilter; // for javadoc
23 * SmartChineseAnalyzer utility constants and methods
24 * @lucene.experimental
26 public class Utility {
28 public static final char[] STRING_CHAR_ARRAY = new String("未##串")
31 public static final char[] NUMBER_CHAR_ARRAY = new String("未##数")
34 public static final char[] START_CHAR_ARRAY = new String("始##始")
37 public static final char[] END_CHAR_ARRAY = new String("末##末").toCharArray();
40 * Delimiters will be filtered to this character by {@link SegTokenFilter}
42 public static final char[] COMMON_DELIMITER = new char[] { ',' };
45 * Space-like characters that need to be skipped: such as space, tab, newline, carriage return.
47 public static final String SPACES = " \t\r\n";
50 * Maximum bigram frequency (used in the smoothing function).
52 public static final int MAX_FREQUENCE = 2079997 + 80000;
55 * compare two arrays starting at the specified offsets.
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
63 public static int compareArray(char[] larray, int lstartIndex, char[] rarray,
67 if (rarray == null || rstartIndex >= rarray.length)
74 if (lstartIndex >= larray.length)
81 int li = lstartIndex, ri = rstartIndex;
82 while (li < larray.length && ri < rarray.length && larray[li] == rarray[ri]) {
86 if (li == larray.length) {
87 if (ri == rarray.length) {
88 // Both arrays are equivalent, return 0.
91 // larray < rarray because larray has ended first.
96 if (ri == rarray.length) {
97 // larray > rarray because rarray has ended first.
100 // determine by comparison
101 if (larray[li] > rarray[ri])
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)}
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)}
120 public static int compareArrayByPrefix(char[] shortArray, int shortIndex,
121 char[] longArray, int longIndex) {
123 // a null prefix is a prefix of longArray
124 if (shortArray == null)
126 else if (longArray == null)
127 return (shortIndex < shortArray.length) ? 1 : 0;
129 int si = shortIndex, li = longIndex;
130 while (si < shortArray.length && li < longArray.length
131 && shortArray[si] == longArray[li]) {
135 if (si == shortArray.length) {
136 // shortArray is a prefix of longArray
139 // shortArray > longArray because longArray ended first.
140 if (li == longArray.length)
143 // determine by comparison
144 return (shortArray[si] > longArray[li]) ? 1 : -1;
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.
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;
166 if ((ch >= 0x0021 && ch <= 0x00BB) || (ch >= 0x2010 && ch <= 0x2642)
167 || (ch >= 0x3001 && ch <= 0x301E))
168 return CharType.DELIMITER;
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;