package de.unikassel.cs.kde.kdd;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Apriori {

	
	/** Reads in the transactions from the file given as first argument and computes all
	 * frequent itemsets with minimal support.
	 * 
	 * @param args - args[0]: file name, args[1] minimal support
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		if (args.length < 2) {
			System.err.println("usage: ");
			System.err.println("  java " + Apriori.class.getName() + " inputfile minSupport");
			System.exit(1);
		}
		final String inputFile = args[0];
		final int minSupport   = Integer.parseInt(args[1]);
		/*
		 * read transactions from file
		 */
		SortedMap<String, SortedSet<String>> transactions = getTransactions(inputFile);
		/*
		 * compute frequent itemsets
		 */
		Set<Set<String>> frequentItemsets = apriori(transactions, minSupport);
		
		/*
		 * print some output
		 */
		System.out.println("transactions: ");
		System.out.println(transactions);
		
		System.out.println("frequent itemsets with minimal support " + minSupport);
		System.out.println(frequentItemsets);
	}
	
	
	/** Computes frequent itemsets with a minimal support of minSupport.
	 * 
	 * @param transactions - a set of transactions.
	 * @param minSupport - minimal support value (absolute support, i.e., number of items).
	 * 
	 * @return All frequent itemsets which have a minimal support of minSupport.
	 */
	private static Set<Set<String>> apriori(SortedMap<String, SortedSet<String>> transactions, int minSupport) {
		Set<Set<String>> frequentItemsets = new TreeSet<Set<String>>();
		
		
		return frequentItemsets;
	}
	
	
	
	/** Reads transactions from a file.
	 * 
	 * Each line of the file contains a transaction, items in a 
	 * transaction are separated with comma (,).  
	 * 
	 * @param filename - name (full path) of the file which contains the transactions.
	 * @return A set of transactions.
	 * @throws IOException - if reading the file did not succeed.
	 */
	private static SortedMap<String, SortedSet<String>> getTransactions (String filename) throws IOException {

		SortedMap<String, SortedSet<String>> transactions = new TreeMap<String, SortedSet<String>>();

		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
		
		/*
		 * read in all transactions
		 */
		String line = null;
		int transactionId = 0;
		while ((line = reader.readLine()) != null) {
			transactionId++;
			SortedSet<String> transaction = new TreeSet<String>();
			/*
			 * extract items from transaction
			 */
			StringTokenizer token = new StringTokenizer(line, ",");
			while (token.hasMoreTokens()) {
				transaction.add(token.nextToken());
			}
			transactions.put("t" + transactionId, transaction);
		}
		reader.close();
		return transactions;
	}
	
}
