| 1 |
package org.egothor.stemmer; |
| 2 |
|
| 3 |
/* |
| 4 |
Egothor Software License version 1.00 |
| 5 |
Copyright (C) 1997-2004 Leo Galambos. |
| 6 |
Copyright (C) 2002-2004 "Egothor developers" |
| 7 |
on behalf of the Egothor Project. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This software is copyrighted by the "Egothor developers". If this |
| 11 |
license applies to a single file or document, the "Egothor developers" |
| 12 |
are the people or entities mentioned as copyright holders in that file |
| 13 |
or document. If this license applies to the Egothor project as a |
| 14 |
whole, the copyright holders are the people or entities mentioned in |
| 15 |
the file CREDITS. This file can be found in the same location as this |
| 16 |
license in the distribution. |
| 17 |
|
| 18 |
Redistribution and use in source and binary forms, with or without |
| 19 |
modification, are permitted provided that the following conditions are |
| 20 |
met: |
| 21 |
1. Redistributions of source code must retain the above copyright |
| 22 |
notice, the list of contributors, this list of conditions, and the |
| 23 |
following disclaimer. |
| 24 |
2. Redistributions in binary form must reproduce the above copyright |
| 25 |
notice, the list of contributors, this list of conditions, and the |
| 26 |
disclaimer that follows these conditions in the documentation |
| 27 |
and/or other materials provided with the distribution. |
| 28 |
3. The name "Egothor" must not be used to endorse or promote products |
| 29 |
derived from this software without prior written permission. For |
| 30 |
written permission, please contact [email protected] |
| 31 |
4. Products derived from this software may not be called "Egothor", |
| 32 |
nor may "Egothor" appear in their name, without prior written |
| 33 |
permission from [email protected]. |
| 34 |
|
| 35 |
In addition, we request that you include in the end-user documentation |
| 36 |
provided with the redistribution and/or in the software itself an |
| 37 |
acknowledgement equivalent to the following: |
| 38 |
"This product includes software developed by the Egothor Project. |
| 39 |
http://egothor.sf.net/" |
| 40 |
|
| 41 |
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 42 |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 43 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 44 |
IN NO EVENT SHALL THE EGOTHOR PROJECT OR ITS CONTRIBUTORS BE LIABLE |
| 45 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 46 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 47 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 48 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 49 |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 50 |
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 51 |
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 52 |
|
| 53 |
This software consists of voluntary contributions made by many |
| 54 |
individuals on behalf of the Egothor Project and was originally |
| 55 |
created by Leo Galambos ([email protected]). |
| 56 |
*/ |
| 57 |
|
| 58 |
import org.apache.lucene.tests.util.LuceneTestCase; |
| 59 |
|
| 60 |
public class TestStemmer extends LuceneTestCase { |
| 61 |
|
| 62 |
public void testTrie() { |
| 63 |
Trie t = new Trie(true); |
| 64 |
|
| 65 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 66 |
String[] vals = {"1", "2", "2", "4"}; |
| 67 |
|
| 68 |
for (int i = 0; i < keys.length; i++) { |
| 69 |
t.add(keys[i], vals[i]); |
| 70 |
} |
| 71 |
|
| 72 |
assertEquals(0, t.root); |
| 73 |
assertEquals(2, t.rows.size()); |
| 74 |
assertEquals(3, t.cmds.size()); |
| 75 |
assertTrieContents(t, keys, vals); |
| 76 |
} |
| 77 |
|
| 78 |
public void testTrieBackwards() { |
| 79 |
Trie t = new Trie(false); |
| 80 |
|
| 81 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 82 |
String[] vals = {"1", "2", "2", "4"}; |
| 83 |
|
| 84 |
for (int i = 0; i < keys.length; i++) { |
| 85 |
t.add(keys[i], vals[i]); |
| 86 |
} |
| 87 |
|
| 88 |
assertTrieContents(t, keys, vals); |
| 89 |
} |
| 90 |
|
| 91 |
public void testMultiTrie() { |
| 92 |
Trie t = new MultiTrie(true); |
| 93 |
|
| 94 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 95 |
String[] vals = {"1", "2", "2", "4"}; |
| 96 |
|
| 97 |
for (int i = 0; i < keys.length; i++) { |
| 98 |
t.add(keys[i], vals[i]); |
| 99 |
} |
| 100 |
|
| 101 |
assertTrieContents(t, keys, vals); |
| 102 |
} |
| 103 |
|
| 104 |
public void testMultiTrieBackwards() { |
| 105 |
Trie t = new MultiTrie(false); |
| 106 |
|
| 107 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 108 |
String[] vals = {"1", "2", "2", "4"}; |
| 109 |
|
| 110 |
for (int i = 0; i < keys.length; i++) { |
| 111 |
t.add(keys[i], vals[i]); |
| 112 |
} |
| 113 |
|
| 114 |
assertTrieContents(t, keys, vals); |
| 115 |
} |
| 116 |
|
| 117 |
public void testMultiTrie2() { |
| 118 |
Trie t = new MultiTrie2(true); |
| 119 |
|
| 120 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 121 |
/* |
| 122 |
* short vals won't work, see line 155 for example |
| 123 |
* the IOOBE is caught (wierd), but shouldnt affect patch cmds? |
| 124 |
*/ |
| 125 |
String[] vals = {"1111", "2222", "2223", "4444"}; |
| 126 |
|
| 127 |
for (int i = 0; i < keys.length; i++) { |
| 128 |
t.add(keys[i], vals[i]); |
| 129 |
} |
| 130 |
|
| 131 |
assertTrieContents(t, keys, vals); |
| 132 |
} |
| 133 |
|
| 134 |
public void testMultiTrie2Backwards() { |
| 135 |
Trie t = new MultiTrie2(false); |
| 136 |
|
| 137 |
String[] keys = {"a", "ba", "bb", "c"}; |
| 138 |
/* |
| 139 |
* short vals won't work, see line 155 for example |
| 140 |
* the IOOBE is caught (wierd), but shouldnt affect patch cmds? |
| 141 |
*/ |
| 142 |
String[] vals = {"1111", "2222", "2223", "4444"}; |
| 143 |
|
| 144 |
for (int i = 0; i < keys.length; i++) { |
| 145 |
t.add(keys[i], vals[i]); |
| 146 |
} |
| 147 |
|
| 148 |
assertTrieContents(t, keys, vals); |
| 149 |
} |
| 150 |
|
| 151 |
private static void assertTrieContents(Trie trie, String[] keys, String[] vals) { |
| 152 |
Trie[] tries = |
| 153 |
new Trie[] { |
| 154 |
trie, |
| 155 |
trie.reduce(new Optimizer()), |
| 156 |
trie.reduce(new Optimizer2()), |
| 157 |
trie.reduce(new Gener()), |
| 158 |
trie.reduce(new Lift(true)), |
| 159 |
trie.reduce(new Lift(false)) |
| 160 |
}; |
| 161 |
|
| 162 |
for (Trie t : tries) { |
| 163 |
for (int i = 0; i < keys.length; i++) { |
| 164 |
assertEquals(vals[i], t.getFully(keys[i]).toString()); |
| 165 |
assertEquals(vals[i], t.getLastOnPath(keys[i]).toString()); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |