import FAUtil.FileLiner;
import FAUtil.Histogram;
import FAUtil.Util;


public class ProcessMLF {

    /**
     * @param args
     */
    public static void main(String[] args) {
	System.out.println("usage : ProcessMLF mlf-file out-file");
	FileLiner in = new FileLiner(args[0]);
	String line, lineID="", accumLine="",temp[];
	String outFile = args[1];
	//String alligned = "data/alignedPh-ID.txt";
	Histogram phoneSet = new Histogram();
	//Util.delete(alligned);
	Util.delete(outFile);

	while((line = in.getLine() ) != null){
	   
	    line = line.replaceAll("ch\\-ih\\+k", "ih");
	    line = line.replaceAll("t\\+ae", "t");
	    line = line.replaceAll("ae\\-ch\\+ax", "ch");
	    line = line.replaceAll("ax", "ah");
	    line = line.replaceAll("ix", "ih");
	    line = line.replaceAll("dx", "dh");
	    
	    temp = line.split(" ");
	    //	    System.out.println(line);
	    if(line.startsWith("\"")){
		lineID = line.substring(line.indexOf("/")+1, line.indexOf("."));
		//Util.append2File(alligned,lineID+"\n");
	    }
	    if(line.startsWith(".")){
		Util.append2File(outFile,lineID+"\t"+accumLine.trim()+"\n");
		lineID ="";
		accumLine ="";
	    }
	    else if(!line.startsWith("\"") && 
		    !line.startsWith("#") && 
		    !temp[2].equals("sp")) {
		//System.out.println(line);
		if(temp.length == 4)
		    {
			accumLine += temp[2] +" ";
			//			phoneSet.put(temp[2]);
		    }
		
		else if(!temp[4].equals("SENT-END"))
		    accumLine = accumLine.trim() + "\t" + temp[2] + " ";
	    }
	}
	//	phoneSet.writeFile("phoneme/VOXphoneSet");
    }
}
