lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r747915 - in /lucene/java/trunk: ./ contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/
Date Wed, 25 Feb 2009 20:44:05 GMT
Author: yonik
Date: Wed Feb 25 20:44:05 2009
New Revision: 747915

URL: http://svn.apache.org/viewvc?rev=747915&view=rev
Log:
LUCENE-1398: Add ReverseStringFilter to contrib/analyzers

Added:
    lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/
    lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
  (with props)
    lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/
    lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
  (with props)
Modified:
    lucene/java/trunk/CHANGES.txt

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=747915&r1=747914&r2=747915&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Wed Feb 25 20:44:05 2009
@@ -159,6 +159,9 @@
     bitset.  Instead, match() is called on demand per docID.  (John
     Wang via Mike McCandless)
 
+15. LUCENE-1398: Add ReverseStringFilter to contrib/analyzers, a filter
+    to reverse the characters in each token.  (Koji Sekiguchi via yonik)
+
 Optimizations
 
  1. LUCENE-1427: Fixed QueryWrapperFilter to not waste time computing

Added: lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java?rev=747915&view=auto
==============================================================================
--- lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
(added)
+++ lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
Wed Feb 25 20:44:05 2009
@@ -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;
+    }
+  }
+}

Propchange: lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/reverse/ReverseStringFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java?rev=747915&view=auto
==============================================================================
--- lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
(added)
+++ lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
Wed Feb 25 20:44:05 2009
@@ -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 ) );
+  }
+}

Propchange: lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/reverse/TestReverseStringFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



Mime
View raw message