Apache Solr open-source search software
LUCENE-1398: Add ReverseStringFilter to contrib/analyzers
Yonik Seeley6c176eb0168d
diff --git a/CHANGES.txt b/CHANGES.txtindex c81ebcfe374..f6bf570e8f0 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -159,6 +159,9 @@ New featuresbitset. Instead, match() is called on demand per docID. (JohnWang via Mike McCandless)+15. LUCENE-1398: Add ReverseStringFilter to contrib/analyzers, a filter+ to reverse the characters in each token. (Koji Sekiguchi via yonik)+Optimizations1. LUCENE-1427: Fixed QueryWrapperFilter to not waste time computingdiff --git a/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java b/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.javanew file mode 100644index 00000000000..6a07a70eba6--- /dev/null+++ b/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java@@ -0,0 +1,68 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.lucene.analysis.reverse;++import org.apache.lucene.analysis.TokenFilter;+import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.Token;++import java.io.IOException;++/**+ * Reverse token string e.g. "country" => "yrtnuoc".+ *+ * @version $Id$+ */+public final class ReverseStringFilter extends TokenFilter {++ public ReverseStringFilter(TokenStream in) {+ super(in);+ }++ public final Token next(Token in) throws IOException {+ assert in != null;+ Token token=input.next(in);+ if( token == null ) return null;+ reverse( token.termBuffer(), token.termLength() );+ return token;+ }++ public static String reverse( final String input ){+ char[] charInput = input.toCharArray();+ reverse( charInput );+ return new String( charInput );+ }++ public static void reverse( char[] buffer ){+ reverse( buffer, buffer.length );+ }++ public static void reverse( char[] buffer, int len ){+ reverse( buffer, 0, len );+ }++ public static void reverse( char[] buffer, int start, int len ){+ if( len <= 1 ) return;+ int num = len>>1;+ for( int i = start; i < ( start + num ); i++ ){+ char c = buffer[i];+ buffer[i] = buffer[start * 2 + len - i - 1];+ buffer[start * 2 + len - i - 1] = c;+ }+ }+}diff --git a/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java b/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.javanew file mode 100644index 00000000000..e13889e61c6--- /dev/null+++ b/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java@@ -0,0 +1,52 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.lucene.analysis.reverse;++import java.io.StringReader;++import org.apache.lucene.analysis.Token;+import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.WhitespaceTokenizer;+import org.apache.lucene.util.LuceneTestCase;++public class TestReverseStringFilter extends LuceneTestCase {+ public void testFilter() throws Exception {+ TokenStream stream = new WhitespaceTokenizer(+ new StringReader("Do have a nice day")); // 1-4 length string+ ReverseStringFilter filter = new ReverseStringFilter(stream);+ final Token reusableToken = new Token();+ assertEquals("oD", filter.next(reusableToken).term());+ assertEquals("evah", filter.next(reusableToken).term());+ assertEquals("a", filter.next(reusableToken).term());+ assertEquals("ecin", filter.next(reusableToken).term());+ assertEquals("yad", filter.next(reusableToken).term());+ assertNull(filter.next(reusableToken));+ }++ public void testReverseString() throws Exception {+ assertEquals( "A", ReverseStringFilter.reverse( "A" ) );+ assertEquals( "BA", ReverseStringFilter.reverse( "AB" ) );+ assertEquals( "CBA", ReverseStringFilter.reverse( "ABC" ) );+ }++ public void testReverseChar() throws Exception {+ char[] buffer = { 'A', 'B', 'C', 'D', 'E', 'F' };+ ReverseStringFilter.reverse( buffer, 2, 3 );+ assertEquals( "ABEDCF", new String( buffer ) );+ }+}