com.krzysztofzabinski.publication1.Remover.java Source code

Java tutorial

Introduction

Here is the source code for com.krzysztofzabinski.publication1.Remover.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.krzysztofzabinski.publication1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author krzysztofzabinski
 */
public class Remover implements Runnable {

    private static List<List<String>> inputList = new ArrayList<>();
    private static List<List<String>> inputRules = new ArrayList<>();
    private static File[] inputFiles;
    private static File[] inputFilesRules;

    public static void readInput() {
        File inputDirectory = new File("input/.");
        inputFiles = inputDirectory.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith("train.csv");
            }
        });

        for (File file : inputFiles) {
            List<String> tmpList = new ArrayList<>();

            BufferedReader reader = null;
            String line;
            try {

                reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
                while ((line = reader.readLine()) != null) {
                    tmpList.add(line);
                }

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }
            }

            inputList.add(tmpList);
        }

    }

    public static void readRules() {
        File inputDirectory = new File("rules/.");
        inputFilesRules = inputDirectory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".csv");
            }
        });
        for (File file : inputFilesRules) {
            List<String> tmpList = new ArrayList<>();

            BufferedReader reader = null;
            String line;
            try {

                reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
                while ((line = reader.readLine()) != null) {
                    tmpList.add(line);
                }

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }

            }
            inputRules.add(tmpList);
        }
    }

    public static void removeUnnecessaryInputFiles() {
        for (int i = 0; i < inputFiles.length; i++) {
            for (int j = 0; j < inputFilesRules.length; j++) {
                if (inputFilesRules[j].getName().contains(inputFiles[i].getName())) {
                    break;
                } else if (j >= (inputFilesRules.length - 1)) {
                    inputList.set(i, null);
                }
            }
        }
        inputList.removeIf(Objects::isNull);
    }

    public static void removeRules() {
        //calculate min support
        int minSupport = 0;
        for (int i = 0; i < inputRules.size(); i++) {
            for (int j = 0; j < inputRules.get(i).size(); j++) {
                String[] tmpRule = inputRules.get(i).get(j).split("->|,|\\(|\\)");
                List<String> tmpSetOfRules = new ArrayList<>();
                for (String tmpRule1 : tmpRule) {
                    if (tmpRule1.contains("=")) {
                        tmpSetOfRules.add(tmpRule1.split(" = ")[1]);
                    }
                }
                int supportCounter = 0;
                for (int k = 0; k < inputList.get(i).size(); k++) {
                    int errorCounter = 0;
                    for (int kk = 0; kk < tmpSetOfRules.size(); kk++) {
                        if (!inputList.get(i).get(k).contains(tmpSetOfRules.get(kk))) {
                            errorCounter++;
                        } else if (errorCounter == 0 && kk >= (tmpSetOfRules.size() - 1)) {
                            supportCounter++;
                        }
                    }
                }
                if (i == 0 && j == 0) {
                    minSupport = supportCounter;
                } else if (minSupport > supportCounter) {
                    minSupport = supportCounter;
                }
            }
        }

        //remove rules with min support
        for (int i = 0; i < inputRules.size(); i++) {
            for (int j = 0; j < inputRules.get(i).size(); j++) {
                String[] tmpRule = inputRules.get(i).get(j).split("->|,|\\(|\\)");
                List<String> tmpSetOfRules = new ArrayList<>();
                for (String tmpRule1 : tmpRule) {
                    if (tmpRule1.contains("=")) {
                        tmpSetOfRules.add(tmpRule1.split(" = ")[1]);
                    }
                }
                int supportCounter = 0;
                for (int k = 0; k < inputList.get(i).size(); k++) {
                    int errorCounter = 0;
                    for (int kk = 0; kk < tmpSetOfRules.size(); kk++) {
                        if (!inputList.get(i).get(k).contains(tmpSetOfRules.get(kk))) {
                            errorCounter++;
                        } else if (errorCounter == 0 && kk >= (tmpSetOfRules.size() - 1)) {
                            supportCounter++;
                        }
                    }
                }
                if (supportCounter <= minSupport) {
                    inputRules.get(i).remove(j);
                }
            }
        }
    }

    public static void writeResultToFile() throws IOException {
        FileUtils.cleanDirectory(new File("output/"));
        for (int i = 0; i < inputRules.size(); i++) {
            try (FileWriter writeFile = new FileWriter("output/" + inputFilesRules[i].getName())) {
                for (int j = 0; j < inputRules.get(i).size(); j++) {
                    writeFile.append(inputRules.get(i).get(j) + "\n");
                }
                writeFile.close();
            }
        }
    }

    @Override
    public void run() {
        readInput();
        readRules();
        removeUnnecessaryInputFiles();
        removeRules();
        try {
            writeResultToFile();
        } catch (IOException ex) {
            Logger.getLogger(Remover.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}