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

Engadidas probas a memoria, documentación de funcionalidade e nova version de explicacions na API

parent 817db1a8
......@@ -2,6 +2,7 @@ package brunolopez.expliclas.explainer;
import java.util.ArrayList;
import simplenlg.features.Feature;
import simplenlg.features.NumberAgreement;
import simplenlg.features.Tense;
import simplenlg.format.english.HTMLFormatter;
import simplenlg.framework.CoordinatedPhraseElement;
......@@ -9,6 +10,7 @@ import simplenlg.framework.DocumentElement;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import simplenlg.lexicon.Lexicon;
import simplenlg.phrasespec.NPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.phrasespec.VPPhraseSpec;
import simplenlg.realiser.english.Realiser;
......@@ -25,160 +27,224 @@ public class ClauseBuilder {
this.realiser = new Realiser(this.lexicon);
}
public SPhraseSpec generateClause(String clause){
public SPhraseSpec generateClause(String clause) {
SPhraseSpec s = this.nlgFactory.createClause();
s.addComplement(clause);
return s;
}
public SPhraseSpec generateClause(ArrayList<String> subject, String verb, ArrayList<String> object, boolean plural) {
SPhraseSpec phrase = this.nlgFactory.createClause();
VPPhraseSpec verbPhrase = this.nlgFactory.createVerbPhrase(verb);
CoordinatedPhraseElement subjectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement objectPhrase = this.nlgFactory.createCoordinatedPhrase();
if (subject.size() > 1) {
for (String s : subject) {
subjectPhrase.addCoordinate(s);
}
}
for (String s : object) {
objectPhrase.addCoordinate(s);
}
if (subject.size() > 1) {
phrase.setSubject(subjectPhrase);
} else {
NPPhraseSpec s = this.nlgFactory.createNounPhrase(subject.get(0));
s.setFeature(Feature.NUMBER, plural ? NumberAgreement.PLURAL : NumberAgreement.SINGULAR);
phrase.setSubject(s);
}
phrase.setVerb(verbPhrase);
phrase.setObject(objectPhrase);
return phrase;
}
public SPhraseSpec generateClause(ArrayList<String> subject, String verb, ArrayList<String> object, ArrayList<String> complement, boolean plural) {
SPhraseSpec phrase = this.nlgFactory.createClause();
VPPhraseSpec verbPhrase = this.nlgFactory.createVerbPhrase(verb);
CoordinatedPhraseElement subjectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement objectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement complementPhrase = this.nlgFactory.createCoordinatedPhrase();
for(String s: subject){
for (String s : subject) {
subjectPhrase.addCoordinate(s);
}
for(String s: object){
for (String s : object) {
objectPhrase.addCoordinate(s);
}
for(String s: complement){
for (String s : complement) {
complementPhrase.addCoordinate(s);
}
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setComplement(complementPhrase);
phrase.setPlural(plural);
return phrase;
}
public SPhraseSpec generateClauseObjectOr(ArrayList<String> subject, String verb, ArrayList<String> object, ArrayList<String> complement, boolean plural) {
SPhraseSpec phrase = this.nlgFactory.createClause();
VPPhraseSpec verbPhrase = this.nlgFactory.createVerbPhrase(verb);
CoordinatedPhraseElement subjectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement objectPhrase = this.nlgFactory.createCoordinatedPhrase();
objectPhrase.setConjunction("or");
CoordinatedPhraseElement complementPhrase = this.nlgFactory.createCoordinatedPhrase();
for (String s : subject) {
subjectPhrase.addCoordinate(s);
}
for (String s : object) {
objectPhrase.addCoordinate(s);
}
for (String s : complement) {
complementPhrase.addCoordinate(s);
}
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setComplement(complementPhrase);
phrase.setPlural(plural);
return phrase;
}
public SPhraseSpec generateClause(SPhraseSpec phrase1, CoordinatedPhraseElement phrase2, String union){
public SPhraseSpec generateClause(SPhraseSpec phrase1, CoordinatedPhraseElement phrase2, String union) {
phrase2.addPreModifier(union);
phrase1.addModifier(phrase2);
return phrase1;
}
public SPhraseSpec generateClause(SPhraseSpec phrase1, SPhraseSpec phrase2, String union){
public SPhraseSpec generateClause(SPhraseSpec phrase1, SPhraseSpec phrase2, String union) {
phrase2.setFeature(Feature.COMPLEMENTISER, union);
phrase2.setFeature(Feature.TENSE, Tense.PRESENT);
phrase1.addComplement(phrase2);
return phrase1;
}
public SPhraseSpec generateModalClause(ArrayList<String> subject, String verb, String modal, ArrayList<String> object, boolean plural){
public SPhraseSpec generateModalClause(ArrayList<String> subject, String verb, String modal, ArrayList<String> object, boolean plural) {
SPhraseSpec phrase = this.nlgFactory.createClause();
VPPhraseSpec verbPhrase = this.nlgFactory.createVerbPhrase(verb);
verbPhrase.setFeature(Feature.MODAL, modal);
CoordinatedPhraseElement subjectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement objectPhrase = this.nlgFactory.createCoordinatedPhrase();
for(String s: subject){
for (String s : subject) {
subjectPhrase.addCoordinate(s);
}
for(String s: object){
for (String s : object) {
objectPhrase.addCoordinate(s);
}
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setPlural(plural);
return phrase;
}
public SPhraseSpec generatePassiveClause(ArrayList<String> subject, String verb, ArrayList<String> object, boolean plural){
public SPhraseSpec generateComplentiserClause(SPhraseSpec phrase1, String compliment, SPhraseSpec phrase2) {
phrase2.setFeature(Feature.COMPLEMENTISER, compliment);
phrase1.addComplement(phrase2);
return phrase1;
}
public SPhraseSpec generatePassiveClause(ArrayList<String> subject, String verb, ArrayList<String> object, boolean plural) {
SPhraseSpec phrase = this.nlgFactory.createClause();
VPPhraseSpec verbPhrase = this.nlgFactory.createVerbPhrase(verb);
verbPhrase.setFeature(Feature.PASSIVE, true);
CoordinatedPhraseElement subjectPhrase = this.nlgFactory.createCoordinatedPhrase();
CoordinatedPhraseElement objectPhrase = this.nlgFactory.createCoordinatedPhrase();
for(String s: subject){
for (String s : subject) {
subjectPhrase.addCoordinate(s);
}
for(String s: object){
for (String s : object) {
objectPhrase.addCoordinate(s);
}
phrase.setSubject(subjectPhrase);
phrase.setVerb(verbPhrase);
phrase.setObject(objectPhrase);
phrase.setPlural(plural);
return phrase;
}
public DocumentElement getParagraph(ArrayList<SPhraseSpec> phrases){
public DocumentElement getParagraph(ArrayList<SPhraseSpec> phrases) {
DocumentElement p = this.nlgFactory.createParagraph();
for(SPhraseSpec s: phrases){
for (SPhraseSpec s : phrases) {
p.addComponent(s);
}
return p;
}
public DocumentElement getParagraph(DocumentElement element){
public DocumentElement getParagraph(DocumentElement element) {
DocumentElement p = this.nlgFactory.createParagraph();
p.addComponent(element);
return p;
}
public DocumentElement generateList(ArrayList<SPhraseSpec> phrases){
public DocumentElement generateList(ArrayList<SPhraseSpec> phrases) {
DocumentElement list = this.nlgFactory.createList();
for(SPhraseSpec s: phrases){
for (SPhraseSpec s : phrases) {
list.addComponent(this.nlgFactory.createListItem(s));
}
return list;
}
public DocumentElement getDocument(ArrayList<DocumentElement> paragraphs, String title){
public DocumentElement getDocument(ArrayList<DocumentElement> paragraphs, String title) {
DocumentElement doc = this.nlgFactory.createDocument(title);
for(DocumentElement p: paragraphs){
for (DocumentElement p : paragraphs) {
doc.addComponent(p);
}
return doc;
}
public String getRealisation(NLGElement element){
public String getRealisation(NLGElement element) {
return this.realiser.realiseSentence(element);
}
public String getRealisation(DocumentElement doc) {
this.realiser.setFormatter(new HTMLFormatter());
return this.realiser.realise(doc).getRealisation();
}
public String getRealisation(SPhraseSpec phrase) {
return this.realiser.realiseSentence(phrase);
}
......
......@@ -36,14 +36,15 @@ public class InfoExtractor {
return this.problem.getType();
}
public String getGlobalPrecision(){
public float getGlobalPrecisionPercentage() {
return this.problem.getCorrectlyClassified();
}
public String getGlobalPrecision() {
Property p = this.problem.getConfig().getPropertyGlob(this.problem.getCorrectlyClassified());
return p.getValueTxt();
}
public int getConsequentsSize() {
return this.problem.getConsequents().size();
}
......@@ -124,7 +125,7 @@ public class InfoExtractor {
HashMap<String, ArrayList<String>> l = new HashMap<>();
HashMap<String, ArrayList<String>> sameLabel = new HashMap<>();
HashMap<ArrayList<String>, ArrayList<String>> labels = new HashMap<>();
for (Antecedent a : this.solution.getRules().get(0).getAntecedents()) {
for (Interval i : a.getIntervals()) {
add = l.get(a.getAttribute().getValueTxt());
......@@ -174,30 +175,29 @@ public class InfoExtractor {
} else {
sameLabel.put((String) pair.getKey(), new ArrayList<String>());
}
}
else if(!((String) pair.getKey()).equals((String) pair2.getKey())){
} else if (!((String) pair.getKey()).equals((String) pair2.getKey())) {
sameLabel.put((String) pair.getKey(), new ArrayList<String>());
}
}
}
}
it = sameLabel.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
ArrayList<String> atts = new ArrayList<>();
atts.add((String)pair.getKey());
for(String s: (ArrayList<String>)pair.getValue())
atts.add((String) pair.getKey());
for (String s : (ArrayList<String>) pair.getValue()) {
atts.add(s);
labels.put(atts, l.get((String)pair.getKey()));
}
labels.put(atts, l.get((String) pair.getKey()));
}
}
/*
} /*
else{
it = labels.entrySet().iterator();
......@@ -210,63 +210,63 @@ public class InfoExtractor {
labels.put(atts, (ArrayList<String>)pair.getValue());
}
}*/
else{
}*/ else {
it = l.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
ArrayList<String> atts = new ArrayList<>();
atts.add((String)pair.getKey());
labels.put(atts, (ArrayList<String>)pair.getValue());
atts.add((String) pair.getKey());
labels.put(atts, (ArrayList<String>) pair.getValue());
}
}
return labels;
}
public String getMainSolution(){
public String getMainSolution() {
return this.solution.getRules().get(0).getConsequent().getText();
}
public ArrayList<String> getAlternatives(){
public ArrayList<String> getAlternatives() {
ArrayList<String> alts = new ArrayList<>();
for(int i=1; i<this.solution.getRules().size(); i++){
for (int i = 1; i < this.solution.getRules().size(); i++) {
alts.add(this.solution.getRules().get(i).getConsequent().getText());
}
return alts;
}
public ArrayList<ArrayList<String>> getModifiedAntecedents(){
public ArrayList<ArrayList<String>> getModifiedAntecedents() {
ArrayList<ArrayList<String>> modified = new ArrayList<>();
ArrayList<String> list;
for(int i=1; i<this.solution.getRules().size(); i++){
for (int i = 1; i < this.solution.getRules().size(); i++) {
list = new ArrayList<>();
for(Antecedent a: this.solution.getRules().get(i).getAntecedents()){
if(a.getAttribute().isModified()){
for (Antecedent a : this.solution.getRules().get(i).getAntecedents()) {
if (a.getAttribute().isModified()) {
list.add(a.getAttribute().getValueTxt().toLowerCase());
}
}
if(!list.isEmpty())
if (!list.isEmpty()) {
modified.add(list);
}
}
return modified;
}
public HashMap<String, ArrayList<String>> getBranchProbs(){
public HashMap<String, ArrayList<String>> getBranchProbs() {
HashMap<String, ArrayList<String>> probs = new HashMap<>();
ArrayList<String> add;
for(int i=1; i<this.solution.getRules().size(); i++){
for (int i = 1; i < this.solution.getRules().size(); i++) {
Property p = this.problem.getConfig().getPropertyAlt(this.solution.getRules().get(i).getConsequent().getPercentage_tree());
add = probs.get(p.getValueTxt());
......@@ -278,13 +278,13 @@ public class InfoExtractor {
}
probs.put(p.getValueTxt(), add);
}
return probs;
}
public HashMap<String, ArrayList<String>> getGlobalProbs(){
public HashMap<String, ArrayList<String>> getGlobalProbs() {
HashMap<String, ArrayList<String>> probs = new HashMap<>();
ArrayList<String> add;
boolean found = false;
......@@ -305,19 +305,55 @@ public class InfoExtractor {
}
}
if (!found && rule.getConsequent().getCode() != this.solution.getRules().get(0).getConsequent().getCode()) {
Property p = this.problem.getConfig().getPropertyTree(rule.getConsequent().getPercentage_global());
add = probs.get(p.getValueTxt());
if (add != null) {
add.add(rule.getConsequent().getText());
} else {
add = new ArrayList<>();
add.add(rule.getConsequent().getText());
for (Consequent c : rule.getConsequent().getMistakes()) {
if (c.getCode() == this.solution.getRules().get(0).getConsequent().getCode()) {
Property p = this.problem.getConfig().getPropertyTree(c.getPercentage_global());
add = probs.get(p.getValueTxt());
if (add != null) {
add.add(rule.getConsequent().getText());
} else {
add = new ArrayList<>();
add.add(rule.getConsequent().getText());
}
probs.put(p.getValueTxt(), add);
break;
}
}
probs.put(p.getValueTxt(), add);
}
found = false;
}
return probs;
}
public float getPairConfusion(String pair1, String pair2) {
float[] code = new float[2];
for (Consequent c : this.problem.getConsequents()) {
if (c.getText().equals(pair1)) {
code[0] = c.getCode();
} else if (c.getText().equals(pair2)) {
code[1] = c.getCode();
}
}
return this.problem.getMatrix().classConfusion((int) code[0] - 1, (int) code[1] - 1);
}
public float usualValue(String consequent, String attribute) {
for (int i = 1; i < this.solution.getRules().size(); i++) {
if (consequent.equals(this.solution.getRules().get(i).getConsequent().getText())) {
for (Antecedent a : this.solution.getRules().get(i).getAntecedents()) {
if (a.getAttribute().getName().toLowerCase().equals(attribute)) {
return a.getAttribute().getValue();
}
}
}
}
return 0;
}
}
package brunolopez.expliclas.interpreter.solutionbuilder;
import brunolopez.expliclas.explainer.Config;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
* Solución ao problema de clasificación
......
......@@ -44,6 +44,7 @@
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://demos.citius.usc.es/ExpliClasAPI/api</param-value>
<!--<param-value>http://localhost:8080/api</param-value>-->
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
......
......@@ -42,6 +42,7 @@
// Build a system
const ui = SwaggerUIBundle({
url: "http://demos.citius.usc.es/ExpliClasAPI/api/swagger.json",
//url: "http://localhost:8080/api/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
......
......@@ -44,6 +44,7 @@
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://demos.citius.usc.es/ExpliClasAPI/api</param-value>
<!--<param-value>http://localhost:8080/api</param-value>-->
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
......
......@@ -42,6 +42,7 @@
// Build a system
const ui = SwaggerUIBundle({
url: "http://demos.citius.usc.es/ExpliClasAPI/api/swagger.json",
//url: "http://localhost:8080/api/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
......
#Generated by Maven
#Mon Jun 11 21:44:36 CEST 2018
#Wed Jun 13 21:41:11 CEST 2018
version=1.0
groupId=brunolopez
artifactId=expliclas-api