⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aprioritgui.java

📁 apriori算法java 带有详细注解
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    }
	}

    /* INPUT DATA SET */

    /** Reads input data from file specified in command line argument. */

    public void inputDataSet() throws IOException {
        int rowIndex=0;
	textArea.append("Reading input file\n" + fileName + "\n");

	// Open the file
	openFile();

	// get first row.
	String line = fileInput.readLine();
	while (line != null) {
	    StringTokenizer dataLine = new StringTokenizer(line);
            int numberOfTokens = dataLine.countTokens();
	    if (numberOfTokens == 0) break;
	    // Convert input string to a sequence of short integers
	    short[] code = binConversion(dataLine,numberOfTokens);
	    // Check for "null" input
	    if (code != null) {
	        // 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];
		}
	    else dataArray[rowIndex]= null;
	    // Increment first index in 2-D data array
	    rowIndex++;
	    // get next line
            line = fileInput.readLine();
	    }

	// Close file
	closeFile();
	}

    /* 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 */

    private 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);
	}

    /* CHECK DATASET ORDERING */

    /** Checks that data set is ordered correctly. */

    private boolean checkOrdering() {
        boolean result = true;

	// Loop through input data
	for(int index=0;index<dataArray.length;index++) {
	    if (!checkLineOrdering(index+1,dataArray[index])) result=false;
	    }

	// Return
	return(result);
	}

    private boolean checkLineOrdering(int lineNum, short[] itemSet) {
        for (int index=0;index<itemSet.length-1;index++) {
	    if (itemSet[index] >= itemSet[index+1]) {
	        JOptionPane.showMessageDialog(null,"FILE FORMAT ERROR:\n" +
	       		"Attribute data in line " + lineNum +
			" not in numeric order");
		return(false);
		}
	    }

	// Default return
	return(true);
	}

    /* COUNT NUMBER OF COLUMNS */
    private 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;
	}

    /* ------------------------------------------------- */
    /*                                                   */
    /*                   OUTPUT METHODS                  */
    /*                                                   */
    /* ------------------------------------------------- */

    /* OUTPUT DATA TABLE */

    /** Outputs stored input data set; initially read from input data file, but
    may be reirdered or pruned if desired by a particular application. */

    public void outputDataArray() {
        for(int index=0;index<dataArray.length;index++) {
	    outputItemSet(dataArray[index]);
	    textArea.append("\n");
	    }
	}

    /* OUTPUT ITEMSET */

    /** Outputs a given item set.
    @param itemSet the given item set. */

    protected void outputItemSet(short[] itemSet) {
	String itemSetStr = " {";

	// Loop through item set elements

	int counter = 0;
	for (int index=0;index<itemSet.length;index++) {
	    if (counter != 0) itemSetStr = itemSetStr + ",";
	    counter++;
	    itemSetStr = itemSetStr + itemSet[index];
	    }

	textArea.append(itemSetStr + "}");
	}

    /* OUTPUT FREQUENT SETS */

    /** Commences the process of outputting the frequent sets contained in
    the T-tree. */

    public void outputFrequentSets() {
	int number = 1;

	textArea.append("FREQUENT (LARGE) ITEM SETS (with support counts)\n" +
			"------------------------------------------------\n");

	// Loop

	short[] itemSetSofar = new short[1];
	for (int index=1; index <= numCols; index++) {
	    if (startTtreeRef[index] !=null) {
	        if (startTtreeRef[index].support >= minSupportRows) {
	            textArea.append("[" + number + "]  {" + index + "} = " +
		    			startTtreeRef[index].support + "\n");
	            itemSetSofar[0] = (short) index;
		    number = outputFrequentSets(number+1,itemSetSofar,
		    			index,startTtreeRef[index].childRef);
		    }
		}
	    }

	// End

	textArea.append("\n");
	}

    /** Outputs T-tree frequent sets. <P> Operates in a recursive manner.
    @param number the number of frequent sets so far.
    @param itemSetSofar the label for a T-treenode as generated sofar.
    @param size the length/size of the current array lavel in the T-tree.
    @param linkRef the reference to the current array lavel in the T-tree.
    @return the incremented (possibly) number the number of frequent sets so
    far. */

    private int outputFrequentSets(int number, short[] itemSetSofar, int size,
    							TtreeNode[] linkRef) {

	// No more nodes

	if (linkRef == null) return(number);

	// Otherwise process

	for (int index=1; index < size; index++) {
	    if (linkRef[index] != null) {
	        if (linkRef[index].support >= minSupportRows) {
		    short[] newItemSetSofar = realloc2(itemSetSofar,
		    				(short) index);
	            textArea.append("[" + number + "] ");
		    outputItemSet(newItemSetSofar);
		    textArea.append(" = " + linkRef[index].support + "\n");
	            number = outputFrequentSets(number + 1,newItemSetSofar,
		    			index,linkRef[index].childRef);
	            }
		}
	    }

	// Return

	return(number);
	}

    /* ------------------------------------------------------- */
    /*                                                         */
    /*                  FILE HANDLING UTILITIES                */
    /*                                                         */
    /* ------------------------------------------------------- */

    /* OPEN FILE */

    private void openFile() {
	try {
	    // Open file
	    FileReader file = new FileReader(fileName);
	    fileInput = new BufferedReader(file);
	    }
	catch(IOException ioException) {
	    JOptionPane.showMessageDialog(this,"Error Opening File",
			 "Error 4: ",JOptionPane.ERROR_MESSAGE);
	    }
	}

    /* CLOSE FILE */

    private void closeFile() {
        if (fileInput != null) {
	    try {
	    	fileInput.close();
		}
	    catch (IOException ioException) {
	        JOptionPane.showMessageDialog(this,"Error Opening File",
			 "Error 4: ",JOptionPane.ERROR_MESSAGE);
	        }
	    }
	}

    /* ------------------------------------------------------- */
    /*                                                         */
    /*                       ARM UTILITIES                     */
    /*                                                         */
    /* ------------------------------------------------------- */
    /* REALLOC 1 */

    /** Resizes given item set so that its length is increased by one
    and append new element
    @param oldItemSet the original item set
    @param newElement the new element/attribute to be appended
    @return the combined item set */

    protected short[] realloc1(short[] oldItemSet, short newElement) {

	// No old item set

	if (oldItemSet == null) {
	    short[] newItemSet = {newElement};
	    return(newItemSet);
	    }

	// Otherwise create new item set with length one greater than old
	// item set

	int oldItemSetLength = oldItemSet.length;
	short[] newItemSet = new short[oldItemSetLength+1];

	// Loop

	int index;
	for (index=0;index < oldItemSetLength;index++)
		newItemSet[index] = oldItemSet[index];
	newItemSet[index] = newElement;

	// Return new item set

	return(newItemSet);
	}

    /* APPEND */

    /** Concatinates two itemSets --- resizes given array so that its
    length is increased by size of second array and second array added.
    @param itemSet1 The first item set.
    @param itemSet2 The item set to be appended.
    @return the combined item set */

    protected short[] append(short[] itemSet1, short[] itemSet2) {

	// Test for emty sets, if found return other

	if (itemSet1 == null) return(copyItemSet(itemSet2));
	else if (itemSet2 == null) return(copyItemSet(itemSet1));

	// Create new array

	short[] newItemSet = new short[itemSet1.length+itemSet2.length];

	// Loop through itemSet 1

	int index1;
	for(index1=0;index1<itemSet1.length;index1++) {
	    newItemSet[index1]=itemSet1[index1];
	    }

	// Loop through itemSet 2

	for(int index2=0;index2<itemSet2.length;index2++) {
	    newItemSet[index1+index2]=itemSet2[index2];
	    }

	// Return

	return(newItemSet);
        }

    /* REALLOC 2 */

    /** Resizes given array so that its length is increased by one element
    and new element added to front
    @param oldItemSet the original item set
    @param newElement the new element/attribute to be appended
    @return the combined item set */

    protected short[] realloc2(short[] oldItemSet, short newElement) {

	// No old array

	if (oldItemSet == null) {
	    short[] newItemSet = {newElement};
	    return(newItemSet);
	    }

	// Otherwise create new array with length one greater than old array

	int oldItemSetLength = oldItemSet.length;
	short[] newItemSet = new short[oldItemSetLength+1];

	// Loop

	newItemSet[0] = newElement;
	for (int index=0;index < oldItemSetLength;index++)
		newItemSet[index+1] = oldItemSet[index];

	// Return new array

	return(newItemSet);
	}

    /* REMOVE FIRST N ELEMENTS */

    /** Removes the first n elements/attributes from the given item set.
    @param oldItemSet the given item set.
    @param n the number of leading elements to be removed.
    @return Revised item set with first n elements removed. */

    protected short[] removeFirstNelements(short[] oldItemSet, int n) {
        if (oldItemSet.length == n) return(null);
    	else {
	    short[] newItemSet = new short[oldItemSet.length-n];
	    for (int index=0;index<newItemSet.length;index++) {
	        newItemSet[index] = oldItemSet[index+n];
	        }
	    return(newItemSet);
	    }
	}

     /* COPY ITEM SET */

    /** Makes a copy of a given itemSet.
    @param itemSet the given item set.
    @return copy of given item set. */

    protected short[] copyItemSet(short[] itemSet) {

	// Check whether there is a itemSet to copy

	if (itemSet == null) return(null);

	// Do copy and return

	short[] newItemSet = new short[itemSet.length];
	for(int index=0;index<itemSet.length;index++) {
	    newItemSet[index] = itemSet[index];
	    }
        return(newItemSet);
	}

    /* ------------------------------------------------------- */
    /*                                                         */
    /*                         MAIN METHOD                     */
    /*                                                         */
    /* ------------------------------------------------------- */

    /* MAIN METHOD */

    public static void main(String[] args) throws IOException {
	// Create instance of class AprioriTgui
	AprioriTgui newFile = new AprioriTgui("LUCS-KDD Apriori-T");

	// Make window vissible
	newFile.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	newFile.setSize(500,800);
        newFile.setVisible(true);
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -