xref: /Lucene/lucene/analysis/stempel/src/java/org/egothor/stemmer/Lift.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 Lift class is a data structure that is a variation of a Patricia trie.
64  *
65  * <p>Lift's <i>raison d'etre</i> is to implement reduction of the trie via the Lift-Up method.,
66  * which makes the data structure less liable to overstemming.
67  */
68 public class Lift extends Reduce {
69   boolean changeSkip;
70 
71   /**
72    * Constructor for the Lift object.
73    *
74    * @param changeSkip when set to <code>true</code>, comparison of two Cells takes a skip command
75    *     into account
76    */
Lift(boolean changeSkip)77   public Lift(boolean changeSkip) {
78     this.changeSkip = changeSkip;
79   }
80 
81   /**
82    * Optimize (eliminate rows with no content) the given Trie and return the reduced Trie.
83    *
84    * @param orig the Trie to optimized
85    * @return the reduced Trie
86    */
87   @Override
optimize(Trie orig)88   public Trie optimize(Trie orig) {
89     List<CharSequence> cmds = orig.cmds;
90     List<Row> rows = new ArrayList<>();
91     List<Row> orows = orig.rows;
92     int[] remap = new int[orows.size()];
93 
94     for (int j = orows.size() - 1; j >= 0; j--) {
95       liftUp(orows.get(j), orows);
96     }
97 
98     Arrays.fill(remap, -1);
99     rows = removeGaps(orig.root, orows, new ArrayList<Row>(), remap);
100 
101     return new Trie(orig.forward, remap[orig.root], cmds, rows);
102   }
103 
104   /**
105    * Reduce the trie using Lift-Up reduction.
106    *
107    * <p>The Lift-Up reduction propagates all leaf-values (patch commands), where possible, to higher
108    * levels which are closer to the root of the trie.
109    *
110    * @param in the Row to consider when optimizing
111    * @param nodes contains the patch commands
112    */
liftUp(Row in, List<Row> nodes)113   public void liftUp(Row in, List<Row> nodes) {
114     Iterator<Cell> i = in.cells.values().iterator();
115     for (; i.hasNext(); ) {
116       Cell c = i.next();
117       if (c.ref >= 0) {
118         Row to = nodes.get(c.ref);
119         int sum = to.uniformCmd(changeSkip);
120         if (sum >= 0) {
121           if (sum == c.cmd) {
122             if (changeSkip) {
123               if (c.skip != to.uniformSkip + 1) {
124                 continue;
125               }
126               c.skip = to.uniformSkip + 1;
127             } else {
128               c.skip = 0;
129             }
130             c.cnt += to.uniformCnt;
131             c.ref = -1;
132           } else if (c.cmd < 0) {
133             c.cnt = to.uniformCnt;
134             c.cmd = sum;
135             c.ref = -1;
136             if (changeSkip) {
137               c.skip = to.uniformSkip + 1;
138             } else {
139               c.skip = 0;
140             }
141           }
142         }
143       }
144     }
145   }
146 }
147