5:11 PM

/* CHECK FILE NAME */

protected void checkFileName() {
if (fileName == null) {
System.out.println("INPUT ERROR: Must specify file name (-F)");
errorFlag = false;
}
}

/* READ INPUT DATA FROM FILE */
public void inputDataSet() {
// Read the file
readFile();
// Check ordering (only if input format is OK)
if (inputFormatOkFlag) {
if (checkOrdering()) {
System.out.println("Number of records = " + numRows);
countNumCols();
System.out.println("Number of columns = " + numCols);
minSupport = (numRows * support)/100.0;
System.out.println("Min support = " +twoDecPlaces(minSupport) + " (records)");
}
else {
System.out.println("Error reading file: " + fileName + "\n");
closeFile();
System.exit(1);
}
}
}

/* Read file */
protected void readFile() {
try {
// Dimension data structure
inputFormatOkFlag=true;
numRows = getNumberOfLines(fileName);
if (inputFormatOkFlag) {
dataArray = new short[numRows][];
// Read file
System.out.println("Reading input file: " + fileName);
readInputDataSet();
}
else System.out.println("Error reading file: " + fileName + "\n");
}
catch(IOException ioException) {
System.out.println("Error reading File");
closeFile();
System.exit(1);
}
}

/** Gets number of lines in input file and checks format of each
line.
@param nameOfFile the filename of the file to be opened.
@return the number of rows in the given file. */

protected int getNumberOfLines(String nameOfFile) throws IOException {
int counter = 0;
// Open the file
if (filePath==null) openFileName(nameOfFile);
else openFilePath();
// Loop through file incrementing counter
// get first row.
String line = fileInput.readLine();
while (line != null) {
checkLine(counter+1,line);
StringTokenizer dataLine = new StringTokenizer(line);
int numberOfTokens = dataLine.countTokens();
if (numberOfTokens == 0) break;
counter++;
line = fileInput.readLine();
}
// Close file and return
closeFile();
return(counter);
}

/** Check whether given line from input file is of appropriate format
, if incorrectly formatted line found
inputFormatOkFlag set to false.
@param counter the line number in the input file.
@param str the current line from the input file. */
protected void checkLine(int counter, String str) {

for (int index=0;index if (!Character.isDigit(str.charAt(index)) && !Character.isWhitespace(str.charAt(index))) {
System.out.println("file input error"+" character on line " + counter +" is not a digit or white space");
inputFormatOkFlag = false;
haveDataFlag = false;
break;
}
}
}

/** Reads input data from file specified in command line argument. */
public void readInputDataSet() throws IOException {
readInputDataSet(fileName);
}

/** Reads input data from given file.
@param fName the given file name. */

protected void readInputDataSet(String fName) throws IOException {
int rowIndex=0;
// Open the file
if (filePath==null) openFileName(fName);
else openFilePath();
// Get first row.
String line = fileInput.readLine();
// Preocess rest of file
while (line != null) {
// Process line
if (!processInputLine(line,rowIndex)) break;
// Increment first (row) index in 2-D data array
rowIndex++;
// get next line
line = fileInput.readLine();
}
// Close file
closeFile();
}

/**Processes a line from the input file and places it in the
dataArray structure.
@param line the line to be processed from the input file
@param rowIndex the index to the current location in the
dataArray structure.
@rerturn true if successfull, false if empty record. */

private boolean processInputLine(String line, int rowIndex) {
// If no line return false
if (line==null) return(false);
// Tokenise line
StringTokenizer dataLine = new StringTokenizer(line);
int numberOfTokens = dataLine.countTokens();
// Empty line or end of file found, return false
if (numberOfTokens == 0) return(false);
// Convert input string to a sequence of short integers
short[] code = binConversion(dataLine,numberOfTokens);
// Dimension row in 2-D dataArray
int codeLength = code.length;
dataArray[rowIndex] = new short[codeLength];
// Assign to elements in row
for (int colIndex=0;colIndex // Return
return(true);
}

/** Checks that data set is ordered correctly.
@return true if appropriate ordering, false otherwise. */

protected boolean checkOrdering() {
boolean result = true;
// Loop through input data
for(int index=0;index if (!checkLineOrdering(index+1,dataArray[index])) {
haveDataFlag = false;
result=false;
}
}
// Return
return(result);
}

/** Checks whether a given line in the input data is in numeric sequence.
@param lineNum the line number.
@param itemSet the item set represented by the line
@return true if OK and false otherwise. */

protected boolean checkLineOrdering(int lineNum, short[] itemSet) {
for (int index=0;index if (itemSet[index] >= itemSet[index+1]) {
System.out.println("FILE FORMAT ERROR");
return(false);
}
}
// Default return
return(true);
}

/** Counts number of columns represented by input data. */
protected void countNumCols() {
int maxAttribute=0;
// Loop through data array
for(int index=0;index int lastIndex = dataArray[index].length-1;
if (dataArray[index][lastIndex] > maxAttribute)
maxAttribute = dataArray[index][lastIndex];
}
numCols = maxAttribute;
numOneItemSets = numCols; // default value only
}

/** Opens input file using fileName.
@param nameOfFile the filename of the file to be opened. */

protected void openFileName(String nameOfFile) {
try {
// Open file
FileReader file = new FileReader(nameOfFile);
fileInput = new BufferedReader(file);
}
catch(IOException ioException) {
System.out.println("Error Opening File");
System.exit(1);
}
}

/** Opens file using filePath . */

protected void openFilePath() {
try {
// Open file
FileReader file = new FileReader(filePath);
fileInput = new BufferedReader(file);
}
catch(IOException ioException) {
System.out.println("Error Opening File");
System.exit(1);
}
}

/** Close file fileName . */

protected void closeFile() {
if (fileInput != null) {
try {
fileInput.close();
}
catch (IOException ioException) {
System.out.println("Error closing File");
System.exit(1);
}
}
}

/** Produce an item set from input
line.
@param dataLine row from the input data file
@param numberOfTokens number of items in row
@return 1-D array of short integers representing attributes in input
row */

protected short[] binConversion(StringTokenizer dataLine,
int numberOfTokens) {
short number;
short[] newItemSet = null;
// Load array
for (int tokenCounter=0;tokenCounter < numberOfTokens;tokenCounter++) {
number = new Short(dataLine.nextToken()).shortValue();
newItemSet = realloc1(newItemSet,number);
}
// Return itemSet
return(newItemSet);
}

/** Reorders input data according to frequency of
single attributes.
*/

public void idInputDataOrdering() {

// Count singles and store in countArray;
int[][] countArray = countSingles();
// sort count array on support value (second index)
orderCountArray(countArray);
// Define conversion and reconversion arrays
defConvertArrays(countArray);
// Set sorted flag
isOrderedFlag = true;
}

0 comments: