package org.lionsoul.jcseg.analyzer.v5x;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Tokenizer;
import org.lionsoul.jcseg.analyzer.v5x.JcsegFilter;
import org.lionsoul.jcseg.tokenizer.core.ADictionary;
import org.lionsoul.jcseg.tokenizer.core.DictionaryFactory;
import org.lionsoul.jcseg.tokenizer.core.JcsegException;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;

/**
 * jcseg analyzer for lucene with version on or after 5.0
 * 
 * @author    chenxin<chenxin619315@gmail.com>
 */
public class JcsegAnalyzer5X extends Analyzer
{
    private int mode;
    private JcsegTaskConfig config = null;
    private ADictionary dic = null;
    
    public JcsegAnalyzer5X( int mode ) 
    {
        this.mode = mode;
        
        //initialize the task config and the dictionary
        config = new JcsegTaskConfig();
        dic = DictionaryFactory.createSingletonDictionary(config);
    }
    
    public JcsegAnalyzer5X( int mode, String proFile )
    {
        this.mode = mode;
        
        config = new JcsegTaskConfig(proFile);
        dic = DictionaryFactory.createSingletonDictionary(config);
    }
    
    public void setConfig( JcsegTaskConfig config ) 
    {
        this.config = config;
    }
    
    public void setDict( ADictionary dic )
    {
        this.dic = dic;
    }
    
    public JcsegTaskConfig getTaskConfig()
    {
        return config;
    }
    
    public ADictionary getDict()
    {
        return dic;
    }

    @Override
    protected TokenStreamComponents createComponents(String fieldName) 
    {
        try {
            Tokenizer tokenizer = new JcsegTokenizer(mode, config, dic);
            return new TokenStreamComponents(tokenizer, new JcsegFilter(tokenizer));
        } catch (JcsegException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return null;
    }
}

@@ -702,24 +702,257 @@
  null;%0A    %0A
+    /**%0A     * initialize the analyzer with the specifield mode%0A     * And jcseg will look for the default configuration file%0A     * %0A     * @param   mode tokenizer mode%0A     * @see     org.lionsoul.jcseg.core.JcsegTaskConfig%0A    */%0A
     public J
@@ -997,34 +997,53 @@
     this
-.
+(
 mode
- = mode
+, new JcsegTaskConfig())
 ;%0A    
+%7D%0A
     %0A   
@@ -1047,14 +1047,19 @@
     
+/**%0A
     
-//
+ * 
 init
@@ -1073,55 +1073,266 @@
 the 
-task config and the dictionary
+analyzer with the specifield mode%0A     *  and the jcseg.properties file %0A     *  %0A     * @param   mode tokenizer mode%0A     * @param   proFile path of jcseg.properties file
 %0A    
+*/%0A
     
-config =
+public JcsegAnalyzer5X( int mode, String proFile )%0A    %7B%0A        this(mode,
  new
@@ -1352,88 +1352,185 @@
 fig(
+proFile)
 );%0A    
+%7D%0A
     
-dic = DictionaryFactory.createSingletonDictionary(config);%0A    %7D
+%0A    /**%0A     * initialize the analyzer with the specifield mode and configuration%0A     * %0A     * @param   mode tokenizer mode%0A     * @param   config
 %0A    
+*/
 %0A   
@@ -1545,33 +1545,32 @@
 JcsegAnalyzer5X(
- 
 int mode, String
@@ -1559,39 +1559,46 @@
 X(int mode, 
-String proFile 
+JcsegTaskConfig config
 )%0A    %7B%0A    
@@ -1611,16 +1611,18 @@
 is.mode 
+  
 = mode;%0A
@@ -1633,67 +1633,49 @@
     
-%0A        config = new JcsegTaskConfig(proFile);%0A        dic
+this.config = config;%0A        this.dic   
  = D

add Analyzer5x#(mode, JcsegTaskConfig) interface to make the analyzer more flexible