package ca.pfv.spmf.tools.dataset_generator;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * Convert a transaction database to a transaction database with utility values
 * from the source code.
 * @author Philippe Fournier-Viger, 2010
 */
public class TransactionDatasetUtilityGenerator {
	
	/**
	 * Convert a transaction database to a transaction database with utility values
	 * from the source code.
	 * @param input the input file path (a transaction database in SPMF format)
	 * @param output the output file path
	 * @param maxQuantity the maximum quantity of each item in a transaction
	 * @param externalUtilityFactor the external utility of items generated by Random.nextGaussian() will be multiplied by this value
	 * @throws IOException if an error while reading/writting files.
	 * @throws NumberFormatException 
	 */
	public void convert(String input, String output, int maxQuantity, double externalUtilityFactor) throws NumberFormatException, IOException {
		
		// for stats
		Set items = new HashSet();
		long avglength =0;
		long tidcount = 0;
		
		Random randomGenerator = new Random(System.currentTimeMillis());
		
		Map externalUtilities = new HashMap();

		BufferedWriter writer = new BufferedWriter(new FileWriter(output)); 
		BufferedReader myInput = new BufferedReader(new InputStreamReader( new FileInputStream(new File(input))));
		// for each line (transaction) until the end of file
		String thisLine;
		while ((thisLine = myInput.readLine()) != null) {
			// if the line is  a comment, is  empty or is a
			// kind of metadata
			if (thisLine.isEmpty() == true ||
					thisLine.charAt(0) == '#' || thisLine.charAt(0) == '%'
							|| thisLine.charAt(0) == '@') {
				continue;
			}
			
			// split the transaction according to the : separator
			String split[] = thisLine.split(" "); 
			

			tidcount++;
			avglength += split.length;

			for(int i=0; i  quantities = new ArrayList();
			int TU = 0;
			
			// split the transaction according to the : separator
			for(int i=0; i  

Ads help maintain this website.