Skip to content

Instantly share code, notes, and snippets.

@gupul2k
Last active December 13, 2015 20:39
Show Gist options
  • Save gupul2k/4971473 to your computer and use it in GitHub Desktop.
Save gupul2k/4971473 to your computer and use it in GitHub Desktop.
Simple Parser to read SMO Output on multiple class [Ex: 100k lines of file] and splits into NC2 = n!/(n-2)!*2!, those many text files. <br> Input: SMO Output in a text file <br> Output: separated binary class weights into corresponding bucket [file names signify]
/* @(#) SeparateSMOBinaryOpsToFiles.java 1.00 2/17/2013
*
* [Copyright Information]
*/
/*
* Revision History:
* Revision Version Project Change Date Author Description
* No. No. Code Req. no.
* 1 1 Sobhan H
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* Class Name : This class reads an SMO ouput of many binary class results and splits into separate text files.
*
* Classes Used : Features
*/
public class SeparateSMOBinaryOpsToFiles {
public static void main(String argv[]) throws FileNotFoundException,
IOException{
String input_Filename = null;
String temp_Inp_Str = null;
try {
if(argv.length != 1) {
System.out.println("Please Provide Parameters: java jclasses.SeparateSMOBinaryOpsToFiles <SMO Output file> ");
return;
}
//Read text file specified and then print to the stuff to required file
input_Filename = argv[0];
FileReader ipFile;
BufferedReader fileIP;
ArrayList bcList = null;
bcList = collectAllBinaryClasses(input_Filename);
//Read for BC Classes in List
for (int one_Class=0; one_Class< bcList.size(); one_Class++) {
ipFile = new FileReader(input_Filename);
fileIP = new BufferedReader(ipFile);
String one_bc_Class_str = (String)bcList.get(one_Class);
String tmp_Str = "";
String strOldLines ="";
System.out.println(one_bc_Class_str);
while ((temp_Inp_Str = fileIP.readLine()) != null) {
if(temp_Inp_Str.length() > 0) {
//Check match for one Binary Combination
if(one_bc_Class_str.compareTo(temp_Inp_Str) == 0) {
//Once matches start reading the lines
while ((tmp_Str = fileIP.readLine()) != null) {
if (tmp_Str.indexOf("Classifier for classes:")== -1) {
strOldLines = strOldLines + tmp_Str;
strOldLines = strOldLines + "\n";
} else {
break;
}
}
}
}
}
one_bc_Class_str = one_bc_Class_str.substring(one_bc_Class_str.indexOf(": ")+1, one_bc_Class_str.length()).trim();
one_bc_Class_str = one_bc_Class_str.replace(", ", "_");
//System.out.println(one_bc_Class_str);
String op_FileName = "./output/"+one_bc_Class_str+".txt";
File outputFile = new File(op_FileName);
FileOutputStream outstream = new FileOutputStream(outputFile, true);
printToInputFile(outstream, strOldLines);
}//End for on all binary combinations
} catch(FileNotFoundException fe) {
fe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* This method collects All Binary Classes an ArrayList.
*@param: String SMO O/p
*@return ArrayList
*/
public static ArrayList collectAllBinaryClasses(String smo_Filename) throws IOException {
ArrayList returnBC_List = new ArrayList();
FileReader bc_FileReader = new FileReader(smo_Filename);
BufferedReader bc_Buff_Rdr = new BufferedReader(bc_FileReader);
String temp_Str = null;
try {
while ((temp_Str = bc_Buff_Rdr.readLine()) != null) {
if(temp_Str.length() > 0) {
if(temp_Str.startsWith("Classifier for classes:")) {
returnBC_List.add(temp_Str.trim());
}
}
}
} catch(FileNotFoundException fe) {
fe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return returnBC_List;
}
/**
* This method print to file.
*@param: String SMO O/p
*@return void
*/
public static void printToInputFile(FileOutputStream outstream,
String content_To_Print) throws java.io.FileNotFoundException,
IOException {
//Writing to File
byte[] buffer = new byte[8192];
String newLine = "\n";
byte[] bufferNewLine = new byte[1];
try {
buffer = content_To_Print.getBytes();
outstream.write(buffer);
bufferNewLine = newLine.getBytes();
outstream.write(bufferNewLine);
outstream.flush();
//outstream.close();
} catch(FileNotFoundException fe) {
fe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}//class ends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment