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.
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.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are
19 1. Redistributions of source code must retain the above copyright
20 notice, the list of contributors, this list of conditions, and the
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.
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/"
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.
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).
55 package org.egothor.stemmer;
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;
64 * A Trie is used to store a dictionary of words and their stems.
66 * Actually, what is stored are words with their respective patch commands. A
67 * trie can be termed forward (keys read from left to right) or backward (keys
68 * read from right to left). This property will vary depending on the language
69 * for which a Trie is constructed.
72 List<Row> rows = new ArrayList<Row>();
73 List<CharSequence> cmds = new ArrayList<CharSequence>();
76 boolean forward = false;
79 * Constructor for the Trie object.
81 * @param is the input stream
82 * @exception IOException if an I/O error occurs
84 public Trie(DataInput is) throws IOException {
85 forward = is.readBoolean();
87 for (int i = is.readInt(); i > 0; i--) {
88 cmds.add(is.readUTF());
90 for (int i = is.readInt(); i > 0; i--) {
91 rows.add(new Row(is));
96 * Constructor for the Trie object.
98 * @param forward set to <tt>true</tt>
100 public Trie(boolean forward) {
103 this.forward = forward;
107 * Constructor for the Trie object.
109 * @param forward <tt>true</tt> if read left to right, <tt>false</tt> if read
111 * @param root index of the row that is the root node
112 * @param cmds the patch commands to store
113 * @param rows a Vector of Vectors. Each inner Vector is a node of this Trie
115 public Trie(boolean forward, int root, List<CharSequence> cmds, List<Row> rows) {
119 this.forward = forward;
123 * Gets the all attribute of the Trie object
125 * @param key Description of the Parameter
126 * @return The all value
128 public CharSequence[] getAll(CharSequence key) {
129 int res[] = new int[key.length()];
131 Row now = getRow(root);
133 StrEnum e = new StrEnum(key, forward);
136 for (int i = 0; i < key.length() - 1; i++) {
137 Character ch = new Character(e.next());
141 for (int j = 0; j < resc; j++) {
160 w = now.getCmd(new Character(e.next()));
163 for (int j = 0; j < resc; j++) {
178 CharSequence R[] = new CharSequence[resc];
179 for (int j = 0; j < resc; j++) {
180 R[j] = cmds.get(res[j]);
186 * Return the number of cells in this Trie object.
188 * @return the number of cells
190 public int getCells() {
193 size += row.getCells();
198 * Gets the cellsPnt attribute of the Trie object
200 * @return The cellsPnt value
202 public int getCellsPnt() {
205 size += row.getCellsPnt();
210 * Gets the cellsVal attribute of the Trie object
212 * @return The cellsVal value
214 public int getCellsVal() {
217 size += row.getCellsVal();
222 * Return the element that is stored in a cell associated with the given key.
225 * @return the associated element
227 public CharSequence getFully(CharSequence key) {
228 Row now = getRow(root);
232 StrEnum e = new StrEnum(key, forward);
234 Character aux = null;
236 for (int i = 0; i < key.length();) {
237 ch = new Character(e.next());
247 for (int skip = c.skip; skip > 0; skip--) {
248 if (i < key.length()) {
249 aux = new Character(e.next());
259 } else if (i < key.length()) {
263 return (cmd == -1) ? null : cmds.get(cmd);
267 * Return the element that is stored as last on a path associated with the
270 * @param key the key associated with the desired element
271 * @return the last on path element
273 public CharSequence getLastOnPath(CharSequence key) {
274 Row now = getRow(root);
276 CharSequence last = null;
277 StrEnum e = new StrEnum(key, forward);
279 for (int i = 0; i < key.length() - 1; i++) {
280 Character ch = new Character(e.next());
292 w = now.getCmd(new Character(e.next()));
293 return (w >= 0) ? cmds.get(w) : last;
297 * Return the Row at the given index.
299 * @param index the index containing the desired Row
302 private Row getRow(int index) {
303 if (index < 0 || index >= rows.size()) {
306 return rows.get(index);
310 * Write this Trie to the given output stream.
312 * @param os the output stream
313 * @exception IOException if an I/O error occurs
315 public void store(DataOutput os) throws IOException {
316 os.writeBoolean(forward);
318 os.writeInt(cmds.size());
319 for (CharSequence cmd : cmds)
320 os.writeUTF(cmd.toString());
322 os.writeInt(rows.size());
328 * Add the given key associated with the given patch command. If either
329 * parameter is null this method will return without executing.
332 * @param cmd the patch command
334 public void add(CharSequence key, CharSequence cmd) {
335 if (key == null || cmd == null) {
338 if (cmd.length() == 0) {
341 int id_cmd = cmds.indexOf(cmd);
343 id_cmd = cmds.size();
348 Row r = getRow(node);
350 StrEnum e = new StrEnum(key, forward);
352 for (int i = 0; i < e.length() - 1; i++) {
353 Character ch = new Character(e.next());
360 rows.add(n = new Row());
365 r.setCmd(new Character(e.next()), id_cmd);
369 * Remove empty rows from the given Trie and return the newly reduced Trie.
371 * @param by the Trie to reduce
372 * @return the newly reduced Trie
374 public Trie reduce(Reduce by) {
375 return by.optimize(this);
378 public void printInfo(CharSequence prefix) {
379 System.out.println(prefix + "nds " + rows.size() + " cmds " + cmds.size()
380 + " cells " + getCells() + " valcells " + getCellsVal() + " pntcells "
385 * This class is part of the Egothor Project
393 * Constructor for the StrEnum object
395 * @param s Description of the Parameter
396 * @param up Description of the Parameter
398 StrEnum(CharSequence s, boolean up) {
404 from = s.length() - 1;
414 char ch = s.charAt(from);