edu.cmu.sv.database.ActionEnumeration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoda Show documentation
Show all versions of yoda Show documentation
A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).
package edu.cmu.sv.database;
import edu.cmu.sv.dialog_state_tracking.DiscourseUnit;
import edu.cmu.sv.domain.ontology.Noun;
import edu.cmu.sv.domain.ontology.Role;
import edu.cmu.sv.domain.ontology.Verb;
import edu.cmu.sv.semantics.SemanticsModel;
import edu.cmu.sv.system_action.dialog_act.DialogAct;
import edu.cmu.sv.utils.Assert;
import edu.cmu.sv.utils.Combination;
import edu.cmu.sv.yoda_environment.MongoLogHandler;
import edu.cmu.sv.yoda_environment.YodaEnvironment;
import org.json.simple.JSONObject;
import org.openrdf.query.*;
import org.openrdf.repository.RepositoryException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by David Cohen on 12/6/14.
*/
public class ActionEnumeration {
public static int maxOntologyBindings = 2;
public static int maxIndividualBindings = 10;
public static enum FOCUS_CONSTRAINT {IN_FOCUS, IN_KB}
public static enum ENUMERATION_TYPE {EXHAUSTIVE, SAMPLED}
public static FOCUS_CONSTRAINT focusConstraint;
public static ENUMERATION_TYPE enumerationType;
public static Set> getPossibleIndividualBindings(DialogAct dialogAct,
YodaEnvironment yodaEnvironment) {
if (dialogAct.getIndividualParameters().size() == 0) {
Set> ans = new HashSet<>();
ans.add(new HashMap<>());
return ans;
}
String variableEnumerationString = "";
String classConstraintString = "";
String focusConstraintString = "";
for (String parameter : dialogAct.getIndividualParameters().keySet()) {
variableEnumerationString += "?" + parameter + " ";
classConstraintString += "?" + parameter + " rdf:type base:" +
((Noun)dialogAct.getIndividualParameters().get(parameter)).name + " .\n";
if (focusConstraint == FOCUS_CONSTRAINT.IN_FOCUS)
focusConstraintString += "?" + parameter + " rdf:type dst:InFocus .\n";
}
String queryString = Database.prefixes + "SELECT DISTINCT " + variableEnumerationString + "WHERE {\n";
queryString += focusConstraintString;
queryString += classConstraintString;
queryString += "}";
if (enumerationType.equals(ENUMERATION_TYPE.SAMPLED))
queryString += " LIMIT " + maxIndividualBindings;
Set> ans = new HashSet<>();
synchronized (yodaEnvironment.db.connection) {
yodaEnvironment.db.log(queryString);
Database.getLogger().info(MongoLogHandler.createSimpleRecord("action enumeration query", queryString).toJSONString());
try {
TupleQuery query = yodaEnvironment.db.connection.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = query.evaluate();
while (result.hasNext()) {
HashMap binding = new HashMap<>();
BindingSet bindings = result.next();
for (String variable : bindings.getBindingNames()) {
binding.put(variable, bindings.getValue(variable).stringValue());
}
ans.add(binding);
}
// queryString = Database.prefixes + "SELECT DISTINCT ?x WHERE { ?x rdf:type base:Noun .}";
// query = yodaEnvironment.db.connection.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// result = query.evaluate();
//
// while (result.hasNext()) {
// Map binding = new HashMap<>();
// BindingSet bindings = result.next();
// for (String variable : bindings.getBindingNames()) {
// binding.put(variable, bindings.getValue(variable).stringValue());
// }
// ans.add(binding);
// }
//
//
// System.out.println("lenght of ans:"+ans.size());
result.close();
} catch (RepositoryException | QueryEvaluationException | MalformedQueryException e) {
e.printStackTrace();
System.exit(0);
}
}
return ans;
}
public static Set