lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject svn commit: r168029 - /lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
Date Wed, 04 May 2005 00:24:17 GMT
Author: ehatcher
Date: Tue May  3 17:24:17 2005
New Revision: 168029

URL: http://svn.apache.org/viewcvs?rev=168029&view=rev
Log:
Wolfgang is non-stop with the additions.  Easy enough to paste in, so here it is with a Collection-based
TokenStream

Modified:
    lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java

Modified: lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java?rev=168029&r1=168028&r2=168029&view=diff
==============================================================================
--- lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
(original)
+++ lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
Tue May  3 17:24:17 2005
@@ -431,7 +431,42 @@
 		if (size > 1) Arrays.sort(entries, termComparator);
 		return entries;
 	}
-	
+
+  /**
+  * Convenience method; Creates and returns a token stream that generates a
+  * token for each keyword in the given collection, "as is", without any
+  * transforming text analysis. The resulting token stream can be fed into
+  * {@link #addField(String, TokenStream)}, perhaps wrapped into another
+  * {@link org.apache.lucene.analysis.TokenFilter}, as desired.
+  *
+  * @param keywords
+  *            the keywords to generate tokens for
+  * @return the corresponding token stream
+  */
+  public TokenStream keywordTokenStream(final Collection keywords) {
+    if (keywords == null)
+      throw new IllegalArgumentException("keywords must not be null");
+
+    return new TokenStream() {
+      Iterator iter = keywords.iterator();
+      int pos = 0;
+      int start = 0;
+      public Token next() {
+        if (!iter.hasNext()) return null;
+
+        Object obj = iter.next();
+        if (obj == null)
+          throw new IllegalArgumentException("keyword must not be null");
+
+        String term = obj.toString();
+        Token token = new Token(term, start, start + term.length());
+        start += term.length() + 1; // separate words by 1 (blank) character
+        pos++;
+        return token;
+      }
+    };
+  }
+
 	/** Returns a String representation of the index data for debugging purposes. */
 	public String toString() {
 		StringBuffer result = new StringBuffer(256);		



Mime
View raw message