package ciranda.features;

import ciranda.utils.CirandaUtils;
import edu.cmu.minorthird.classify.Feature;
import edu.cmu.minorthird.classify.Instance;
import edu.cmu.minorthird.classify.MutableInstance;
import edu.cmu.minorthird.util.LineProcessingUtil;

/**
 * @author vitor carvalho
 * March 2005
 *
 * Extracts features related to Propose act classification
 */
public class ProposeFE implements FeatureExtractor {
	private String body;
	private String subject;
	
	public ProposeFE(String mybody){
		body = mybody;
		subject = "";
	}
	
	public ProposeFE(String mybody, String subj){
		body = mybody;
		subject = subj;
	}
	
	public Instance string2Instance(){
		Instance ins_subj = new MutableInstance();		
		String pre_body = preProcessBody();
		Instance ins = extractBodyInstance(pre_body);
				
		//combining subject features
		String pre_subj = "";
		if((subject.length()>1)){
			pre_subj = preProcessSubj();
			ins_subj = extractSubjInstance(pre_subj);
			ins_subj = CirandaUtils.renameFeatures((MutableInstance)ins_subj,"subject_");
			CirandaUtils.addingInstances((MutableInstance)ins,(MutableInstance)ins_subj);
		}			
		return ins;
	}
	
    //	specific pre processing for request detection
	private Instance extractBodyInstance(String str){
//		MutableInstance ins = (MutableInstance)CirandaUtils.extract1g2g3g4g5g(str,true,true,true,true,true);
//		MutableInstance ins = (MutableInstance)CirandaUtils.extract1g2g3g4g5g(str,true,false,false,false,false);
		MutableInstance ins = (MutableInstance)CirandaUtils.extract1g2g3g4g5g(str,true,true,true,false,false);//default

		return ins;
	}

	//add specifics of subject processin here.
	private Instance extractSubjInstance(String str){
		Instance ins = CirandaUtils.extract1g2g3g4g5g(str,true,true,true,false,false);
		return ins;
	}


	//specific pre processing for request detection
	private String preProcessBody(){
		String tmp = body;
		tmp = CirandaUtils.removeFWDparts(tmp);//remove FWD lines of messages
		tmp = CirandaUtils.removeQuotes(tmp);//remove reply to lines
	//	tmp = removeStopWords(tmp); //remove some stop words

tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("\\?"," \\? ");
		tmp = tmp.replaceAll("\\s+"," ");
		tmp = tmp.replaceAll("\\n+"," ");
		tmp = tmp.replaceAll("\\-+","-");
		tmp = tmp.replaceAll("\\-+","-");
		
		tmp = tmp.replaceAll("\\d+","NUMBEX");
		tmp = tmp.replaceAll("NUMBEX\\:NUMBEX","HOREX ");
		tmp = tmp.replaceAll("NUMBEX[p|P|a|A][m|M]","NUMBEX PM ");
		tmp = tmp.replaceAll("HOREX [p|P|a|A][m|M]","HOREX PM ");
		tmp = tmp.replaceAll("HOREX  [p|P|a|A][m|M]","HOREX PM ");
		
		tmp = tmp.replaceAll("\\'m"," am");
		tmp = tmp.replaceAll("\\'ll"," will");
		tmp = tmp.replaceAll("\\'t"," not");
	tmp = tmp.replaceAll("[P|p]\\.[M|m]\\."," PM ");
	tmp = tmp.replaceAll(" [P|p][M|m]"," PM ");
	tmp = tmp.replaceAll("[A|a]\\.[M|m]\\."," PM ");
	tmp = tmp.replaceAll("\\.(doc|Doc|DOC|xls|txt|pdf|XLS|rtf|ppt|PPT)"," FILENAMEX ");
	tmp = tmp.replaceAll("([M|m]eetings)","meeting");
		tmp = tmp.replaceAll("\\?"," \\? ");
		tmp = tmp.replaceAll("\\!"," \\!");
		tmp = tmp.replaceAll("[e|E][\\-](mail|MAIL|Mail)","email");
		tmp = tmp.replaceAll("\\'d"," would");
		tmp = tmp.replaceAll("\\'re"," are");
		tmp = tmp.replaceAll("(YOUR|your|Your|Their|their)","IORRR");
		tmp = tmp.replaceAll("(YOU|you|You)","PPPEOPLE");
		tmp = tmp.replaceAll("(I|We|we|WE) ","PPPEOPLE ");
		tmp = tmp.replaceAll(" (ME|me|her|them|him)[ |\\.|\\,|\\;]"," MMEE ");
		tmp = tmp.replaceAll("[W|w](HERE|here|HEN|hen|HY|hy|HO|ho)","WWHH");
		tmp = tmp.replaceAll("[L|l][e|E][L|t]\\'[S|s]","LETS");
		tmp = tmp.replaceAll("[\\.|\\,|\\;|\\:]"," PUNCT ");
//		tmp = tmp.replaceAll("PUNCT","");//removing punctuation
		tmp = tmp.replaceAll("\\'s","");
//		tmp = tmp.replaceAll("(ASAP|asap|Asap)","ASAP");
		tmp = tmp.replaceAll("[A|a]s [S|s]oon [A|a]s [P|p]ossible","ASAP");
		tmp = tmp.replaceAll("(\\)|\\(|\\[|\\])","");
		tmp = tmp.replaceAll("\\\"","");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("([A|a]fter|[B|b]efore|during)","AAAFTER");
		tmp = tmp.replaceAll("([S|s]eptember|[M|m]onday|MONDAY|[T|t]uesday|TUESDAY|[W|w]ednesday|WEDNESDAY|[F|f]riday|FRIDAY|THURSDAY|[T|t]hursday|SATURDAY|[S|s]aturday|[S|s]unday|SUNDAY)","DDDAY");		
		tmp = tmp.replaceAll(" ([S|s]ep|[M|m]on|MON|[T|t]ue|TUE|[W|w]ed|WED|[F|f]ri|FRI|THU|[T|t]hu|SAT|[S|s]at) "," DDDAY ");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("  "," ");
		tmp = tmp.replaceAll("  "," ");

		return tmp;
	}

	//pick your flavor of stop words
	private String removeStopWords(String str){
		String tmp = str.replaceAll(" ([T|t]he|of|A|an|a|An) "," ");
		return tmp;
	}
	
	private String preProcessSubj(){
		String tmp = subject;
		tmp = tmp.replaceAll("[\\.|\\,|\\;|\\:]"," PUNCT ");
	//	tmp = removeStopWords(tmp);
		return tmp;
	}

	public static void main(String[] args) {
		ProposeFE rfe = new ProposeFE("vitor rocha de carvalho", "jacare jacare tartaruga");
		Instance e = rfe.string2Instance();
		System.out.println(e.toString());
	}
}
