add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / stempel / src / java / org / egothor / stemmer / Row.java
1 /*
2                     Egothor Software License version 1.00
3                     Copyright (C) 1997-2004 Leo Galambos.
4                  Copyright (C) 2002-2004 "Egothor developers"
5                       on behalf of the Egothor Project.
6                              All rights reserved.
7
8    This  software  is  copyrighted  by  the "Egothor developers". If this
9    license applies to a single file or document, the "Egothor developers"
10    are the people or entities mentioned as copyright holders in that file
11    or  document.  If  this  license  applies  to the Egothor project as a
12    whole,  the  copyright holders are the people or entities mentioned in
13    the  file CREDITS. This file can be found in the same location as this
14    license in the distribution.
15
16    Redistribution  and  use  in  source and binary forms, with or without
17    modification, are permitted provided that the following conditions are
18    met:
19     1. Redistributions  of  source  code  must retain the above copyright
20        notice, the list of contributors, this list of conditions, and the
21        following disclaimer.
22     2. Redistributions  in binary form must reproduce the above copyright
23        notice, the list of contributors, this list of conditions, and the
24        disclaimer  that  follows  these  conditions  in the documentation
25        and/or other materials provided with the distribution.
26     3. The name "Egothor" must not be used to endorse or promote products
27        derived  from  this software without prior written permission. For
28        written permission, please contact Leo.G@seznam.cz
29     4. Products  derived  from this software may not be called "Egothor",
30        nor  may  "Egothor"  appear  in  their name, without prior written
31        permission from Leo.G@seznam.cz.
32
33    In addition, we request that you include in the end-user documentation
34    provided  with  the  redistribution  and/or  in the software itself an
35    acknowledgement equivalent to the following:
36    "This product includes software developed by the Egothor Project.
37     http://egothor.sf.net/"
38
39    THIS  SOFTWARE  IS  PROVIDED  ``AS  IS''  AND ANY EXPRESSED OR IMPLIED
40    WARRANTIES,  INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41    MERCHANTABILITY  AND  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42    IN  NO  EVENT  SHALL THE EGOTHOR PROJECT OR ITS CONTRIBUTORS BE LIABLE
43    FOR   ANY   DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
44    CONSEQUENTIAL  DAMAGES  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45    SUBSTITUTE  GOODS  OR  SERVICES;  LOSS  OF  USE,  DATA, OR PROFITS; OR
46    BUSINESS  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47    WHETHER  IN  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
48    OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
49    IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
51    This  software  consists  of  voluntary  contributions  made  by  many
52    individuals  on  behalf  of  the  Egothor  Project  and was originally
53    created by Leo Galambos (Leo.G@seznam.cz).
54  */
55 package org.egothor.stemmer;
56
57 import java.io.DataInput;
58 import java.io.DataOutput;
59 import java.io.IOException;
60 import java.util.Iterator;
61 import java.util.TreeMap;
62
63 /**
64  * The Row class represents a row in a matrix representation of a trie.
65  */
66 public class Row {
67   TreeMap<Character,Cell> cells = new TreeMap<Character,Cell>();
68   int uniformCnt = 0;
69   int uniformSkip = 0;
70   
71   /**
72    * Construct a Row object from input carried in via the given input stream.
73    * 
74    * @param is the input stream
75    * @exception IOException if an I/O error occurs
76    */
77   public Row(DataInput is) throws IOException {
78     for (int i = is.readInt(); i > 0; i--) {
79       char ch = is.readChar();
80       Cell c = new Cell();
81       c.cmd = is.readInt();
82       c.cnt = is.readInt();
83       c.ref = is.readInt();
84       c.skip = is.readInt();
85       cells.put(ch, c);
86     }
87   }
88   
89   /**
90    * The default constructor for the Row object.
91    */
92   public Row() {}
93   
94   /**
95    * Construct a Row using the cells of the given Row.
96    * 
97    * @param old the Row to copy
98    */
99   public Row(Row old) {
100     cells = old.cells;
101   }
102   
103   /**
104    * Set the command in the Cell of the given Character to the given integer.
105    * 
106    * @param way the Character defining the Cell
107    * @param cmd the new command
108    */
109   public void setCmd(Character way, int cmd) {
110     Cell c = at(way);
111     if (c == null) {
112       c = new Cell();
113       c.cmd = cmd;
114       cells.put(way, c);
115     } else {
116       c.cmd = cmd;
117     }
118     c.cnt = (cmd >= 0) ? 1 : 0;
119   }
120   
121   /**
122    * Set the reference to the next row in the Cell of the given Character to the
123    * given integer.
124    * 
125    * @param way the Character defining the Cell
126    * @param ref The new ref value
127    */
128   public void setRef(Character way, int ref) {
129     Cell c = at(way);
130     if (c == null) {
131       c = new Cell();
132       c.ref = ref;
133       cells.put(way, c);
134     } else {
135       c.ref = ref;
136     }
137   }
138   
139   /**
140    * Return the number of cells in use.
141    * 
142    * @return the number of cells in use
143    */
144   public int getCells() {
145     Iterator<Character> i = cells.keySet().iterator();
146     int size = 0;
147     for (; i.hasNext();) {
148       Character c = i.next();
149       Cell e = at(c);
150       if (e.cmd >= 0 || e.ref >= 0) {
151         size++;
152       }
153     }
154     return size;
155   }
156   
157   /**
158    * Return the number of references (how many transitions) to other rows.
159    * 
160    * @return the number of references
161    */
162   public int getCellsPnt() {
163     Iterator<Character> i = cells.keySet().iterator();
164     int size = 0;
165     for (; i.hasNext();) {
166       Character c = i.next();
167       Cell e = at(c);
168       if (e.ref >= 0) {
169         size++;
170       }
171     }
172     return size;
173   }
174   
175   /**
176    * Return the number of patch commands saved in this Row.
177    * 
178    * @return the number of patch commands
179    */
180   public int getCellsVal() {
181     Iterator<Character> i = cells.keySet().iterator();
182     int size = 0;
183     for (; i.hasNext();) {
184       Character c = i.next();
185       Cell e = at(c);
186       if (e.cmd >= 0) {
187         size++;
188       }
189     }
190     return size;
191   }
192   
193   /**
194    * Return the command in the Cell associated with the given Character.
195    * 
196    * @param way the Character associated with the Cell holding the desired
197    *          command
198    * @return the command
199    */
200   public int getCmd(Character way) {
201     Cell c = at(way);
202     return (c == null) ? -1 : c.cmd;
203   }
204   
205   /**
206    * Return the number of patch commands were in the Cell associated with the
207    * given Character before the Trie containing this Row was reduced.
208    * 
209    * @param way the Character associated with the desired Cell
210    * @return the number of patch commands before reduction
211    */
212   public int getCnt(Character way) {
213     Cell c = at(way);
214     return (c == null) ? -1 : c.cnt;
215   }
216   
217   /**
218    * Return the reference to the next Row in the Cell associated with the given
219    * Character.
220    * 
221    * @param way the Character associated with the desired Cell
222    * @return the reference, or -1 if the Cell is <tt>null,/tt>
223    */
224   public int getRef(Character way) {
225     Cell c = at(way);
226     return (c == null) ? -1 : c.ref;
227   }
228   
229   /**
230    * Write the contents of this Row to the given output stream.
231    * 
232    * @param os the output stream
233    * @exception IOException if an I/O error occurs
234    */
235   public void store(DataOutput os) throws IOException {
236     os.writeInt(cells.size());
237     Iterator<Character> i = cells.keySet().iterator();
238     for (; i.hasNext();) {
239       Character c = i.next();
240       Cell e = at(c);
241       if (e.cmd < 0 && e.ref < 0) {
242         continue;
243       }
244       
245       os.writeChar(c.charValue());
246       os.writeInt(e.cmd);
247       os.writeInt(e.cnt);
248       os.writeInt(e.ref);
249       os.writeInt(e.skip);
250     }
251   }
252   
253   /**
254    * Return the number of identical Cells (containing patch commands) in this
255    * Row.
256    * 
257    * @param eqSkip when set to <tt>false</tt> the removed patch commands are
258    *          considered
259    * @return the number of identical Cells, or -1 if there are (at least) two
260    *         different cells
261    */
262   public int uniformCmd(boolean eqSkip) {
263     Iterator<Cell> i = cells.values().iterator();
264     int ret = -1;
265     uniformCnt = 1;
266     uniformSkip = 0;
267     for (; i.hasNext();) {
268       Cell c = i.next();
269       if (c.ref >= 0) {
270         return -1;
271       }
272       if (c.cmd >= 0) {
273         if (ret < 0) {
274           ret = c.cmd;
275           uniformSkip = c.skip;
276         } else if (ret == c.cmd) {
277           if (eqSkip) {
278             if (uniformSkip == c.skip) {
279               uniformCnt++;
280             } else {
281               return -1;
282             }
283           } else {
284             uniformCnt++;
285           }
286         } else {
287           return -1;
288         }
289       }
290     }
291     return ret;
292   }
293   
294   /**
295    * Write the contents of this Row to stdout.
296    */
297   public void print() {
298     for (Iterator<Character> i = cells.keySet().iterator(); i.hasNext();) {
299       Character ch = i.next();
300       Cell c = at(ch);
301       System.out.print("[" + ch + ":" + c + "]");
302     }
303     System.out.println();
304   }
305   
306   Cell at(Character index) {
307     return cells.get(index);
308   }
309 }