/**
 * Created on Mar 20, 2005
 * @author Vitor R. Carvalho
 */
package ciranda.classify;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import ciranda.SpeechAct;

import edu.cmu.minorthird.classify.Classifier;
//import edu.cmu.minorthird.text.mixup.MixupProgramTest;
import edu.cmu.minorthird.util.IOUtil;

/**
 * Created on Mar 20, 2005
 * @author Vitor R. Carvalho
 * 
 * Factory class for all pre-trained classifiers. It loads from file the 
 * Request, Dlv, etc classifier models that were produced by SpeechActTrainer
 * 
 */
public class ModelFactory {

	private Map actmap = new HashMap();//hastable of act classifiers
	
	public ModelFactory(){;}	
	
	public Classifier getRequestClassifier(){
		return loadAct("Req");		
	}
	
	public Classifier getDeliveryClassifier(){
		return loadAct("Dlv");		
	}
	
	public Classifier getCommitClassifier(){
		return loadAct("Cmt");	
	}
	
	public Classifier getProposeClassifier(){
		return loadAct("Prop");		
	}
	
	public Classifier getReqAmdPropClassifier(){
		return loadAct("ReqAmdProp");		
	}
	
	public Classifier getDlvCmtClassifier(){
		return loadAct("DlvCmt");		
	}
	
	public Classifier getMeetClassifier(){
		return loadAct("Meet");
//                return loadAct("meet");
	}
	
	public Classifier getDdataClassifier(){
		return loadAct("Ddata");	
	}
	
	private Classifier loadAct(String act){
		if(actmap.containsKey(act)){	
			return (Classifier)actmap.get(act);
		}
		else{
//			loadModel(act);
			loadModel3(act);
			return (Classifier)actmap.get(act);
		}
	}
		 
	private void loadModel(String act){
		String filename = act+"_maxent50all5.amodel";
		String path = "models/";
//		String path = "";
		File tmpfile = new File(path+filename);
		try {
			Classifier model = (Classifier) IOUtil.loadSerialized(tmpfile);
			actmap.put(act,model);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 
	private void loadModel3(String act){
		String filename = "models/"+act+"_maxent50all5.amodel";

		try {
//			System.out.println("input "+filename);
			InputStream in = SpeechAct.class.getResourceAsStream(filename);
			Classifier model = (Classifier)IOUtil.loadSerialized(in);
			actmap.put(act,model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
