pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / fa / PersianNormalizer.java
1 package org.apache.lucene.analysis.fa;
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 import static org.apache.lucene.analysis.util.StemmerUtil.*;
21
22 /**
23  * Normalizer for Persian.
24  * <p>
25  * Normalization is done in-place for efficiency, operating on a termbuffer.
26  * <p>
27  * Normalization is defined as:
28  * <ul>
29  * <li>Normalization of various heh + hamza forms and heh goal to heh.
30  * <li>Normalization of farsi yeh and yeh barree to arabic yeh
31  * <li>Normalization of persian keheh to arabic kaf
32  * </ul>
33  * 
34  */
35 public class PersianNormalizer {
36   public static final char YEH = '\u064A';
37
38   public static final char FARSI_YEH = '\u06CC';
39
40   public static final char YEH_BARREE = '\u06D2';
41
42   public static final char KEHEH = '\u06A9';
43
44   public static final char KAF = '\u0643';
45
46   public static final char HAMZA_ABOVE = '\u0654';
47
48   public static final char HEH_YEH = '\u06C0';
49
50   public static final char HEH_GOAL = '\u06C1';
51
52   public static final char HEH = '\u0647';
53
54   /**
55    * Normalize an input buffer of Persian text
56    * 
57    * @param s input buffer
58    * @param len length of input buffer
59    * @return length of input buffer after normalization
60    */
61   public int normalize(char s[], int len) {
62
63     for (int i = 0; i < len; i++) {
64       switch (s[i]) {
65       case FARSI_YEH:
66       case YEH_BARREE:
67         s[i] = YEH;
68         break;
69       case KEHEH:
70         s[i] = KAF;
71         break;
72       case HEH_YEH:
73       case HEH_GOAL:
74         s[i] = HEH;
75         break;
76       case HAMZA_ABOVE: // necessary for HEH + HAMZA
77         len = delete(s, i, len);
78         i--;
79         break;
80       default:
81         break;
82       }
83     }
84
85     return len;
86   }
87 }