add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / stempel / src / java / org / egothor / stemmer / MultiTrie.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.ArrayList;
61 import java.util.List;
62
63 /**
64  * The MultiTrie is a Trie of Tries. It stores words and their associated patch
65  * commands. The MultiTrie handles patch commmands individually (each command by
66  * itself).
67  */
68 public class MultiTrie extends Trie {
69   final char EOM = '*';
70   final String EOM_NODE = "" + EOM;
71   
72   List<Trie> tries = new ArrayList<Trie>();
73   
74   int BY = 1;
75   
76   /**
77    * Constructor for the MultiTrie object.
78    * 
79    * @param is the input stream
80    * @exception IOException if an I/O error occurs
81    */
82   public MultiTrie(DataInput is) throws IOException {
83     super(false);
84     forward = is.readBoolean();
85     BY = is.readInt();
86     for (int i = is.readInt(); i > 0; i--) {
87       tries.add(new Trie(is));
88     }
89   }
90   
91   /**
92    * Constructor for the MultiTrie object
93    * 
94    * @param forward set to <tt>true</tt> if the elements should be read left to
95    *          right
96    */
97   public MultiTrie(boolean forward) {
98     super(forward);
99   }
100   
101   /**
102    * Return the element that is stored in a cell associated with the given key.
103    * 
104    * @param key the key to the cell holding the desired element
105    * @return the element
106    */
107   @Override
108   public CharSequence getFully(CharSequence key) {
109     StringBuilder result = new StringBuilder(tries.size() * 2);
110     for (int i = 0; i < tries.size(); i++) {
111       CharSequence r = tries.get(i).getFully(key);
112       if (r == null || (r.length() == 1 && r.charAt(0) == EOM)) {
113         return result;
114       }
115       result.append(r);
116     }
117     return result;
118   }
119   
120   /**
121    * Return the element that is stored as last on a path belonging to the given
122    * key.
123    * 
124    * @param key the key associated with the desired element
125    * @return the element that is stored as last on a path
126    */
127   @Override
128   public CharSequence getLastOnPath(CharSequence key) {
129     StringBuilder result = new StringBuilder(tries.size() * 2);
130     for (int i = 0; i < tries.size(); i++) {
131       CharSequence r = tries.get(i).getLastOnPath(key);
132       if (r == null || (r.length() == 1 && r.charAt(0) == EOM)) {
133         return result;
134       }
135       result.append(r);
136     }
137     return result;
138   }
139   
140   /**
141    * Write this data structure to the given output stream.
142    * 
143    * @param os the output stream
144    * @exception IOException if an I/O error occurs
145    */
146   @Override
147   public void store(DataOutput os) throws IOException {
148     os.writeBoolean(forward);
149     os.writeInt(BY);
150     os.writeInt(tries.size());
151     for (Trie trie : tries)
152       trie.store(os);
153   }
154   
155   /**
156    * Add an element to this structure consisting of the given key and patch
157    * command. 
158    * <p>
159    * This method will return without executing if the <tt>cmd</tt>
160    * parameter's length is 0.
161    * 
162    * @param key the key
163    * @param cmd the patch command
164    */
165   @Override
166   public void add(CharSequence key, CharSequence cmd) {
167     if (cmd.length() == 0) {
168       return;
169     }
170     int levels = cmd.length() / BY;
171     while (levels >= tries.size()) {
172       tries.add(new Trie(forward));
173     }
174     for (int i = 0; i < levels; i++) {
175       tries.get(i).add(key, cmd.subSequence(BY * i, BY * i + BY));
176     }
177     tries.get(levels).add(key, EOM_NODE);
178   }
179   
180   /**
181    * Remove empty rows from the given Trie and return the newly reduced Trie.
182    * 
183    * @param by the Trie to reduce
184    * @return the newly reduced Trie
185    */
186   @Override
187   public Trie reduce(Reduce by) {
188     List<Trie> h = new ArrayList<Trie>();
189     for (Trie trie : tries)
190       h.add(trie.reduce(by));
191     
192     MultiTrie m = new MultiTrie(forward);
193     m.tries = h;
194     return m;
195   }
196   
197   /**
198    * Print the given prefix and the position(s) in the Trie where it appears.
199    * 
200    * @param prefix the desired prefix
201    */
202   @Override
203   public void printInfo(CharSequence prefix) {
204     int c = 0;
205     for (Trie trie : tries)
206       trie.printInfo(prefix + "[" + (++c) + "] ");
207   }
208 }