xref: /Lucene/lucene/analysis/stempel/src/java/org/egothor/stemmer/Optimizer.java (revision 8ac26737913d0c1555019e93bc6bf7db1ab9047e)
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.util.ArrayList;
58 import java.util.Arrays;
59 import java.util.Iterator;
60 import java.util.List;
61 
62 /**
63  * The Optimizer class is a Trie that will be reduced (have empty rows removed).
64  *
65  * <p>The reduction will be made by joining two rows where the first is a subset of the second.
66  */
67 public class Optimizer extends Reduce {
68   /** Constructor for the Optimizer object. */
Optimizer()69   public Optimizer() {}
70 
71   /**
72    * Optimize (remove empty rows) from the given Trie and return the resulting Trie.
73    *
74    * @param orig the Trie to consolidate
75    * @return the newly consolidated Trie
76    */
77   @Override
optimize(Trie orig)78   public Trie optimize(Trie orig) {
79     List<CharSequence> cmds = orig.cmds;
80     List<Row> rows = new ArrayList<>();
81     List<Row> orows = orig.rows;
82     int[] remap = new int[orows.size()];
83 
84     for (int j = orows.size() - 1; j >= 0; j--) {
85       Row now = new Remap(orows.get(j), remap);
86       boolean merged = false;
87 
88       for (int i = 0; i < rows.size(); i++) {
89         Row q = merge(now, rows.get(i));
90         if (q != null) {
91           rows.set(i, q);
92           merged = true;
93           remap[j] = i;
94           break;
95         }
96       }
97 
98       if (merged == false) {
99         remap[j] = rows.size();
100         rows.add(now);
101       }
102     }
103 
104     int root = remap[orig.root];
105     Arrays.fill(remap, -1);
106     rows = removeGaps(root, rows, new ArrayList<Row>(), remap);
107 
108     return new Trie(orig.forward, remap[root], cmds, rows);
109   }
110 
111   /**
112    * Merge the given rows and return the resulting Row.
113    *
114    * @param master the master Row
115    * @param existing the existing Row
116    * @return the resulting Row, or <code>null</code> if the operation cannot be realized
117    */
merge(Row master, Row existing)118   public Row merge(Row master, Row existing) {
119     Iterator<Character> i = master.cells.keySet().iterator();
120     Row n = new Row();
121     for (; i.hasNext(); ) {
122       Character ch = i.next();
123       // XXX also must handle Cnt and Skip !!
124       Cell a = master.cells.get(ch);
125       Cell b = existing.cells.get(ch);
126 
127       Cell s = (b == null) ? new Cell(a) : merge(a, b);
128       if (s == null) {
129         return null;
130       }
131       n.cells.put(ch, s);
132     }
133     i = existing.cells.keySet().iterator();
134     for (; i.hasNext(); ) {
135       Character ch = i.next();
136       if (master.at(ch) != null) {
137         continue;
138       }
139       n.cells.put(ch, existing.at(ch));
140     }
141     return n;
142   }
143 
144   /**
145    * Merge the given Cells and return the resulting Cell.
146    *
147    * @param m the master Cell
148    * @param e the existing Cell
149    * @return the resulting Cell, or <code>null</code> if the operation cannot be realized
150    */
merge(Cell m, Cell e)151   public Cell merge(Cell m, Cell e) {
152     Cell n = new Cell();
153 
154     if (m.skip != e.skip) {
155       return null;
156     }
157 
158     if (m.cmd >= 0) {
159       if (e.cmd >= 0) {
160         if (m.cmd == e.cmd) {
161           n.cmd = m.cmd;
162         } else {
163           return null;
164         }
165       } else {
166         n.cmd = m.cmd;
167       }
168     } else {
169       n.cmd = e.cmd;
170     }
171     if (m.ref >= 0) {
172       if (e.ref >= 0) {
173         if (m.ref == e.ref) {
174           if (m.skip == e.skip) {
175             n.ref = m.ref;
176           } else {
177             return null;
178           }
179         } else {
180           return null;
181         }
182       } else {
183         n.ref = m.ref;
184       }
185     } else {
186       n.ref = e.ref;
187     }
188     n.cnt = m.cnt + e.cnt;
189     n.skip = m.skip;
190     return n;
191   }
192 }
193