📄 itemsinput.java
字号:
/**
* @(#)ItemSet.java
*
* Loads items and transactions from text files and generates candidate itemsets.
*
* @author Adrian Bright
*/
import java.io.*;
import java.util.*;
public class ItemsInput
{
/* The list of item definitions */
private LinkedList itemsRef;
/* The maximum transaction length from all the itemsets */
private int maxTransactionLength;
/* The total number of transactions */
private int transactionNum;
/**
* ItemsInput Reads the item definitions from file.
* @param fileName Name of the file with the item definitions.
*/
public ItemsInput(String fileName)
{
try
{
BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
String line = null;
itemsRef = new LinkedList();
ItemRef newItemRef = new ItemRef();
StringTokenizer st;
line = inputStream.readLine();
while (line != null)
{
newItemRef = new ItemRef();
st = new StringTokenizer(line);
newItemRef.setItemID(st.nextToken());
newItemRef.setItemName(st.nextToken());
itemsRef.add(newItemRef);
line = inputStream.readLine();
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("File " + fileName + " not found or could not be opened.");
}
catch(IOException e)
{
System.out.println("Error reading from file " + fileName + ".");
}
}
/**
* findCandidateOneItems Finds the frequent one-itemsets.
* @param fileName The file in which the transactions are stored.
* @return List of the candidate one-itemsets.
*/
public LinkedList findCandidateOneItems(String fileName)
{
LinkedList itemsCand = new LinkedList();
try
{
String currentAttribute;
LinkedList transItems = new LinkedList();
ListIterator itTrans;
ListIterator itItemsCand;
LinkedList items = new LinkedList();
BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
String line = null;
StringTokenizer st;
maxTransactionLength = 0;
transactionNum = 0;
int lengthCount = 0;
String currentTransactionID = new String();
String newTransactionID;
boolean firstLine = true;
/* build the frequency counter */
ItemRef newItemRef = new ItemRef();
ItemSet newItemsetElements;
ListIterator it = itemsRef.listIterator(0);
String nextLine;
boolean lastLine = false;
while (it.hasNext())
{
newItemsetElements = new ItemSet();
newItemRef = (ItemRef)it.next();
newItemsetElements.setOneItem(newItemRef.getItemID());
itemsCand.add(newItemsetElements);
}
line = inputStream.readLine();
nextLine = line;
while (line != null)
{
nextLine = inputStream.readLine();
if (nextLine == null)
{
lastLine = true;
}
st = new StringTokenizer(line);
newTransactionID = st.nextToken();
if (!newTransactionID.equals(currentTransactionID))
{
if (!firstLine || lastLine/* || acrossFormat*/)
{
itTrans = items.listIterator(0);
while (itTrans.hasNext())
{
currentAttribute = (String)itTrans.next();
itItemsCand = itemsCand.listIterator(0);
while (itItemsCand.hasNext())
{
newItemsetElements = (ItemSet)itItemsCand.next();
if((newItemsetElements.getItem(0).compareTo(currentAttribute)) == 0)
{
newItemsetElements.incrementSupport();
}
}
}
}
else
{
firstLine = false;
}
transactionNum++;
currentTransactionID = newTransactionID;
items = new LinkedList();
//newTrans.setTID(newTransactionID);
if (lengthCount > maxTransactionLength)
{
maxTransactionLength = lengthCount;
}
lengthCount = 0;
}
while (st.hasMoreTokens())
{
items.add(st.nextToken());
lengthCount++;
}
line = nextLine;
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("File " + fileName + " not found or could not be opened.");
}
catch(IOException e)
{
System.out.println("Error reading from file " + fileName + ".");
}
return itemsCand;
}
/**
* printOneItemsets Prints the 1-itemsets to screen.
* @param items The candidate one-itemsets.
*/
public void printOneItemsets(LinkedList items)
{
ItemSet newItemsetElements;
ListIterator itItems = items.listIterator(0);
System.out.println("1-itemsets");
while (itItems.hasNext())
{
newItemsetElements = (ItemSet)itItems.next();
System.out.println(newItemsetElements.getItem(0));
}
System.out.println("***********************");
}
/**
* removeInfrequentItemsets Removes the infrequent itemsets.
* @param itemsFreq The list of candidate one-itemsets.
* @param minSup The minimum support.
* @return List of the frequent 1-itemsets.
*/
public LinkedList removeInfrequentItemsets(LinkedList itemsFreq,int minSup)
{
ListIterator itItemsFreq = itemsFreq.listIterator(0);
ItemSet newItemsetElements;
LinkedList indexesToDelete = new LinkedList();
while (itItemsFreq.hasNext())
{
newItemsetElements = (ItemSet)itItemsFreq.next();
if(newItemsetElements.getSupport() < minSup)
{
indexesToDelete.add((new Integer(itemsFreq.indexOf(newItemsetElements))));
}
}
/* the removals have to be done from the end of the list, otherwise the indexes would change as it progresses */
for(int count = indexesToDelete.size()-1; count>=0; count--)
{
itemsFreq.remove(((Integer)indexesToDelete.get(count)).intValue());
}
return itemsFreq;
}
/**
* generateNItemsets Generates the candidate n-itemsets.
* @param oneItemsets The frequent one-itemsets.
* @param nMinusOneItemsets The frequent n-1 itemsets.
* @param k The current length of itemset being generated, eg. 3-itemsets.
* @return List of the candidate n-itemsets.
*/
public LinkedList generateNItemsets(LinkedList oneItemsets,LinkedList nMinusOneItemsets,int k)
{
LinkedList nItemSets = new LinkedList();
ListIterator itOldItemsets = nMinusOneItemsets.listIterator(0);
ListIterator itOneItemsets;
ItemSet itemset;
ItemSet oneItemset;
ItemSet newItemset;
LinkedList newItemsetElements;
LinkedList oldItemSet;
ListIterator itOldItems;
while (itOldItemsets.hasNext())
{
itemset = (ItemSet)itOldItemsets.next();
itOneItemsets = oneItemsets.listIterator(0);
while (itOneItemsets.hasNext())
{
newItemsetElements = new LinkedList();
oneItemset = (ItemSet)itOneItemsets.next();
oldItemSet = (LinkedList)itemset.getItems();
itOldItems = oldItemSet.listIterator(0);
while (itOldItems.hasNext())
{
newItemsetElements.add((String)itOldItems.next());
}
if (oneItemset.getItem(0).compareTo((String)newItemsetElements.getLast()) > 0)
{
/* one-itemset element added */
newItemsetElements.add(oneItemset.getItem(0));
nItemSets.add(new ItemSet(newItemsetElements));
}
}
}
/* print all n-itemsets */
/*ListIterator itNewSets = nItemSets.listIterator(0);
ListIterator itSet;
ItemSet newItemSet;
int count = 0;
System.out.println(k+1 + "-itemsets:");
while (itNewSets.hasNext())
{
newItemSet = (ItemSet)itNewSets.next();
newItemsetElements = newItemSet.getItems();
itSet = newItemsetElements.listIterator(0);
//System.out.print(count + ": ");
while (itSet.hasNext())
{
System.out.print((String)itSet.next());
}
System.out.println();
count++;
}
System.out.println("***********************");*/
return nItemSets;
}
/**
* printItems Prints the item definitions to screen.
*/
public void printItems()
{
ItemRef newItemRef = new ItemRef();
ListIterator it = itemsRef.listIterator(0);
System.out.println("All Items: ");
while (it.hasNext())
{
newItemRef = (ItemRef)it.next();
System.out.println(newItemRef.getItemID() + ": " + newItemRef.getItemName());
}
System.out.println("***********************");
}
/**
* getItemsRef Returns the item definition list.
* @return The list of item definitions.
*/
public LinkedList getItemsRef()
{
return itemsRef;
}
/**
* getMaxTransactionLength Returns the maximum length itemset in the list of transactions.
* @return The maximum transaction length.
*/
public int getMaxTransactionLength()
{
return maxTransactionLength;
}
/**
* getTransactionNum Returns the total number of transactions.
* @return The number of transactions.
*/
public int getTransactionNum()
{
return transactionNum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -