Debian Codegraph @

lucene/analysis/stempel/src/test/org/egothor/stemmer/TestCompile.java

raw
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 java.io.BufferedInputStream;
59 import java.io.DataInputStream;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.io.LineNumberReader;
63 import java.nio.charset.StandardCharsets;
64 import java.nio.file.Files;
65 import java.nio.file.Path;
66 import java.util.Locale;
67 import java.util.StringTokenizer;
68 import org.apache.lucene.tests.util.LuceneTestCase;
69
70 public class TestCompile extends LuceneTestCase {
71
72 public void testCompile() throws Exception {
73 Path dir = createTempDir("testCompile");
74 Path output = dir.resolve("testRules.txt");
75 try (InputStream input = getClass().getResourceAsStream("testRules.txt")) {
76 Files.copy(input, output);
77 }
78 String path = output.toAbsolutePath().toString();
79 Compile.main(new String[] {"test", path});
80 Path compiled = dir.resolve("testRules.txt.out");
81 Trie trie = loadTrie(compiled);
82 assertTrie(trie, output, true, true);
83 assertTrie(trie, output, false, true);
84 }
85
86 public void testCompileBackwards() throws Exception {
87 Path dir = createTempDir("testCompile");
88 Path output = dir.resolve("testRules.txt");
89 try (InputStream input = getClass().getResourceAsStream("testRules.txt")) {
90 Files.copy(input, output);
91 }
92 String path = output.toAbsolutePath().toString();
93 Compile.main(new String[] {"-test", path});
94 Path compiled = dir.resolve("testRules.txt.out");
95 Trie trie = loadTrie(compiled);
96 assertTrie(trie, output, true, true);
97 assertTrie(trie, output, false, true);
98 }
99
100 public void testCompileMulti() throws Exception {
101 Path dir = createTempDir("testCompile");
102 Path output = dir.resolve("testRules.txt");
103 try (InputStream input = getClass().getResourceAsStream("testRules.txt")) {
104 Files.copy(input, output);
105 }
106 String path = output.toAbsolutePath().toString();
107 Compile.main(new String[] {"Mtest", path});
108 Path compiled = dir.resolve("testRules.txt.out");
109 Trie trie = loadTrie(compiled);
110 assertTrie(trie, output, true, true);
111 assertTrie(trie, output, false, true);
112 }
113
114 static Trie loadTrie(Path path) throws IOException {
115 Trie trie;
116 DataInputStream is = new DataInputStream(new BufferedInputStream(Files.newInputStream(path)));
117 String method = is.readUTF().toUpperCase(Locale.ROOT);
118 if (method.indexOf('M') < 0) {
119 trie = new Trie(is);
120 } else {
121 trie = new MultiTrie(is);
122 }
123 is.close();
124 return trie;
125 }
126
127 private static void assertTrie(Trie trie, Path file, boolean usefull, boolean storeorig)
128 throws Exception {
129 LineNumberReader in =
130 new LineNumberReader(Files.newBufferedReader(file, StandardCharsets.UTF_8));
131
132 for (String line = in.readLine(); line != null; line = in.readLine()) {
133 try {
134 line = line.toLowerCase(Locale.ROOT);
135 StringTokenizer st = new StringTokenizer(line);
136 String stem = st.nextToken();
137 if (storeorig) {
138 CharSequence cmd = (usefull) ? trie.getFully(stem) : trie.getLastOnPath(stem);
139 StringBuilder stm = new StringBuilder(stem);
140 Diff.apply(stm, cmd);
141 assertEquals(stem.toLowerCase(Locale.ROOT), stm.toString().toLowerCase(Locale.ROOT));
142 }
143 while (st.hasMoreTokens()) {
144 String token = st.nextToken();
145 if (token.equals(stem)) {
146 continue;
147 }
148 CharSequence cmd = (usefull) ? trie.getFully(token) : trie.getLastOnPath(token);
149 StringBuilder stm = new StringBuilder(token);
150 Diff.apply(stm, cmd);
151 assertEquals(stem.toLowerCase(Locale.ROOT), stm.toString().toLowerCase(Locale.ROOT));
152 }
153 } catch (
154 @SuppressWarnings("unused")
155 java.util.NoSuchElementException x) {
156 // no base token (stem) on a line
157 }
158 }
159
160 in.close();
161 }
162 }