📄 assocrulemining.java
字号:
@param fName the given file name. */
protected void readOutputSchema(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) {
outputSchema[rowIndex] = line;
// Increment row index in output schema array
rowIndex++;
// get next line
line = fileInput.readLine();
}
// Close file
closeFile();
}
/** Check if number of attributes in output schema are same as number of
attributes in input file. <P>If either the output schema or the input
file has nor been loaded then method will return false.
@return true if number of attributes are the same and false otherwise. */
public boolean checkSchemaVdata() {
boolean schemaAndDataAttsSame = true;
// Check schema
if (outputSchema==null) {
JOptionPane.showMessageDialog(null,"No output schema file.",
"CHECK SCHEMA v DATA ATTRIBUTES ERROR",
JOptionPane.ERROR_MESSAGE);
return(!schemaAndDataAttsSame);
}
// Check data array
if (dataArray==null) {
JOptionPane.showMessageDialog(null,"No input data file.",
"CHECK SCHEMA v DATA ATTRIBUTES ERROR",
JOptionPane.ERROR_MESSAGE);
return(!schemaAndDataAttsSame);
}
// Check lengths.
if (outputSchema.length==numCols) return(schemaAndDataAttsSame);
else {
JOptionPane.showMessageDialog(null,"Number of attributes in " +
"schema file (" + outputSchema.length + ") not\n" +
"same as number of attributes in data file (" +
numCols + ")\n","CHECK SCHEMA v DATA ATTRIBUTES ERROR",
JOptionPane.ERROR_MESSAGE);
return(!schemaAndDataAttsSame);
}
}
/* ---------------------------------------------------------------- */
/* */
/* READ INPUT DATA FROM FILE (GUI VERSIONS) */
/* */
/* ---------------------------------------------------------------- */
/* INPUT DATA SET */
/** Commences process of getting input data.
@param textArea the text area in the GUI used for output.
@param fName the name of the input file to be read. */
public void inputDataSet(JTextArea textArea, File fName) {
// Set filePath instance field
filePath = fName;
// Read the file
readFile(textArea);
// Check ordering (only if input format is OK)
if (inputFormatOkFlag) {
if (checkOrdering()) {
// Output to text area
textArea.append("Number of records = " + numRows + "\n");
countNumCols();
textArea.append("Number of columns = " + numCols + "\n");
// Set have data flag to true
haveDataFlag = true;
}
else {
// Set have data flag to false
haveDataFlag = false;
// Set inputFormatOkFlag to true by default for next input
// file
inputFormatOkFlag = true;
textArea.append("Error reading file: " + filePath + "\n\n");
}
}
}
/* READ FILE */
/** Reads input data from file specified in command line argument. <P>
Proceeds as follows:
<OL>
<LI>Gets number of lines in file, checking format of each line (space
separated integers), if incorrectly formatted line found
<TT>inputFormatOkFlag</TT> set to <TT>false</TT>.
<LI>Diminsions input array.
<LI>Reads data
</OL>
@param textArea the text area in the gui used for output. */
public void readFile(JTextArea textArea) {
try {
// Dimension data structure
inputFormatOkFlag=true;
numRows = getNumberOfLines(fileName);
if (inputFormatOkFlag) {
dataArray = new short[numRows][];
// Read file
textArea.append("Reading input file:\n" + filePath + "\n");
readInputDataSet();
// Set have data flag to true
haveDataFlag = true;
}
else {
// Set have data flag to false
haveDataFlag = false;
textArea.append("Error reading file:\n" + filePath + "\n\n");
}
}
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 false
haveDataFlag = false;
}
}
/* ---------------------------------------------------------------- */
/* */
/* REORDER DATA SET ACCORDING TO ATTRIBUTE FREQUENCY */
/* */
/* ---------------------------------------------------------------- */
/* REORDER INPUT DATA: */
/** Reorders input data according to frequency of single attributes. <P>
Example, given the data set:
<PRE>
1 2 5
1 2 3
2 4 5
1 2 5
2 3 5
</PRE>
This would produce a countArray (ignore index 0 because there is no
attributr number 0):
<PRE>
+---+---+---+---+---+---+
| | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
| | 3 | 5 | 2 | 1 | 4 |
+---+---+---+---+---+---+
</PRE>
Which sorts to:
<PRE>
+---+---+---+---+---+---+
| | 2 | 5 | 1 | 3 | 4 |
+---+---+---+---+---+---+
| | 5 | 4 | 3 | 2 | 1 |
+---+---+---+---+---+---+
</PRE>
Giving rise to the conversion Array of the form (no index 0):
<PRE>
+---+---+---+---+---+---+
| | 3 | 1 | 4 | 5 | 2 |
+---+---+---+---+---+---+
| | 3 | 5 | 2 | 1 | 4 |
+---+---+---+---+---+---+
</PRE>
Note that the first row gives the new attribute number (old attribute
number is the index). The second row here are the counts used to identify
the ordering but which now no longer play a role in the conversion
exercise. Thus the new column (attriburte) number for column/attribute 1 is
column 3 (i.e. the first vale at index 1). The reconversion array will be
of the form (values are the indexes from the conversion array while indexes
represent the first vlaue from the conversion array):
<PRE>
+---+---+---+---+---+---+
| | 2 | 5 | 1 | 3 | 4 |
+---+---+---+---+---+---+
</PRE>
For example to convert the attribute number 3 back to its original number
we look up the value at index 3.
*/
public void idInputDataOrdering() {
// Count singles and store in countArray;
int[][] countArray = countSingles();
// Bubble sort count array on support value (second index)
orderCountArray(countArray);
// Define conversion and reconversion arrays
defConvertArrays(countArray);
// Set sorted flag
isOrderedFlag = true;
}
/* COUNT SINGLES */
/** Counts number of occurrences of each single attribute in the
input data.
@return 2-D array where first row represents column numbers
and second row represents support counts. */
protected int[][] countSingles() {
// Dimension and initialize count array
int[][] countArray = new int[numCols+1][2];
for (int index=0;index<countArray.length;index++) {
countArray[index][0] = index;
countArray[index][1] = 0;
}
// Step through input data array counting singles and incrementing
// appropriate element in the count array
for(int rowIndex=0;rowIndex<dataArray.length;rowIndex++) {
if (dataArray[rowIndex] != null) {
for (int colIndex=0;colIndex<dataArray[rowIndex].length;
colIndex++)
countArray[dataArray[rowIndex][colIndex]][1]++;
}
}
// Return
return(countArray);
}
/* ORDER COUNT ARRAY */
/** Bubble sorts count array produced by <TT>countSingles</TT> method
so that array is ordered according to frequency of single items.
@param countArray The 2-D array returned by the <TT>countSingles</TT>
method. */
private void orderCountArray(int[][] countArray) {
int attribute, quantity;
boolean isOrdered;
int index;
do {
isOrdered = true;
index = 1;
while (index < (countArray.length-1)) {
if (countArray[index][1] >= countArray[index+1][1]) index++;
else {
isOrdered=false;
// Swap
attribute = countArray[index][0];
quantity = countArray[index][1];
countArray[index][0] = countArray[index+1][0];
countArray[index][1] = countArray[index+1][1];
countArray[index+1][0] = attribute;
countArray[index+1][1] = quantity;
// Increment index
index++;
}
}
} while (isOrdered==false);
}
/* SORT FIRST N ELEMENTS IN COUNT ARRAY */
/** Bubble sorts first N elements in count array produced by
<TT>countSingles</TT> method so that array is ordered according to
frequency of single items. <P> Used when ordering classification input
data where we wish classes to be listed last.
@param countArray The 2-D array returned by the <TT>countSingles</TT>
method.
@param endIndex the index of the Nth element. */
protected void orderFirstNofCountArray(int[][] countArray, int endIndex) {
int attribute, quantity;
boolean isOrdered;
int index;
do {
isOrdered = true;
index = 1;
while (index < endIndex) {
if (countArray[index][1] >= countArray[index+1][1]) index++;
else {
isOrdered=false;
// Swap
attribute = countArray[index][0];
quantity = countArray[index][1];
countArray[index][0] = countArray[index+1][0];
countArray[index][1] = countArray[index+1][1];
countArray[index+1][0] = attribute;
countArray[index+1][1] = quantity;
// Increment index
index++;
}
}
} while (isOrdered==false);
}
/* DEFINE CONVERSION ARRAYS: */
/** Defines conversion and reconversion arrays.
@param countArray The 2-D array sorted by the <TT>orderCcountArray</TT>
method.*/
protected void defConvertArrays(int[][] countArray) {
// Dimension arrays
conversionArray = new int[numCols+1][2];
reconversionArray = new short[numCols+1];
// Assign values by processing the count array which has now been
// ordered.
for(int index=1;index<countArray.length;index++) {
conversionArray[countArray[index][0]][0] = index;
conversionArray[countArray[index][0]][1] = countArray[index][1];
reconversionArray[index] = (short) countArray[index][0];
}
// Diagnostic ouput if desired
//outputConversionArrays();
}
/* DEFINE CONVERSION ARRAYS: */
/** Defines conversion and reconversion arrays using only the first
N elements of the count array, last elements are not ordered.
@param countArray The 2-D array sorted by the <TT>orderCcountArray</TT>
method.
@param endIndex the index of the Nth element. */
protected void defConvertArrays(int[][] countArray, int endIndex) {
// Dimension arrays
conversionArray = new int[numCols+1][2];
reconversionArray = new short[numCols+1];
// Assign values by processing the count array which has now been
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -