You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
7.8 KiB
268 lines
7.8 KiB
6 years ago
|
package util;
|
||
|
|
||
|
import static util.Util.*;
|
||
|
|
||
|
import java.io.*;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.*;
|
||
|
|
||
|
import org.apache.commons.csv.CSVFormat;
|
||
|
import org.apache.commons.csv.CSVPrinter;
|
||
|
import org.apache.commons.lang3.tuple.Pair;
|
||
|
import org.json.simple.JSONArray;
|
||
|
import org.json.simple.JSONObject;
|
||
|
|
||
|
import data.Enums.WordLevelType;
|
||
|
|
||
|
@SuppressWarnings("unchecked")
|
||
|
public class Export {
|
||
|
public static void SetToJSON(Set<Pair<String, Map<String, Long>>> set) {
|
||
|
JSONArray wrapper = new JSONArray();
|
||
|
|
||
|
for (Pair<String, Map<String, Long>> p : set) {
|
||
|
JSONArray data_wrapper = new JSONArray();
|
||
|
JSONObject metric = new JSONObject();
|
||
|
|
||
|
String title = p.getLeft();
|
||
|
Map<String, Long> map = p.getRight();
|
||
|
|
||
|
if (map.isEmpty())
|
||
|
continue;
|
||
|
|
||
|
long total = Util.mapSumFrequencies(map);
|
||
|
|
||
|
for (Map.Entry<String, Long> e : map.entrySet()) {
|
||
|
JSONObject data_entry = new JSONObject();
|
||
|
data_entry.put("word", e.getKey());
|
||
|
data_entry.put("frequency", e.getValue());
|
||
|
data_entry.put("percent", formatNumberAsPercent((double) e.getValue() / total));
|
||
|
|
||
|
data_wrapper.add(data_entry);
|
||
|
}
|
||
|
|
||
|
metric.put("Title", title);
|
||
|
metric.put("data", data_wrapper);
|
||
|
wrapper.add(metric);
|
||
|
}
|
||
|
|
||
|
try (FileWriter file = new FileWriter("statistics.json")) {
|
||
|
file.write(wrapper.toJSONString());
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String SetToCSV(Set<Pair<String, Map<String, Long>>> set, File resultsPath, LinkedHashMap<String, String> headerInfoBlock) {
|
||
|
//Delimiter used in CSV file
|
||
|
String NEW_LINE_SEPARATOR = "\n";
|
||
|
|
||
|
//CSV file header
|
||
|
Object[] FILE_HEADER = {"word", "frequency", "percent"};
|
||
|
|
||
|
String fileName = "";
|
||
|
|
||
|
for (Pair<String, Map<String, Long>> p : set) {
|
||
|
String title = p.getLeft();
|
||
|
fileName = title.replace(": ", "-");
|
||
|
fileName = fileName.replace(" ", "_").concat(".csv");
|
||
|
|
||
|
fileName = resultsPath.toString().concat(File.separator).concat(fileName);
|
||
|
|
||
|
Map<String, Long> map = p.getRight();
|
||
|
|
||
|
if (map.isEmpty())
|
||
|
continue;
|
||
|
|
||
|
long total = Util.mapSumFrequencies(map);
|
||
|
|
||
|
OutputStreamWriter fileWriter = null;
|
||
|
CSVPrinter csvFilePrinter = null;
|
||
|
|
||
|
//Create the CSVFormat object with "\n" as a record delimiter
|
||
|
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR).withDelimiter(';');
|
||
|
|
||
|
try {
|
||
|
//initialize FileWriter object
|
||
|
fileWriter = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);
|
||
|
|
||
|
//initialize CSVPrinter object
|
||
|
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
|
||
|
|
||
|
// write info block
|
||
|
printHeaderInfo(csvFilePrinter, headerInfoBlock);
|
||
|
|
||
|
//Create CSV file header
|
||
|
csvFilePrinter.printRecord(FILE_HEADER);
|
||
|
|
||
|
for (Map.Entry<String, Long> e : map.entrySet()) {
|
||
|
List dataEntry = new ArrayList<>();
|
||
|
dataEntry.add(e.getKey());
|
||
|
dataEntry.add(e.getValue().toString());
|
||
|
dataEntry.add(formatNumberAsPercent((double) e.getValue() / total));
|
||
|
csvFilePrinter.printRecord(dataEntry);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
System.out.println("Error in CsvFileWriter!");
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (fileWriter != null) {
|
||
|
fileWriter.flush();
|
||
|
fileWriter.close();
|
||
|
}
|
||
|
if (csvFilePrinter != null) {
|
||
|
csvFilePrinter.close();
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
System.out.println("Error while flushing/closing fileWriter/csvPrinter!");
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return fileName;
|
||
|
}
|
||
|
|
||
|
public static String SetToCSV(String title, Object[][] result, File resultsPath, LinkedHashMap<String, String> headerInfoBlock) {
|
||
|
//Delimiter used in CSV file
|
||
|
String NEW_LINE_SEPARATOR = "\n";
|
||
|
|
||
|
//CSV file header
|
||
|
Object[] FILE_HEADER = {"word", "frequency", "percent"};
|
||
|
|
||
|
String fileName = "";
|
||
|
|
||
|
fileName = title.replace(": ", "-");
|
||
|
fileName = fileName.replace(" ", "_").concat(".csv");
|
||
|
|
||
|
fileName = resultsPath.toString().concat(File.separator).concat(fileName);
|
||
|
|
||
|
OutputStreamWriter fileWriter = null;
|
||
|
CSVPrinter csvFilePrinter = null;
|
||
|
|
||
|
//Create the CSVFormat object with "\n" as a record delimiter
|
||
|
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR).withDelimiter(';');
|
||
|
|
||
|
try {
|
||
|
//initialize FileWriter object
|
||
|
fileWriter = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);
|
||
|
|
||
|
//initialize CSVPrinter object
|
||
|
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
|
||
|
|
||
|
// write info block
|
||
|
printHeaderInfo(csvFilePrinter, headerInfoBlock);
|
||
|
|
||
|
//Create CSV file header
|
||
|
csvFilePrinter.printRecord(FILE_HEADER);
|
||
|
|
||
|
for (Object[] resultEntry : result) {
|
||
|
List dataEntry = new ArrayList<>();
|
||
|
dataEntry.add(resultEntry[0]);
|
||
|
dataEntry.add(resultEntry[1]);
|
||
|
dataEntry.add(formatNumberAsPercent(resultEntry[2]));
|
||
|
csvFilePrinter.printRecord(dataEntry);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
System.out.println("Error in CsvFileWriter!");
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (fileWriter != null) {
|
||
|
fileWriter.flush();
|
||
|
fileWriter.close();
|
||
|
}
|
||
|
if (csvFilePrinter != null) {
|
||
|
csvFilePrinter.close();
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
System.out.println("Error while flushing/closing fileWriter/csvPrinter!");
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return fileName;
|
||
|
}
|
||
|
|
||
|
public static String nestedMapToCSV(String title, Map<WordLevelType, Map<String, Map<String, Long>>> result, File resultsPath, LinkedHashMap<String, String> headerInfoBlock) {
|
||
|
//Delimiter used in CSV file
|
||
|
String NEW_LINE_SEPARATOR = "\n";
|
||
|
|
||
|
//CSV file header
|
||
|
Object[] FILE_HEADER = {"type", "key", "word", "frequency"};
|
||
|
|
||
|
String fileName = "";
|
||
|
|
||
|
fileName = title.replace(": ", "-");
|
||
|
fileName = fileName.replace(" ", "_").concat(".csv");
|
||
|
|
||
|
fileName = resultsPath.toString().concat(File.separator).concat(fileName);
|
||
|
|
||
|
OutputStreamWriter fileWriter = null;
|
||
|
CSVPrinter csvFilePrinter = null;
|
||
|
|
||
|
//Create the CSVFormat object with "\n" as a record delimiter
|
||
|
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR).withDelimiter(';');
|
||
|
|
||
|
try {
|
||
|
//initialize FileWriter object
|
||
|
fileWriter = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);
|
||
|
|
||
|
//initialize CSVPrinter object
|
||
|
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
|
||
|
|
||
|
// write info block
|
||
|
printHeaderInfo(csvFilePrinter, headerInfoBlock);
|
||
|
|
||
|
//Create CSV file header
|
||
|
csvFilePrinter.printRecord(FILE_HEADER);
|
||
|
|
||
|
for (Map.Entry<WordLevelType, Map<String, Map<String, Long>>> typeEntry : result.entrySet()) {
|
||
|
for (Map.Entry<String, Map<String, Long>> keyWordEntry : typeEntry.getValue().entrySet()) {
|
||
|
for (Map.Entry<String, Long> calculationResults : keyWordEntry.getValue().entrySet()) {
|
||
|
List values = new ArrayList();
|
||
|
values.add(typeEntry.getKey().getName());
|
||
|
values.add(keyWordEntry.getKey());
|
||
|
values.add(calculationResults.getKey());
|
||
|
values.add(calculationResults.getValue());
|
||
|
csvFilePrinter.printRecord(values);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
System.out.println("Error in CsvFileWriter!");
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (fileWriter != null) {
|
||
|
fileWriter.flush();
|
||
|
fileWriter.close();
|
||
|
}
|
||
|
if (csvFilePrinter != null) {
|
||
|
csvFilePrinter.close();
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
System.out.println("Error while flushing/closing fileWriter/csvPrinter!");
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return fileName;
|
||
|
}
|
||
|
|
||
|
private static void printHeaderInfo(CSVPrinter csvFilePrinter, LinkedHashMap<String, String> headerInfoBlock) throws IOException {
|
||
|
for (Map.Entry<String, String> entry : headerInfoBlock.entrySet()) {
|
||
|
List values = new ArrayList();
|
||
|
values.add(entry.getKey());
|
||
|
values.add(entry.getValue());
|
||
|
csvFilePrinter.printRecord(values);
|
||
|
}
|
||
|
|
||
|
// 2 empty lines
|
||
|
List values = new ArrayList();
|
||
|
csvFilePrinter.printRecord(values);
|
||
|
csvFilePrinter.printRecord(values);
|
||
|
|
||
|
}
|
||
|
}
|