Commit d8377663 authored by Bruno López Trigo's avatar Bruno López Trigo

Solucionados bugs en demostrador e engadidas melloras nas explicacións

parent 44a9b217
......@@ -106,7 +106,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
this.treeBuilder = new TreeBuilder();
return this.treeBuilder.buildVisualNode(
new VisualNode(), root, config);
new VisualNode(), root, config, lang);
}
......@@ -138,7 +138,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
this.treeInterpreter = new TreeInterpreter(root);
return this.treeInterpreter.classify(instance, config);
return this.treeInterpreter.classify(instance, config, lang);
}
......@@ -170,32 +170,32 @@ public class ClassifierManagerImpl implements ClassifierManager {
this.treeInterpreter = new TreeInterpreter(root);
ArrayList<ArrayList<Classification>> classifications = new ArrayList();
ArrayList<Classification> solution = new ArrayList();
for(int i=1; i<percentage; i++){
classifications.add(this.treeInterpreter.exploreAlternatives(instance, config, ((double) i) / 100));
for (int i = 1; i < percentage; i++) {
classifications.add(this.treeInterpreter.exploreAlternatives(instance, config, ((double) i) / 100, lang));
}
classifications.add(this.treeInterpreter.exploreAlternatives(instance, config, percentage / 100));
classifications.add(this.treeInterpreter.exploreAlternatives(instance, config, percentage / 100, lang));
boolean found = false;
for(int i=0; i<classifications.size(); i++){
for(Classification c: classifications.get(i)){
for(Classification clasification: solution){
if(c.getConsequent().getId().equals(clasification.getConsequent().getId())){
for (int i = 0; i < classifications.size(); i++) {
for (Classification c : classifications.get(i)) {
for (Classification clasification : solution) {
if (c.getConsequent().getId().equals(clasification.getConsequent().getId())) {
found = true;
}
}
if(!found){
if (!found) {
solution.add(c);
}
found = false;
}
}
return solution;
}
@Override
......@@ -221,10 +221,10 @@ public class ClassifierManagerImpl implements ClassifierManager {
}
Matrix m = null;
if(matrix != null)
if (matrix != null) {
return this.mapper.readMatrixJSON(matrix);
else if(type.equals("cv")){
} else if (type.equals("cv")) {
this.matrixBuilder = new MatrixBuilder(input);
m = this.matrixBuilder.readMatrix();
this.mapper.writeJSON(m, new File(matrixLocation));
......@@ -236,7 +236,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
return m;
}
}
@Override
public Attribute getAttributeConfig(String token, String dataset, String attribute, String lang) throws NotFoundEx, IOException {
File configFile;
......@@ -476,6 +476,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
if (attEn.getProperties().size() != attributeConfig.getProperties().size()) {
throw new FormatEx("Properties size doesn't match");
}
attEn.setName(attributeConfig.getName());
boolean found = false;
for (Property p : attEn.getProperties()) {
for (Property prop : attributeConfig.getProperties()) {
......@@ -495,6 +496,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
if (attEs.getProperties().size() != attributeConfig.getProperties().size()) {
throw new FormatEx("Properties size doesn't match");
}
attEs.setName(attributeConfig.getName());
found = false;
for (Property p : attEs.getProperties()) {
for (Property prop : attributeConfig.getProperties()) {
......@@ -514,6 +516,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
if (attGl.getProperties().size() != attributeConfig.getProperties().size()) {
throw new FormatEx("Properties size doesn't match");
}
attGl.setName(attributeConfig.getName());
found = false;
for (Property p : attGl.getProperties()) {
for (Property prop : attributeConfig.getProperties()) {
......
......@@ -192,8 +192,8 @@ public class TreeBuilder {
categoricNodeMatch = categoricNode.matcher(line);
categoricNodeAuxMatch = categoricNodeAux.matcher(line);
if(prevLevel > level && pending.get(prevLevel) instanceof CategoricNode ) {
if(((CategoricNode) pending.get(prevLevel)).getChildren().size()
if (prevLevel > level && pending.get(prevLevel) instanceof CategoricNode) {
if (((CategoricNode) pending.get(prevLevel)).getChildren().size()
== ((CategoricNode) pending.get(prevLevel)).getAttribute().getProperties().size()
|| ((CategoricNode) pending.get(level - 1)).containsNotEqual()) {
pending.remove(prevLevel);
......@@ -687,19 +687,6 @@ public class TreeBuilder {
node = new ConsequentNode(c.copy());
}
((CategoricNode) pending.get(level)).addChild(categoricNodeAuxMatch.group(2) + ":" + categoricNodeAuxMatch.group(3), node);
if(pending.get(level - 1) != null){
if (((CategoricNode) pending.get(level - 1)).getChildren().size()
== ((CategoricNode) pending.get(level - 1)).getAttribute().getProperties().size()
|| ((CategoricNode) pending.get(level - 1)).containsNotEqual()) {
pending.remove(level - 1);
}
} else {
if (((CategoricNode) pending.get(level)).getChildren().size()
== ((CategoricNode) pending.get(level)).getAttribute().getProperties().size()
|| ((CategoricNode) pending.get(level)).containsNotEqual()) {
pending.remove(level);
}
}
} else {
throw new FormatEx("Wrong tree format");
}
......@@ -782,7 +769,7 @@ public class TreeBuilder {
}
public VisualNode buildVisualNode(VisualNode vnode, Node node, DatasetConfig config) {
public VisualNode buildVisualNode(VisualNode vnode, Node node, DatasetConfig config, String lang) {
DecimalFormat df = new DecimalFormat("#.##");
VisualNode aux;
String label;
......@@ -791,24 +778,45 @@ public class TreeBuilder {
vnode.setName(config.getAttributeById(((NumericNode) node).getAttribute().getId()).getName());
label = ((NumericNode) node).getComparisonSymbol() + " " + df.format(((NumericNode) node).getSplitValue());
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getLeftChild(), config));
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getLeftChild(), config, lang));
vnode.addChild(aux);
label = label.contains("<=") ? label.replace("<=", ">") : label.replace("<", ">=");
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getRightChild(), config));
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getRightChild(), config, lang));
vnode.addChild(aux);
} else if (node instanceof CategoricNode) {
vnode.setName(config.getAttributeById(((CategoricNode) node).getAttribute().getId()).getName());
for (Map.Entry<String, Node> entry : ((CategoricNode) node).getChildren().entrySet()) {
CategoricProperty prop = (CategoricProperty) config.getAttributeById(
((CategoricNode) node).getAttribute().getId()).getPropertyByName(entry.getKey());
((CategoricNode) node).getAttribute().getId()).getPropertyByName(entry.getKey().replaceFirst("=:|!=:", ""));
label = "";
if (entry.getKey().startsWith("=")) {
label = "is " + entry.getKey().replaceFirst("=:", "");
switch (lang) {
case "en":
label = "is " + prop.getValue();
break;
case "es":
label = "es " + prop.getValue();
break;
case "gl":
label = "é " + prop.getValue();
break;
}
} else {
label = "is not " + entry.getKey().replaceFirst("!=:", "");
switch (lang) {
case "en":
label = "is not " + prop.getValue();
break;
case "es":
label = "no es " + prop.getValue();
break;
case "gl":
label = "non é " + prop.getValue();
break;
}
}
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), entry.getValue(), config));
aux.addChild(buildVisualNode(new VisualNode(), entry.getValue(), config, lang));
vnode.addChild(aux);
}
} else {
......@@ -821,7 +829,7 @@ public class TreeBuilder {
}
public VisualNode buildVisualNode(VisualNode vnode, Node node, DatasetConfig config, ArrayList<Node> path) {
public VisualNode buildVisualNode(VisualNode vnode, Node node, DatasetConfig config, ArrayList<Node> path, String lang) {
DecimalFormat df = new DecimalFormat("#.##");
VisualNode aux;
String label;
......@@ -830,25 +838,46 @@ public class TreeBuilder {
vnode.setName(config.getAttributeById(((NumericNode) node).getAttribute().getId()).getName());
label = ((NumericNode) node).getComparisonSymbol() + " " + df.format(((NumericNode) node).getSplitValue());
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getLeftChild(), config, path));
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getLeftChild(), config, path, lang));
vnode.addChild(aux);
label = label.contains("<=") ? label.replace("<=", ">") : label.replace("<", ">=");
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getRightChild(), config, path));
aux.addChild(buildVisualNode(new VisualNode(), ((NumericNode) node).getRightChild(), config, path, lang));
vnode.addChild(aux);
vnode.setVisited(path.contains(node));
} else if (node instanceof CategoricNode) {
vnode.setName(config.getAttributeById(((CategoricNode) node).getAttribute().getId()).getName());
for (Map.Entry<String, Node> entry : ((CategoricNode) node).getChildren().entrySet()) {
CategoricProperty prop = (CategoricProperty) config.getAttributeById(
((CategoricNode) node).getAttribute().getId()).getPropertyByName(entry.getKey());
((CategoricNode) node).getAttribute().getId()).getPropertyByName(entry.getKey().replaceFirst("=:|!=:", ""));
label = "";
if (entry.getKey().startsWith("=")) {
label = "is " + entry.getKey().replaceFirst("=:", "");
switch (lang) {
case "en":
label = "is " + prop.getValue();
break;
case "es":
label = "es " + prop.getValue();
break;
case "gl":
label = "é " + prop.getValue();
break;
}
} else {
label = "is not " + entry.getKey().replaceFirst("!=:", "");
switch (lang) {
case "en":
label = "is not " + prop.getValue();
break;
case "es":
label = "no es " + prop.getValue();
break;
case "gl":
label = "non é " + prop.getValue();
break;
}
}
aux = new VisualNode("", label, false);
aux.addChild(buildVisualNode(new VisualNode(), entry.getValue(), config, path));
aux.addChild(buildVisualNode(new VisualNode(), entry.getValue(), config, path, lang));
vnode.addChild(aux);
}
vnode.setVisited(path.contains(node));
......
......@@ -40,7 +40,7 @@ public class TreeInterpreter {
this.tree = tree;
}
public Classification classify(Instance instance, DatasetConfig config) {
public Classification classify(Instance instance, DatasetConfig config, String lang) {
ArrayList<Node> path = new ArrayList();
path.add(tree);
Attribute att;
......@@ -133,7 +133,21 @@ public class TreeInterpreter {
}
}
VisualNode treePath = this.treebuilder.buildVisualNode(new VisualNode(), this.tree, config, path);
if (this.tree instanceof ConsequentNode) {
if (instance.getSolution() != null) {
if (((ConsequentNode) this.tree).getConsequent().getId().equals(instance.getSolution())) {
classification.setState("correct");
} else {
classification.setState("incorrect");
}
} else {
classification.setState("unknown");
}
classification.setConsequent(((ConsequentNode) this.tree).getConsequent());
classification.setInstance(instance);
}
VisualNode treePath = this.treebuilder.buildVisualNode(new VisualNode(), this.tree, config, path, lang);
classification.setTree(treePath);
......@@ -141,7 +155,7 @@ public class TreeInterpreter {
}
public ArrayList<Classification> exploreAlternatives(Instance instance, DatasetConfig config, double percentageMargin) {
public ArrayList<Classification> exploreAlternatives(Instance instance, DatasetConfig config, double percentageMargin, String lang) {
ArrayList<Node> path = new ArrayList();
path.add(tree);
Attribute att;
......@@ -291,7 +305,7 @@ public class TreeInterpreter {
}
classification.setConsequent(((ConsequentNode) path.get(path.size() - 1)).getConsequent());
solutions.add(((ConsequentNode) path.get(path.size() - 1)).getConsequent().getId());
VisualNode treePath = this.treebuilder.buildVisualNode(new VisualNode(), this.tree, config, path);
VisualNode treePath = this.treebuilder.buildVisualNode(new VisualNode(), this.tree, config, path, lang);
classification.setTree(treePath);
classification.setInstance(alternatives.get(0));
classifications.add(classification);
......@@ -304,6 +318,23 @@ public class TreeInterpreter {
modifiedList.remove(0);
}
}
if (tree instanceof ConsequentNode) {
if (instance.getSolution() != null) {
if (((ConsequentNode) this.tree).getConsequent().getId().equals(instance.getSolution())) {
classification.setState("correct");
} else {
classification.setState("incorrect");
}
} else {
classification.setState("unknown");
}
classification.setConsequent(((ConsequentNode) this.tree).getConsequent());
classification.setInstance(instance);
VisualNode treePath = this.treebuilder.buildVisualNode(new VisualNode(), this.tree, config, path, lang);
classification.setTree(treePath);
alternatives.remove(0);
classifications.add(classification);
}
path.clear();
path.add(tree);
}
......
......@@ -55,9 +55,7 @@ public class DatasetManagerImpl implements DatasetManager {
for (String d : datasets) {
try {
datasetList.add(buildDataset(d, new File(this.fmanager.getBASELocation(token) + "/" + d + "/" + d + ".arff")));
} catch (IOException ex) {
throw new NotFoundEx("No datatsets found");
}
} catch (IOException ex) { }
}
}
}
......
......@@ -32,6 +32,7 @@ public class FormatChecker {
}
String att;
String newAtt;
String newLine;
ArrayList<String> modified = new ArrayList();
......@@ -39,30 +40,33 @@ public class FormatChecker {
if(!line.endsWith("}") && line.contains("'")){
att = line.split("'")[1];
newAtt = att.replaceAll("[ !#$%&\\()*+,./:;<=>?@\\[\\]^`{|}~`]", "-");
printWriter.println(line.split("'")[0] + "'" + newAtt + "'" + line.split("'" + newAtt + "'")[1]);
newLine = line.split("'")[0] + "'" + newAtt + "'" + line.split("'" + att + "'")[1];
printWriter.println(newLine);
}
else if(!line.endsWith("}")){
att = line.split("\\s+")[1];
newAtt = att.replaceAll("[ !#$%&\\()*+,./:;<=>?@\\[\\]^`{|}~`]", "-");
printWriter.println(line.split(" ")[0] + " " + newAtt + " " + line.split(newAtt)[1]);
newLine = line.split(" ")[0] + " " + newAtt + " " + line.split(att)[1];
printWriter.println(newLine);
}
else if (line.contains("'")){
else if (line.split("\\{")[0].contains("'")){
att = line.split("'")[1];
newAtt = att.replaceAll("[ !#$%&\\()*+,./:;<=>?@\\[\\]^`{|}~`]", "-");
printWriter.println(line.split("'")[0] + "'" + newAtt + "' " + line.split("'" + newAtt + "'")[1]);
newLine = line.split("'")[0] + "'" + newAtt + "' " + line.split("'" + att + "'")[1];
printWriter.println(newLine);
}
else {
att = line.split("\\s+")[1];
newAtt = att.replaceAll("[ !#$%&\\()*+,./:;<=>?@\\[\\]^`{|}~`]", "-");
printWriter.println(line.split(" ")[0] + " " + newAtt + " " + line.split(newAtt)[1]);
newLine = line.split(" ")[0] + " " + newAtt + " " + line.split(att)[1];
printWriter.println(newLine);
}
line = reader.readLine();
if(!att.equals(newAtt)){
modified.add(att);
}
printWriter.println(line);
line = reader.readLine();
}
while(line != null){
......
......@@ -73,34 +73,33 @@ public abstract class ClauseGenerator {
return phrase;
}
SPhraseSpec generateClauseSubjectElement(String determiner, NLGElement subject, String verb, boolean pluralVerb, String object, boolean pluralObject) {
public SPhraseSpec generateClauseSubjectElement(NLGElement subject, String verb, boolean plural, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setPlural(pluralVerb);
verbPhrase.setPlural(plural);
NPPhraseSpec subjectPhrase = this.getNlgFactory().createNounPhrase(subject);
subjectPhrase.setDeterminer(determiner);
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
objectPhrase.setPlural(pluralObject);
if(subject!=null){
phrase.setSubject(subjectPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setComplement(object); // Con object se coloca antes del verbo
}
else{
phrase = this.getNlgFactory().createClause(null, verbPhrase, objectPhrase);
}
return phrase;
}
}
public SPhraseSpec generateClauseSubjectElement(NLGElement subject, String verb, boolean plural, String object) {
public SPhraseSpec generateNegativeClauseSubjectElement(NLGElement subject, String verb, boolean plural, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setFeature(Feature.NEGATED, true);
verbPhrase.setPlural(plural);
NPPhraseSpec subjectPhrase = this.getNlgFactory().createNounPhrase(subject);
......@@ -110,7 +109,7 @@ public abstract class ClauseGenerator {
if(subject!=null){
phrase.setSubject(subjectPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setComplement(object); // Con object se coloca antes del verbo
}
else{
phrase = this.getNlgFactory().createClause(null, verbPhrase, objectPhrase);
......@@ -120,35 +119,46 @@ public abstract class ClauseGenerator {
}
public SPhraseSpec generateClause(String subject, String verb, boolean plural, String object) {
public SPhraseSpec generateClause(String subject, String verb, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setPlural(plural);
NPPhraseSpec subjectPhrase = this.getNlgFactory().createNounPhrase(subject);
if(subject!=null){
phrase.setSubject(subject);
phrase.setVerb(verb);
phrase.setComplement(object); // Con object se coloca antes del verbo
}
else{
phrase = this.getNlgFactory().createClause(null, verb, object);
}
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
return phrase;
}
public SPhraseSpec generateNegativeClause(String subject, String verb, String object) {
SPhraseSpec phrase = this.getNlgFactory().createClause();
VPPhraseSpec verbPhrase = this.getNlgFactory().createVerbPhrase(verb);
verbPhrase.setFeature(Feature.NEGATED, true);
if(subject!=null){
phrase.setSubject(subjectPhrase);
phrase.setVerbPhrase(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setSubject(subject);
phrase.setVerb(verbPhrase);
phrase.setComplement(object); // Con object se coloca antes del verbo
}
else{
phrase = this.getNlgFactory().createClause(null, verbPhrase, objectPhrase);
phrase = this.getNlgFactory().createClause(null, verb, object);
}
return phrase;
}
public SPhraseSpec generateClause(String determiner, String subject, String verb, boolean plural, String object) {
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);
subjectPhrase.setDeterminer(determiner);
NPPhraseSpec objectPhrase = this.getNlgFactory().createNounPhrase(object);
......@@ -296,7 +306,7 @@ public abstract class ClauseGenerator {
return phrase;
}
public SPhraseSpec generateComplentiserClause(SPhraseSpec phrase1, String compliment, NLGElement phrase2) {
phrase2.setFeature(Feature.COMPLEMENTISER, compliment);
phrase1.addComplement(phrase2);
......
......@@ -129,13 +129,20 @@ public class InfoExtractor {
return this.analyzer.getConfusionBetween(consequents.get(0).getMatrixPosition() - 1, consequents.get(1).getMatrixPosition() - 1);
}
public ArrayList<String> getWrongInstances(ArrayList<Consequent> consequents) {
public ArrayList<String> getWrongInstances(ArrayList<Consequent> consequents, String lang) {
ArrayList<String> instances = new ArrayList();
String label;
if(lang.equals("en"))
label = "Instance ";
else
label = "Instancia ";
ArrayList<Integer> wrongInstances = this.analyzer.getWrongInstances(consequents.get(0).getMatrixPosition() - 1, consequents.get(1).getMatrixPosition() - 1);
if (wrongInstances != null) {
for (Integer i : wrongInstances) {
instances.add("Instance " + i);
instances.add(label + i);
}
}
......@@ -213,6 +220,21 @@ public class InfoExtractor {
return null;
}
public String globalProbabilityHigh(int alternative) {
Consequent solution = this.classifications.get(0).getConsequent();
Consequent alt = this.classifications.get(alternative).getConsequent();
double percentage = this.analyzer.getConfusionBetween(solution.getMatrixPosition() - 1, alt.getMatrixPosition() - 1);
if (percentage >= this.global.getGlobalProbabilityCut()) {
return this.global.getLabelGlobalFor(percentage);
}
return null;
}
public String branchProbabilityLow(int alternative) {
......@@ -225,8 +247,23 @@ public class InfoExtractor {
return null;
}
public String globalProbabilityLow(int alternative) {
Consequent solution = this.classifications.get(0).getConsequent();
Consequent alt = this.classifications.get(alternative).getConsequent();