📄 assocrulemining.java
字号:
// 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 LINE */
/** Check whether given line from input file is of appropriate format
(space separated integers), if incorrectly formatted line found
<TT>inputFormatOkFlag</TT> set to <TT>false</TT>.
@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 <str.length();index++) {
if (!Character.isDigit(str.charAt(index)) &&
!Character.isWhitespace(str.charAt(index))) {
JOptionPane.showMessageDialog(null,"Character on line " +
counter + " is not a digit or white space",
"FILE INPUT ERROR",JOptionPane.ERROR_MESSAGE);
inputFormatOkFlag = false;
haveDataFlag = false;
break;
}
}
}
/* READ INPUT DATA SET */
/** Reads input data from file specified in command line argument. */
public void readInputDataSet() throws IOException {
readInputDataSet(fileName);
}
/* READ INPUT DATA SET */
/** 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();
// Process 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();
}
/* READ INPUT DATA SEGMENT */
/** Reads input data segment from a given file and places content into to
the data array structure commencing at the given row index, continues until
the end index is rerached.
@param fName the given file name.
@param startRowIndex the given row strat index.
@param endRowIndex the given row end index. */
protected void readInputDataSetSeg(String fName, int startRowIndex,
int endRowIndex) throws IOException {
int rowIndex=startRowIndex;
// Open the file
if (filePath==null) openFileName(fName);
else openFilePath();
// get first row.
String line = fileInput.readLine();
for (int index=startRowIndex;index<endRowIndex;index++) {
// Process line
processInputLine(line,index);
// get next line
line = fileInput.readLine();
}
// Close file
closeFile();
}
/* PROCESS INPUT LINE */
/** Processes a line from the input file and places it in the
<TT>dataArray</TT> structure.
@param line the line to be processed from the input file
@param rowIndex the index to the current location in the
<TT>dataArray</TT> structure.
@return 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<codeLength;colIndex++)
dataArray[rowIndex][colIndex] = code[colIndex];
// Return
return(true);
}
/* CHECK DATASET ORDERING */
/** 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<dataArray.length;index++) {
if (!checkLineOrdering(index+1,dataArray[index])) {
haveDataFlag = false;
result=false;
}
}
// Return
return(result);
}
/* CHECK LINE ORDERING */
/** 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<itemSet.length-1;index++) {
if (itemSet[index] >= itemSet[index+1]) {
JOptionPane.showMessageDialog(null,"Attribute data in line " +
lineNum + " not in numeric order",
"FILE INPUT ERROR",JOptionPane.ERROR_MESSAGE);
return(false);
}
}
// Default return
return(true);
}
/* COUNT NUMBER OF COLUMNS */
/** Counts number of columns represented by input data. */
protected void countNumCols() {
int maxAttribute=0;
// Loop through data array
for(int index=0;index<dataArray.length;index++) {
int lastIndex = dataArray[index].length-1;
if (dataArray[index][lastIndex] > maxAttribute)
maxAttribute = dataArray[index][lastIndex];
}
numCols = maxAttribute;
numOneItemSets = numCols; // default value only
}
/* OPEN FILE NAME */
/** Opens input file using fileName (instance field).
@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) {
JOptionPane.showMessageDialog(null,"Error Opening File \"" +
nameOfFile + "\"","FILE INPUT ERROR",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
/* OPEN FILE PATH */
/** Opens file using filePath (instance field). */
protected void openFilePath() {
try {
// Open file
FileReader file = new FileReader(filePath);
fileInput = new BufferedReader(file);
}
catch(IOException ioException) {
JOptionPane.showMessageDialog(null,"Error Opening File \"" +
filePath + "\"","FILE INPUT ERROR",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
/* CLOSE FILE */
/** Close file fileName (instance field). */
protected void closeFile() {
if (fileInput != null) {
try {
fileInput.close();
}
catch (IOException ioException) {
JOptionPane.showMessageDialog(null,"Error Closeing File",
"FILE ERROR",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
}
/* BINARY CONVERSION. */
/** Produce an item set (array of elements) 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);
}
/* ------------------------------------------------------ */
/* */
/* OUTPUT SCHEMA METHODS (GUI VERSIONS) */
/* */
/* ------------------------------------------------------ */
/* Colection of methods concerned wioth the loading and checking of output
schema file. The output schema is used in relation to outputting attributes
as lables rather than attribute numbers. Currently (May 2006) this
fuctionality is only available with respect to the various ARM and CARM GUI
interfaces that have been developed by the LUCS team. */
/* INPUT OUTPUT SCHEMA. */
/** Commences process of reading indicated output schema file and
stores to 1-D array.
@param textArea the text area in the GUI used for output.
@param fName the name of the input file to be read. */
public void inputOutputSchema(JTextArea textArea, File fName) {
// Set filePath instance field
filePath = fName;
// Read the file
try {
// Dimension data structure
numRowsInOutputSchema = getNumLinesInOutputSchema(null);
textArea.append("Number of lines (attributes) in output schema " +
"file = " + numRowsInOutputSchema + "\n");
outputSchema = new String[numRowsInOutputSchema];
// Read file
readOutputSchema();
// Set have data flag to true
hasOutputSchemaFlag = true;
}
catch(IOException ioException) {
JOptionPane.showMessageDialog(null,"Error reading File",
"FILE INPUT ERROR",JOptionPane.ERROR_MESSAGE);
textArea.append("Error reading File\n");
closeFile();
// Set have data flag to true
hasOutputSchemaFlag = false;
}
}
/* GET NUMBER OF LINES IN OUTPUT SCHEMA. */
/** Gets the number of lines (attributes) in the output schema file. <P>
Similar to getNmberOfLines method above but without line checking.
@param fName the name of the output schema file.
@return the number of lines (attributes) in the output schema. */
protected int getNumLinesInOutputSchema(String fName) throws IOException {
int rowIndex=0;
// Open the file
if (filePath==null) openFileName(fName);
else openFilePath();
// Get first row.
String line = fileInput.readLine();
// Process rest of file
while (line != null) {
// Increment row index in output schema array
rowIndex++;
// get next line
line = fileInput.readLine();
}
// Close file and returm
closeFile();
return(rowIndex);
}
/* READ OUTPUT SCHEMA */
/** Reads output schema from file. */
public void readOutputSchema() throws IOException {
readOutputSchema(fileName);
}
/* READ OUTPUT SCEMA */
/** Reads outpur schema from given file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -