Commit 5e9f723a authored by Bruno López Trigo's avatar Bruno López Trigo

Engadida explicación de confusión entre clases e manexador de arquivos

parent 6c8d7d6f
Pipeline #1952 failed with stages
in 27 seconds
......@@ -2,10 +2,11 @@ package brunolopez.expliclas.builder;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.DatasetConfig;
public interface BuilderManager {
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx;
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx, NotFoundEx;
}
package brunolopez.expliclas.builder;
import brunolopez.expliclas.json.MapperJSON;
import brunolopez.expliclas.utils.MapperJSON;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.CategoricAttribute;
import brunolopez.expliclas.models.CategoricProperty;
import brunolopez.expliclas.models.NumericAttribute;
......@@ -10,6 +11,7 @@ import brunolopez.expliclas.models.Consequent;
import brunolopez.expliclas.models.DatasetConfig;
import brunolopez.expliclas.models.Interval;
import brunolopez.expliclas.models.NumericProperty;
import brunolopez.expliclas.utils.FileManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
......@@ -26,25 +28,22 @@ import weka.estimators.EstimatorUtils;
public class BuilderManagerImpl implements BuilderManager {
private final String BASE = getClass().getClassLoader().getResource("").getFile();
private final MapperJSON mapper;
private final FileManager fmanager;
public BuilderManagerImpl() {
this.mapper = new MapperJSON();
this.fmanager = new FileManager();
}
@Override
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx {
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx, NotFoundEx {
FileReader readerLRN;
DatasetConfig config = new DatasetConfig(datasetName);
File dataset = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + ".arff");
String logDirectory = this.BASE + "tmp/" + token + "/" + datasetName + "/" + model;
String logLocation = logDirectory + "/" + datasetName + ".arff." + model + ".log.txt";
File configLocation = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + ".json");
File configLocationEs = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + "-es.json");
File configLocationGl = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + "-gl.json");
File dataset = this.fmanager.getDataset(token, datasetName);
String logLocation = this.fmanager.getLogLocation(token, datasetName, model);
if(new File(logLocation).exists()){
throw new ConflictEx("Classifier already exists");
}
......@@ -74,7 +73,7 @@ public class BuilderManagerImpl implements BuilderManager {
reptree.buildClassifier(instancesLRN);
}
new File(logDirectory).mkdir();
new File(this.fmanager.getLogDirectory(token, datasetName, model)).mkdir();
log = new PrintStream(new FileOutputStream(logLocation, false));
log.println("=== Run information ===\n");
......@@ -171,13 +170,16 @@ public class BuilderManagerImpl implements BuilderManager {
log.close();
readerLRN.close();
if(!configLocation.exists()){
this.mapper.writeJSON(config, configLocation);
this.mapper.writeJSON(config, configLocationEs);
this.mapper.writeJSON(config, configLocationGl);
}
else
File configLocation;
try{
configLocation = this.fmanager.getConfig(token, datasetName, "en");
config = this.mapper.readConfigJSON(configLocation);
} catch (NotFoundEx ex){
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "en")));
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "es")));
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "gl")));
}
} catch (Exception ex) {
throw new FormatEx("Error building log, check format");
......
......@@ -48,6 +48,8 @@ public class MatrixBuilder {
}
}
this.sc.close();
return m;
}
......
package brunolopez.expliclas.classifiers;
import brunolopez.expliclas.json.MapperJSON;
import brunolopez.expliclas.utils.MapperJSON;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.CategoricAttribute;
import brunolopez.expliclas.models.CategoricNode;
import brunolopez.expliclas.models.CategoricProperty;
import brunolopez.expliclas.models.Consequent;
......@@ -12,10 +11,8 @@ import brunolopez.expliclas.models.ConsequentNode;
import brunolopez.expliclas.models.DatasetConfig;
import brunolopez.expliclas.models.Node;
import brunolopez.expliclas.models.NodeShape;
import brunolopez.expliclas.models.NumericAttribute;
import brunolopez.expliclas.models.NumericNode;
import brunolopez.expliclas.models.VisualNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
......@@ -52,7 +49,7 @@ public class TreeBuilder {
}
}
public VisualNode buildTree() throws NotFoundEx, IOException, FormatEx, ConflictEx {
public VisualNode buildTree(boolean override) throws NotFoundEx, IOException, FormatEx, ConflictEx {
// Grupo 1: Atributo --- Grupo 2: Condicion --- Grupo 3: Valor
Pattern numericNode = Pattern.compile("^([\\w-_]*)\\s(<=|>)\\s([+-]?[0-9]*.?[0-9]+)$");
Pattern numericNodeAux = Pattern.compile("^([\\w-_]*)\\s(<|>=)\\s([+-]?[0-9]*.?[0-9]+)$");
......@@ -71,7 +68,7 @@ public class TreeBuilder {
throw new NotFoundEx("Classifier not found");
}
if(this.output.exists())
if(this.output.exists() && !override)
throw new ConflictEx("Tree already exists");
String line = goToTreeStart(numericNode, numericNodeAux, conseqNode, conseqNodeAux, categoricNode, categoricNodeAux, oneNode);
......@@ -692,6 +689,8 @@ public class TreeBuilder {
line = this.sc.nextLine();
}
this.sc.close();
this.mapper.writeJSON(root, this.output);
return buildVisualNode(new VisualNode(), root);
......
......@@ -2,10 +2,8 @@ package brunolopez.expliclas.classifiers;
import brunolopez.expliclas.models.Antecedent;
import brunolopez.expliclas.models.Attribute;
import brunolopez.expliclas.models.CategoricAttribute;
import brunolopez.expliclas.models.CategoricNode;
import brunolopez.expliclas.models.Classification;
import brunolopez.expliclas.models.Consequent;
import brunolopez.expliclas.models.ConsequentNode;
import brunolopez.expliclas.models.DatasetConfig;
import brunolopez.expliclas.models.Instance;
......
......@@ -14,7 +14,7 @@ public interface DatasetManager {
public ArrayList<Dataset> listDatasets(String token) throws NotFoundEx;
public ArrayList<Dataset> deleteDatasets(String token) throws NotFoundEx;
public Dataset getDataset(String token, String name) throws NotFoundEx;
public Dataset getDataset(String token, String name) throws NotFoundEx, IOException;
public Dataset deleteDataset(String token, String name) throws NotFoundEx;
public ModifiedDataset uploadDataset(String token, String name, InputStream file) throws FormatEx, ConflictEx, IOException;
public Line getDatasetLine(String token, String name, int number) throws FormatEx, NotFoundEx, IOException;
......
......@@ -8,6 +8,7 @@ import brunolopez.expliclas.models.Dataset;
import brunolopez.expliclas.models.Line;
import brunolopez.expliclas.models.ModifiedDataset;
import brunolopez.expliclas.models.NumericAttribute;
import brunolopez.expliclas.utils.FileManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
......@@ -20,21 +21,25 @@ import java.util.regex.Pattern;
public class DatasetManagerImpl implements DatasetManager {
private final String BASE = getClass().getClassLoader().getResource("").getFile();
private final FileManager fmanager;
public DatasetManagerImpl() {
this.fmanager = new FileManager();
}
@Override
public ArrayList<Dataset> listDatasets(String token) throws NotFoundEx {
String[] datasets = new File(this.BASE).list();
String[] datasets = this.fmanager.getBASE().list();
File tmp;
ArrayList<Dataset> datasetList = new ArrayList();
for (String d : datasets) {
if (!d.equals("brunolopez") && !d.equals("global") && !d.equals("tmp")) {
File f = new File(this.BASE + d);
File f = new File(this.fmanager.getBASELocation() + d);
if (f.isDirectory()) {
try {
tmp = new File(this.BASE + "/" + d + "/" + d + ".arff");
tmp = new File(this.fmanager.getBASELocation() + "/" + d + "/" + d + ".arff");
datasetList.add(buildDataset(d, tmp));
} catch (IOException ex) {
throw new NotFoundEx("No datatsets found");
......@@ -44,12 +49,12 @@ public class DatasetManagerImpl implements DatasetManager {
}
if (!token.isEmpty()) {
tmp = new File(this.BASE + "tmp/" + token);
tmp = this.fmanager.getBASE(token);
if (tmp.exists()) {
datasets = tmp.list();
for (String d : datasets) {
try {
datasetList.add(buildDataset(d, new File(this.BASE + "tmp/" + token + "/" + d + "/" + d + ".arff")));
datasetList.add(buildDataset(d, new File(this.fmanager.getBASELocation(token) + "/" + d + "/" + d + ".arff")));
} catch (IOException ex) {
throw new NotFoundEx("No datatsets found");
}
......@@ -62,51 +67,21 @@ public class DatasetManagerImpl implements DatasetManager {
}
@Override
public Dataset getDataset(String token, String name) throws NotFoundEx {
public Dataset getDataset(String token, String name) throws NotFoundEx, IOException {
String[] datasets = new File(this.BASE).list();
File tmp;
String[] datasets = this.fmanager.getBASE().list();
File data;
Dataset dataset = null;
boolean found = false;
for (String d : datasets) {
if (!d.equals("brunolopez") && !d.equals("global") && !d.equals("tmp") && d.equals(name)) {
File f = new File(this.BASE + d);
if (f.isDirectory()) {
tmp = new File(this.BASE + "/" + d + "/" + d + ".arff");
try {
dataset = buildDataset(d, tmp);
} catch (IOException ex) {
throw new NotFoundEx("No dataset found");
}
found = true;
break;
}
}
}
if (!token.isEmpty() && !found) {
tmp = new File(this.BASE + "tmp/" + token);
if (tmp.exists()) {
datasets = tmp.list();
for (String d : datasets) {
if (d.equals(name)) {
try {
dataset = buildDataset(d, new File(this.BASE + "tmp/" + token + "/" + d + "/" + d + ".arff"));
} catch (IOException ex) {
throw new NotFoundEx("No dataset found");
}
found=true;
break;
}
}
}
}
if(!found){
throw new NotFoundEx("No dataset found");
try {
data = this.fmanager.getDataset(name);
} catch (NotFoundEx ex) {
data = this.fmanager.getDataset(token, name);
}
dataset = buildDataset(name, data);
return dataset;
}
......@@ -157,21 +132,21 @@ public class DatasetManagerImpl implements DatasetManager {
@Override
public ArrayList<Dataset> deleteDatasets(String token) throws NotFoundEx {
File userDir = new File(this.BASE + "tmp/" + token);
File userDir = this.fmanager.getBASE(token);
String[] datasets;
ArrayList<Dataset> removed = new ArrayList<>();
if (userDir.exists()) {
datasets = userDir.list();
for (String dataset : datasets) {
File classifier = new File(this.BASE + "tmp/" + token + "/" + dataset);
File classifier = new File(this.fmanager.getBASELocation(token) + "/" + dataset);
String[] listClassifiers = classifier.list();
for (String c : listClassifiers) {
File log = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + c);
File log = new File(this.fmanager.getBASELocation(token) + "/" + dataset + "/" + c);
if (log.isDirectory()) {
String[] listLogs = log.list();
for (String l : listLogs) {
File remove = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + c + "/" + l);
File remove = new File(this.fmanager.getBASELocation(token) + "/" + dataset + "/" + c + "/" + l);
remove.delete();
}
}
......@@ -195,9 +170,9 @@ public class DatasetManagerImpl implements DatasetManager {
@Override
public ModifiedDataset uploadDataset(String token, String name, InputStream file) throws FormatEx, ConflictEx, IOException {
File defaultDataset = new File(this.BASE + name + "/" + name + ".arff");
File userDataset = new File(this.BASE + "tmp/" + token + "/" + name + "/" + name + ".arff");
String tmpLocation = this.BASE + "tmp/" + token + "/" + name + "/" + name + ".arff.tmp";
File defaultDataset = new File(this.fmanager.getDatasetLocation(name));
File userDataset = new File(this.fmanager.getDatasetLocation(token, name));
String tmpLocation = this.fmanager.getBASELocation(token) + "/" + name + "/" + name + ".arff.tmp";
ModifiedDataset dataset = null;
......@@ -205,7 +180,7 @@ public class DatasetManagerImpl implements DatasetManager {
throw new ConflictEx("Dataset already exists");
}
new File(this.BASE + "tmp/" + token + "/" + name).mkdirs();
new File(this.fmanager.getBASE(token) + "/" + name).mkdirs();
writeToFile(file, tmpLocation);
......@@ -233,19 +208,15 @@ public class DatasetManagerImpl implements DatasetManager {
@Override
public Dataset deleteDataset(String token, String name) throws NotFoundEx {
File datasetDir = new File(this.BASE + "tmp/" + token + "/" + name);
File datasetDir = this.fmanager.getDataset(token, name);
Dataset dataset = null;
if (!datasetDir.exists()) {
throw new NotFoundEx("Dataset not found");
}
for (String content : datasetDir.list()) {
File tmp = new File(this.BASE + "tmp/" + token + "/" + name + "/" + content);
File tmp = new File(this.fmanager.getBASELocation(token) + "/" + name + "/" + content);
if (tmp.isDirectory()) {
for (String log : tmp.list()) {
File logFile = new File(this.BASE + "tmp/" + token + "/" + name + "/" + content + "/" + log);
File logFile = new File(this.fmanager.getBASELocation(token) + "/" + name + "/" + content + "/" + log);
logFile.delete();
}
} else if (tmp.getName().endsWith(".arff")) {
......@@ -267,23 +238,14 @@ public class DatasetManagerImpl implements DatasetManager {
@Override
public Line getDatasetLine(String token, String name, int number) throws FormatEx, NotFoundEx, IOException {
File datasetDefault = new File(this.BASE + name + "/" + name + ".arff");
File dataset;
Dataset d;
Line line = null;
if (datasetDefault.exists()) {
d = buildDataset(name, datasetDefault);
if (number < 1 || number > d.getLines().size()) {
throw new FormatEx("Invalid line");
}
line = d.getLine(number - 1);
return line;
}
File dataset = new File(this.BASE + "tmp/" + token + "/" + name + "/" + name + ".arff");
Line line;
if (!dataset.exists()) {
throw new NotFoundEx("Dataset not found");
try {
dataset = this.fmanager.getDataset(name);
} catch (NotFoundEx ex) {
dataset = this.fmanager.getDataset(token, name);
}
d = buildDataset(name, dataset);
......
......@@ -2,6 +2,7 @@ package brunolopez.expliclas.explainer;
import java.util.ArrayList;
import simplenlg.features.Feature;
import simplenlg.features.NumberAgreement;
import simplenlg.framework.CoordinatedPhraseElement;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
......@@ -69,7 +70,29 @@ public abstract class ClauseGenerator {
}
public SPhraseSpec generateClausePreModifier(String subject, String subjectModifier, String verb, String object, boolean plural) {
public SPhraseSpec generateClause(String subject, String verb, boolean plural, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setPlural(plural);
NPPhraseSpec subjectPhrase = this.getNlgFactory().createNounPhrase(subject);
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
if(subject!=null){
phrase.setSubject(subjectPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
}
else{
phrase = this.getNlgFactory().createClause(null, verbPhrase, objectPhrase);
}
return phrase;
}
public SPhraseSpec generateClausePreModifier(String subject, boolean plural, String subjectModifier, String verb, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
......@@ -80,14 +103,14 @@ public abstract class ClauseGenerator {
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
return phrase;
}
public SPhraseSpec generateClausePostModifier(String subject, String subjectModifier, String verb, String object, boolean plural) {
public SPhraseSpec generateClausePostModifier(String subject, boolean plural, String subjectModifier, String verb, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
......@@ -98,12 +121,40 @@ public abstract class ClauseGenerator {
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
return phrase;
}
public SPhraseSpec generateModalClause(String subject, String verb, String modal, boolean pluralVerb, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setFeature(Feature.MODAL, modal);
verbPhrase.setPlural(pluralVerb);
NPPhraseSpec subjectPhrase = this.getNlgFactory().createNounPhrase(subject);
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
if(subject!=null){
phrase.setSubject(subjectPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
}
else{
phrase = this.getNlgFactory().createClause(null, verbPhrase, objectPhrase);
}
return phrase;
}
public VPPhraseSpec generateModalVerbClause(String verb, boolean pluralVerb, String modal, String verbComplement){
VPPhraseSpec vPhrase;
vPhrase = this.getNlgFactory().createVerbPhrase("haber");
vPhrase.setFeature(Feature.MODAL, "poder");
vPhrase.addComplement("confusión");
return vPhrase;
}
public SPhraseSpec generateComplentiserClause(SPhraseSpec phrase1, String compliment, SPhraseSpec phrase2) {
phrase2.setFeature(Feature.COMPLEMENTISER, compliment);
......
......@@ -2,7 +2,6 @@ package brunolopez.expliclas.explainer;
import simplenlg.framework.NLGElement;
import simplenlg.lexicon.english.XMLLexicon;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.realiser.english.Realiser;
public class ClauseGeneratorEn extends ClauseGenerator {
......@@ -15,10 +14,6 @@ public class ClauseGeneratorEn extends ClauseGenerator {
this.realiser = new Realiser(super.getLexicon());
}
public String getRealisation(SPhraseSpec phrase) {
return this.realiser.realiseSentence(phrase);
}
public String getRealisation(NLGElement phrase) {
return this.realiser.realiseSentence(phrase);
}
......
......@@ -2,7 +2,6 @@ package brunolopez.expliclas.explainer;
import simplenlg.framework.NLGElement;
import simplenlg.lexicon.spanish.XMLLexicon;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.realiser.spanish.Realiser;
public class ClauseGeneratorEs extends ClauseGenerator {
......@@ -13,10 +12,6 @@ public class ClauseGeneratorEs extends ClauseGenerator {
super(new XMLLexicon());
this.realiser = new Realiser(super.getLexicon());
}
public String getRealisation(SPhraseSpec phrase) {
return this.realiser.realiseSentence(phrase);
}
public String getRealisation(NLGElement phrase) {
return this.realiser.realiseSentence(phrase);
......
......@@ -2,7 +2,6 @@ package brunolopez.expliclas.explainer;
import simplenlg.framework.NLGElement;
import simplenlg.lexicon.galician.XMLLexicon;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.realiser.galician.Realiser;
public class ClauseGeneratorGl extends ClauseGenerator {
......@@ -13,11 +12,7 @@ public class ClauseGeneratorGl extends ClauseGenerator {
super(new XMLLexicon());
this.realiser = new Realiser(super.getLexicon());
}
public String getRealisation(SPhraseSpec phrase) {
return this.realiser.realiseSentence(phrase);
}
public String getRealisation(NLGElement phrase) {
return this.realiser.realiseSentence(phrase);
}
......
......@@ -26,7 +26,9 @@ public class ConfusionAnalyzer {
while (!line.startsWith("Correctly Classified Instances")) {
line = this.sc.nextLine();
}
this.sc.close();
return Double.parseDouble(line.split("\\s+")[4]);
}
......@@ -94,4 +96,8 @@ public class ConfusionAnalyzer {
}
public Double getConfusionBetween(int consequentA, int consequentB){
return this.matrix.getConfusion(consequentA, consequentB);
}
}
package brunolopez.expliclas.explainer;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.Classification;
import brunolopez.expliclas.models.Consequent;
import brunolopez.expliclas.models.Explanation;
import java.io.IOException;