[Home] DMelt member access. Java source code: ca.pfv.spmf.tools.dataset_converter.TransactionDatabaseConverter
package ca.pfv.spmf.tools.dataset_converter;
/* This file is copyright (c) 2008-2012 Philippe Fournier-Viger
*
* This file is part of the SPMF DATA MINING SOFTWARE
* (http://www.philippe-fournier-viger.com/spmf).
*
* SPMF is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* SPMF is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with
* SPMF. If not, see .
*/
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.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ca.pfv.spmf.input.sequence_database_array_integers.Sequence;
import ca.pfv.spmf.input.sequence_database_array_integers.SequenceDatabase;
/**
* This class is for converting transaction databases from various formats
* to the SPMF format.
@see Formats
* @author Philippe Fournier-Viger
*/
public class TransactionDatabaseConverter {
String input; // the path of the input file
String output; // the path of the file to be written to disk in SPMF format
int lineCount =0; // the number of sequences in the input file
/**
* This method converts a transaction database from a given format to the SPMF format.
* @param input the path of the input file
* @param output the path of the file to be written to disk in SPMF format
* @param inputFileformat the format of the input file
* @param lineCount the number of lines from the input file that should be converted
* @throws IOException an exception is thrown if there is an error reading/writing files Otherwise, null.
*/
public void convert(String input, String output, Formats inputFileformat, int lineCount) throws IOException {
// we save the parameter in the class fields
this.input = input;
this.output = output;
this.lineCount = lineCount;
// we call the appropriate method for converting a database
// according to the format of the input file
if(inputFileformat.equals(Formats.CSV_INTEGER)){
convertCSV();
}else if(inputFileformat.equals(Formats.ARFF)){
convertARFF(true, false);
}else if(inputFileformat.equals(Formats.ARFF_WITH_MISSING_VALUES)){
convertARFF(false, false);
}else if(inputFileformat.equals(Formats.SPMF_SEQUENCE_DB)){
convertSequenceDB();
}
}
/**
* This method convert a transaction database in ARFF format to SPMF format and
* return a map of key = item id value = corresponding attribute value. This
* method is to be used by the GUI version of SPMF that need to keep the mapping
* between item IDs and attribute value in memory to avoid an extra database scan.
* @param inputFile the path of the file to be converted
* @param outputFile the path for saving the converted file
* @param lineCount the number of lines of the input file to be converted
* @return a map of entry (key : itemID, value: attribute-value) if the input format is ARFF.
* @throws IOException if an error while reading/writing files
*/
public Map convertARFFandReturnMap(String inputFile, String outputFile,
int lineCount) throws IOException {
// we save the parameter in the class fields
this.input = inputFile;
this.output = outputFile;
this.lineCount = lineCount;
return convertARFF(true, true);
}
/**
* This method convert a file from the ARFF format to the SPMF format.
*
* @param returnMapItemIDValue
* @throws IOException exception if error while reading/writing files.
* @return a map where an entry indicates for an item (key), the corresponding attribute value (value).
*/
private Map convertARFF(boolean ignoreMissingValues, boolean returnMapItemIDValue) throws IOException {
// This map will be used to store mapping from item id (key) to attribute value (value).
// It is used only if returnMapItemIDValue is set to true. This is used by the GUI of SPMF
// which need to keep this information in memory to avoid an extra database scan after an algorithm
// is applied.
Map mapItemsIDsToAttributeValues = null;
if(returnMapItemIDValue){
mapItemsIDsToAttributeValues = new HashMap();
}
// object for writing the output file
BufferedWriter writer = new BufferedWriter(new FileWriter(output));
BufferedReader myInput = null;
try {
// Objects to read the file
FileInputStream fin = new FileInputStream(new File(input));
myInput = new BufferedReader(new InputStreamReader(fin));
int count = 0; // to count the number of data instance lines
int attributeCount =0; // to count the number fo attributes
// the last item ID used in the output file
int lastItemAdded =0;
// A list that stores a map for each attribute.
// An entry in the map is :
// key = String (attribute value)
// value = Integer (item id)
List
Ads help maintain this website.