package org.elasticsearch.index.analysis;

import com.chenlb.mmseg4j.*;
import com.chenlb.mmseg4j.analysis.MMSegTokenizer;
import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettings;

import java.io.File;
import java.io.Reader;

/**
 * Created by IntelliJ IDEA.
 * User: Medcl'
 * Date: 12-6-6
 * Time: 下午3:59
 */
public class MMsegTokenizerFactory extends AbstractTokenizerFactory {

    private Settings settings;
    Seg seg_method;
    private String seg_type;

    @Inject
    public MMsegTokenizerFactory(Index index, @IndexSettings Settings indexSettings,Environment env, @Assisted String name, @Assisted Settings settings) {
        super(index, indexSettings, name, settings);
        this.settings=settings;

        String path=new File(env.configFile(),"mmseg").getPath();
        logger.info(path);
        Dictionary dic = Dictionary.getInstance(path);
        seg_type = settings.get("seg_type", "max_word");
        if(seg_type.equals("max_word")){
            seg_method = new MaxWordSeg(dic);
        }else if(seg_type.equals("complex")){
            seg_method = new ComplexSeg(dic);
        }else if(seg_type.equals("simple")){
            seg_method =new SimpleSeg(dic);
        }

    }

    @Override
    public Tokenizer create(Reader reader) {
        logger.info(seg_type);
        logger.info(seg_method.toString());
        return  new MMSegTokenizer(seg_method,reader);
    }
}

@@ -723,22 +723,22 @@
     
-Seg seg_method
+Dictionary dic
 ;%0D%0A 
@@ -1129,27 +1129,16 @@
         
-Dictionary 
 dic = Di
@@ -1220,24 +1220,156 @@
 ax_word%22);%0D%0A
+    %7D%0D%0A%0D%0A    @Override%0D%0A    public Tokenizer create(Reader reader) %7B%0D%0A        logger.info(seg_type);%0D%0A        Seg seg_method=null;%0D%0A
         if(s
@@ -1622,32 +1622,32 @@
 impleSeg(dic);%0D%0A
+
         %7D%0D%0A%0D%0A   
@@ -1644,112 +1644,8 @@
   %7D%0D
-%0A%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public Tokenizer create(Reader reader) %7B%0D%0A        logger.info(seg_type);%0D
 %0A   

fix bug for concurrent tokenizing with a single Seg instance