Skip to content

Commit

Permalink
add csv output function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hossiphi committed Apr 10, 2021
1 parent e4b4809 commit 99448f2
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ release.properties
.project
Pipeline/src/main/resources/texts/
Pipeline/evaluations/
Pipeline/ecsaEvaluations/
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,39 @@
@MetaInfServices(TextAgent.class)
public class InitialTextAgent extends TextAgent {

private List<IExtractor> extractors = new ArrayList<>();

public InitialTextAgent() {
super(GenericTextConfig.class);
}

private InitialTextAgent(IText text, ITextState textState, GenericTextConfig config) {
super(DependencyType.TEXT, GenericTextConfig.class, text, textState);
initializeAgents(config.textExtractors);
}

@Override
public TextAgent create(IText text, ITextState textExtractionState, Configuration config) {
return new InitialTextAgent(text, textExtractionState, (GenericTextConfig) config);
}

@Override
public void exec() {
for (IWord word : text.getWords()) {
for (IExtractor extractor : extractors) {
extractor.exec(word);
}
}
}

private void initializeAgents(List<String> extractorList) {
Map<String, TextExtractor> loadedExtractors = Loader.loadLoadable(TextExtractor.class);

for (String textExtractor : extractorList) {
if (!loadedExtractors.containsKey(textExtractor)) {
throw new IllegalArgumentException("TextAgent " + textExtractor + " not found");
}
extractors.add(loadedExtractors.get(textExtractor).create(textState));
}
}
private List<IExtractor> extractors = new ArrayList<>();

public InitialTextAgent() {
super(GenericTextConfig.class);
}

private InitialTextAgent(IText text, ITextState textState, GenericTextConfig config) {
super(DependencyType.TEXT, GenericTextConfig.class, text, textState);
initializeAgents(config.textExtractors);
}

@Override
public TextAgent create(IText text, ITextState textExtractionState, Configuration config) {
return new InitialTextAgent(text, textExtractionState, (GenericTextConfig) config);
}

@Override
public void exec() {
for (IWord word : text.getWords()) {
for (IExtractor extractor : extractors) {
extractor.exec(word);
}
}
}

private void initializeAgents(List<String> extractorList) {
Map<String, TextExtractor> loadedExtractors = Loader.loadLoadable(TextExtractor.class);

for (String textExtractor : extractorList) {
if (!loadedExtractors.containsKey(textExtractor)) {
throw new IllegalArgumentException("TextAgent " + textExtractor + " not found");
}
extractors.add(loadedExtractors.get(textExtractor).create(textState));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private static void printResultsInFiles(//
// FilePrinter.writeRecommendedRelationToFile(recommendationState);
// FilePrinter.writeConnectionsToFile(connectionState, 0);
// FilePrinter.writeConnectionRelationsToFile(connectionState);

FilePrinter.writeStatesToFile(data.getModelState(), data.getTextState(), data.getRecommendationState(), data.getConnectionState(), duration);
FilePrinter.writeTraceLinksInCsvFile(data.getConnectionState(), duration);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ private PipelineConfig() {
*/
public static final String FILE_FOR_RESULTS_PATH = CONFIG.getProperty("fileForResults_Path");

public static final String FILE_FOR_CSV_RESULTS_PATH = CONFIG.getProperty("fileForCSVResults_Path");

private static SystemParameters loadParameters(String filePath) {
return new SystemParameters(filePath, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package edu.kit.ipd.consistency_analyzer.pipeline.helpers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -613,6 +618,85 @@ public static void writeStatesToFile(IModelState extractionState, ITextState ntr

}

/**
* https://www.baeldung.com/java-csv
*
* @param connectionState
* @param duration
* @throws FileNotFoundException
*/
public static void writeTraceLinksInCsvFile(IConnectionState connectionState, Duration duration) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'_at_'HH-mm");
Date date = new Date(System.currentTimeMillis());

String fileName = PipelineConfig.FILE_FOR_CSV_RESULTS_PATH + "Eval_" + formatter.format(date) + ".csv";
File resultFile = new File(fileName);

boolean fileCreated = createFileIfNonExistent(resultFile);
if (!fileCreated) {
return;
}

List<String[]> dataLines = new ArrayList<>();

dataLines.add(new String[] { "Found TraceLinks: ", "", formatter.format(date) });
dataLines.add(new String[] { "" });
dataLines.add(new String[] { "UID of Modelelement", "SentenceNumber", "Probability" });

for (IInstanceLink instanceLink : connectionState.getInstanceLinks()) {

String probability = Double.toString(instanceLink.getProbability());
String modelElementUid = instanceLink.getModelInstance().getUid();

for (INounMapping nameMapping : instanceLink.getTextualInstance().getNameMappings()) {
for (IWord word : nameMapping.getNodes()) {
dataLines.add(new String[] { modelElementUid, Integer.toString(word.getSentenceNo()), probability });
}
}

}

try (FileWriter pw = new FileWriter(resultFile)) {
dataLines.stream().map(FilePrinter::convertToCSV).forEach(s -> {
try {
pw.append(s).append("\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
} catch (IOException e) {
logger.error(GENERIC_ERROR);
logger.debug(e.getMessage(), e.getCause());
}

}

/**
* https://www.baeldung.com/java-csv
*
* @param data
* @return
*/
private static String convertToCSV(String[] data) {
return Stream.of(data).map(FilePrinter::escapeSpecialCharacters).collect(Collectors.joining(","));
}

/**
* https://www.baeldung.com/java-csv
*
* @param data
* @return
*/
private static String escapeSpecialCharacters(String data) {
String escapedData = data.replaceAll("\\R", " ");
if (data.contains(",") || data.contains("\"") || data.contains("'")) {
data = data.replace("\"", "\"\"");
escapedData = "\"" + data + "\"";
}
return escapedData;
}

private static Comparator<IRecommendedInstance> getRecommendedInstancesComparator() {
return (ri1, ri2) -> ri1.getName().compareTo(ri2.getName());
}
Expand Down
3 changes: 2 additions & 1 deletion Pipeline/src/main/resources/configs/Pipeline.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
documentation_Path = /texts/TeammatesMdList2.txt
testDocumentation_Path = /texts/TestText.txt
fileForInput_Path = /texts/InstanceEvaluation_Sentences.txt
fileForResults_Path = evaluations/InstanceEvaluation.txt
fileForResults_Path = evaluations/InstanceEvaluation.txt
fileForCSVResults_Path = ecsaEvaluations/

0 comments on commit 99448f2

Please sign in to comment.