add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / smartcn / src / java / org / apache / lucene / analysis / cn / smart / hhmm / AbstractDictionary.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.hhmm;
19
20 import java.io.UnsupportedEncodingException;
21
22 /**
23  * <p>
24  * SmartChineseAnalyzer abstract dictionary implementation.
25  * </p>
26  * <p>
27  * Contains methods for dealing with GB2312 encoding.
28  * </p>
29  * @lucene.experimental
30  */
31 abstract class AbstractDictionary {
32   /**
33    * First Chinese Character in GB2312 (15 * 94)
34    * Characters in GB2312 are arranged in a grid of 94 * 94, 0-14 are unassigned or punctuation.
35    */
36   public static final int GB2312_FIRST_CHAR = 1410;
37
38   /**
39    * Last Chinese Character in GB2312 (87 * 94). 
40    * Characters in GB2312 are arranged in a grid of 94 * 94, 88-94 are unassigned.
41    */
42   public static final int GB2312_CHAR_NUM = 87 * 94;
43
44   /**
45    * Dictionary data contains 6768 Chinese characters with frequency statistics.
46    */
47   public static final int CHAR_NUM_IN_FILE = 6768;
48
49   // =====================================================
50   // code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
51   // B0A0 啊 阿 埃 挨 哎 唉 哀 皑 癌 蔼 矮 艾 碍 爱 隘
52   // B0B0 鞍 氨 安 俺 按 暗 岸 胺 案 肮 昂 盎 凹 敖 熬 翱
53   // B0C0 袄 傲 奥 懊 澳 芭 捌 扒 叭 吧 笆 八 疤 巴 拔 跋
54   // B0D0 靶 把 耙 坝 霸 罢 爸 白 柏 百 摆 佰 败 拜 稗 斑
55   // B0E0 班 搬 扳 般 颁 板 版 扮 拌 伴 瓣 半 办 绊 邦 帮
56   // B0F0 梆 榜 膀 绑 棒 磅 蚌 镑 傍 谤 苞 胞 包 褒 剥
57   // =====================================================
58   //
59   // GB2312 character set:
60   // 01 94 Symbols
61   // 02 72 Numbers
62   // 03 94 Latin
63   // 04 83 Kana
64   // 05 86 Katakana
65   // 06 48 Greek
66   // 07 66 Cyrillic
67   // 08 63 Phonetic Symbols
68   // 09 76 Drawing Symbols
69   // 10-15 Unassigned
70   // 16-55 3755 Plane 1, in pinyin order
71   // 56-87 3008 Plane 2, in radical/stroke order
72   // 88-94 Unassigned
73   // ======================================================
74
75   /**
76    * <p>
77    * Transcode from GB2312 ID to Unicode
78    * </p>
79    * <p>
80    * GB2312 is divided into a 94 * 94 grid, containing 7445 characters consisting of 6763 Chinese characters and 682 symbols.
81    * Some regions are unassigned (reserved).
82    * </p>
83    * 
84    * @param ccid GB2312 id
85    * @return unicode String
86    */
87   public String getCCByGB2312Id(int ccid) {
88     if (ccid < 0 || ccid > WordDictionary.GB2312_CHAR_NUM)
89       return "";
90     int cc1 = ccid / 94 + 161;
91     int cc2 = ccid % 94 + 161;
92     byte[] buffer = new byte[2];
93     buffer[0] = (byte) cc1;
94     buffer[1] = (byte) cc2;
95     try {
96       String cchar = new String(buffer, "GB2312");
97       return cchar;
98     } catch (UnsupportedEncodingException e) {
99       return "";
100     }
101   }
102
103   /**
104    * Transcode from Unicode to GB2312
105    * 
106    * @param ch input character in Unicode, or character in Basic Latin range.
107    * @return position in GB2312
108    */
109   public short getGB2312Id(char ch) {
110     try {
111       byte[] buffer = Character.toString(ch).getBytes("GB2312");
112       if (buffer.length != 2) {
113         // Should be a two-byte character
114         return -1;
115       }
116       int b0 = (buffer[0] & 0x0FF) - 161; // Code starts from A1, therefore subtract 0xA1=161
117       int b1 = (buffer[1] & 0x0FF) - 161; // There is no Chinese char for the first and last symbol. 
118                                                                                         // Therefore, each code page only has 16*6-2=94 characters.
119       return (short) (b0 * 94 + b1);
120     } catch (UnsupportedEncodingException e) {
121       e.printStackTrace();
122     }
123     return -1;
124   }
125
126   /**
127    * 32-bit FNV Hash Function
128    * 
129    * @param c input character
130    * @return hashcode
131    */
132   public long hash1(char c) {
133     final long p = 1099511628211L;
134     long hash = 0xcbf29ce484222325L;
135     hash = (hash ^ (c & 0x00FF)) * p;
136     hash = (hash ^ (c >> 8)) * p;
137     hash += hash << 13;
138     hash ^= hash >> 7;
139     hash += hash << 3;
140     hash ^= hash >> 17;
141     hash += hash << 5;
142     return hash;
143   }
144
145   /**
146    * 32-bit FNV Hash Function
147    * 
148    * @param carray character array
149    * @return hashcode
150    */
151   public long hash1(char carray[]) {
152     final long p = 1099511628211L;
153     long hash = 0xcbf29ce484222325L;
154     for (int i = 0; i < carray.length; i++) {
155       char d = carray[i];
156       hash = (hash ^ (d & 0x00FF)) * p;
157       hash = (hash ^ (d >> 8)) * p;
158     }
159
160     // hash += hash << 13;
161     // hash ^= hash >> 7;
162     // hash += hash << 3;
163     // hash ^= hash >> 17;
164     // hash += hash << 5;
165     return hash;
166   }
167
168   /**
169    * djb2 hash algorithm,this algorithm (k=33) was first reported by dan
170    * bernstein many years ago in comp.lang.c. another version of this algorithm
171    * (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i];
172    * the magic of number 33 (why it works better than many other constants,
173    * prime or not) has never been adequately explained.
174    * 
175    * @param c character
176    * @return hashcode
177    */
178   public int hash2(char c) {
179     int hash = 5381;
180
181     /* hash 33 + c */
182     hash = ((hash << 5) + hash) + c & 0x00FF;
183     hash = ((hash << 5) + hash) + c >> 8;
184
185     return hash;
186   }
187
188   /**
189    * djb2 hash algorithm,this algorithm (k=33) was first reported by dan
190    * bernstein many years ago in comp.lang.c. another version of this algorithm
191    * (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i];
192    * the magic of number 33 (why it works better than many other constants,
193    * prime or not) has never been adequately explained.
194    * 
195    * @param carray character array
196    * @return hashcode
197    */
198   public int hash2(char carray[]) {
199     int hash = 5381;
200
201     /* hash 33 + c */
202     for (int i = 0; i < carray.length; i++) {
203       char d = carray[i];
204       hash = ((hash << 5) + hash) + d & 0x00FF;
205       hash = ((hash << 5) + hash) + d >> 8;
206     }
207
208     return hash;
209   }
210 }