package de.arago.lucene.util;

import java.io.Reader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
import org.apache.lucene.analysis.ngram.NGramTokenizer;
import org.apache.lucene.util.Version;

/**
*
* @author vvoss
*/
final public class LowCaseAnalyzer extends Analyzer {
    private final int mingram;
    private final int maxgram;

    public LowCaseAnalyzer() {
        this(2, 4);
    }

    public LowCaseAnalyzer(int mingram, int maxgram) {
        this.mingram = mingram;
        this.maxgram = maxgram;
    }
    
    @Override
  protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
    Tokenizer source;
    TokenStream stream;
      
    if("_name_prefix".equals(fieldName)) {
        source = new NGramTokenizer(reader,mingram,maxgram);
        stream = new LowerCaseFilter(Version.LUCENE_43, source);
    } else if(fieldName.startsWith("_ngram_")) {
        source = new NGramTokenizer(reader,3,4);
        stream = null;
    } else {
        source = new WhitespaceTokenizer(Version.LUCENE_36,reader);
        stream = new LowerCaseFilter(Version.LUCENE_43,source);
    }
    
    return new TokenStreamComponents(source, stream);
  }

    /*@Override
    public TokenStream tokenStream(String string, Reader reader) {
        if("_name_prefix".equals(string)) {
            NGramTokenizer ngram = new NGramTokenizer(reader,mingram,maxgram);
            TokenStream stream = new LowerCaseFilter(Version.LUCENE_43,ngram);
            return stream;
        } else if(string.startsWith("_ngram_")) {
            NGramTokenizer ngram = new NGramTokenizer(reader,3,4);
            return ngram;
        } else {
            TokenStream stream = new WhitespaceTokenizer(Version.LUCENE_43,reader);
            stream = new LowerCaseFilter(Version.LUCENE_43,stream);
            return stream;
        }
    }*/

}
@@ -175,32 +175,88 @@
 ysis.Tokenizer;%0A
+import org.apache.lucene.analysis.core.LetterTokenizer;%0A
 import org.apach
@@ -347,32 +347,101 @@
 spaceTokenizer;%0A
+import org.apache.lucene.analysis.miscellaneous.WordDelimiterFilter;%0A
 import org.apach
@@ -472,32 +472,85 @@
 NGramTokenizer;%0A
+import org.apache.lucene.analysis.util.CharArraySet;%0A
 import org.apach
@@ -1173,32 +1173,56 @@
    stream = new 
+WordDelimiterFilter(new 
 LowerCaseFilter(
@@ -1243,16 +1243,70 @@
 _43,
- 
 source)
+, WordDelimiterFilter.ALPHANUM, CharArraySet.EMPTY_SET)
 ;%0A  
@@ -1497,10 +1497,10 @@
 ENE_
+4
 3
-6
 ,rea
@@ -1518,32 +1518,56 @@
    stream = new 
+WordDelimiterFilter(new 
 LowerCaseFilter(
@@ -1591,16 +1591,71 @@
 ,source)
+, WordDelimiterFilter.ALPHANUM, CharArraySet.EMPTY_SET)
 ;%0A    %7D%0A

allow only alphanum in ngrams