Merge branch 'master' of woodyfolsom.net:/opt/git/cs6601p2
This commit is contained in:
@@ -25,7 +25,6 @@ public class FoodNetBuilder {
|
|||||||
public static final String TASTE = "Taste";
|
public static final String TASTE = "Taste";
|
||||||
|
|
||||||
public static final String SOMEONE_VEGETARIAN = "vegetarian";
|
public static final String SOMEONE_VEGETARIAN = "vegetarian";
|
||||||
// public static final String SOMEONE_ALLERGIC_NUTS = "allergic-nuts";
|
|
||||||
|
|
||||||
public static final String CONTAINS_MEAT = "Meat";
|
public static final String CONTAINS_MEAT = "Meat";
|
||||||
public static final String CONTAINS_VEGETABLE = "Vegetable";
|
public static final String CONTAINS_VEGETABLE = "Vegetable";
|
||||||
@@ -33,9 +32,6 @@ public class FoodNetBuilder {
|
|||||||
public static final String CONTAINS_PORK = TYPE.PORK.toString();
|
public static final String CONTAINS_PORK = TYPE.PORK.toString();
|
||||||
public static final String CONTAINS_TOMATOS = TYPE.TOMATO.toString();
|
public static final String CONTAINS_TOMATOS = TYPE.TOMATO.toString();
|
||||||
public static final String CONTAINS_POTATOS = TYPE.POTATO.toString();
|
public static final String CONTAINS_POTATOS = TYPE.POTATO.toString();
|
||||||
// public static final String CONTAINS_NUTS = "Nuts";
|
|
||||||
// public static final String CONTAINS_GENERIC_NUTS =
|
|
||||||
// TYPE.GENERIC_NUTS.toString();
|
|
||||||
|
|
||||||
public static final String TRUE_VALUE = "true";
|
public static final String TRUE_VALUE = "true";
|
||||||
public static final String FALSE_VALUE = "false";
|
public static final String FALSE_VALUE = "false";
|
||||||
@@ -45,24 +41,24 @@ public class FoodNetBuilder {
|
|||||||
public static final String RATING_DOMAIN[] = { "1", "2", "3", "4", "5",
|
public static final String RATING_DOMAIN[] = { "1", "2", "3", "4", "5",
|
||||||
"6", "7", "8", "9", "10" };
|
"6", "7", "8", "9", "10" };
|
||||||
|
|
||||||
private static final String[] VARIABLES = { SOMEONE_VEGETARIAN, /*
|
private static final String[] VARIABLES = { SOMEONE_VEGETARIAN,
|
||||||
* SOMEONE_ALLERGIC_NUTS
|
|
||||||
* ,
|
|
||||||
*/
|
|
||||||
CONTAINS_BEEF, CONTAINS_MEAT, CONTAINS_PORK, CONTAINS_POTATOS,
|
CONTAINS_BEEF, CONTAINS_MEAT, CONTAINS_PORK, CONTAINS_POTATOS,
|
||||||
CONTAINS_TOMATOS, CONTAINS_VEGETABLE, /*
|
CONTAINS_TOMATOS, CONTAINS_VEGETABLE, TASTE };
|
||||||
* CONTAINS_NUTS,
|
|
||||||
* CONTAINS_GENERIC_NUTS,
|
|
||||||
*/TASTE };
|
|
||||||
|
|
||||||
private static final String[] OBSERVED = { CONTAINS_BEEF, CONTAINS_PORK,
|
private static final String[] OBSERVED = { CONTAINS_BEEF, CONTAINS_PORK,
|
||||||
CONTAINS_POTATOS, CONTAINS_TOMATOS /* , CONTAINS_GENERIC_NUTS */};
|
CONTAINS_POTATOS, CONTAINS_TOMATOS };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the onthology:
|
||||||
|
*
|
||||||
|
* Ingredient -> category
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static Ontology createOntology() {
|
public static Ontology createOntology() {
|
||||||
HashSet<String> classes = new HashSet<String>();
|
HashSet<String> classes = new HashSet<String>();
|
||||||
|
|
||||||
classes.add(CONTAINS_MEAT);
|
classes.add(CONTAINS_MEAT);
|
||||||
// classes.add(CONTAINS_NUTS);
|
|
||||||
classes.add(CONTAINS_VEGETABLE);
|
classes.add(CONTAINS_VEGETABLE);
|
||||||
|
|
||||||
Ontology ontology = new Ontology(classes);
|
Ontology ontology = new Ontology(classes);
|
||||||
@@ -70,17 +66,30 @@ public class FoodNetBuilder {
|
|||||||
ontology.define(CONTAINS_PORK, CONTAINS_MEAT);
|
ontology.define(CONTAINS_PORK, CONTAINS_MEAT);
|
||||||
ontology.define(CONTAINS_BEEF, CONTAINS_MEAT);
|
ontology.define(CONTAINS_BEEF, CONTAINS_MEAT);
|
||||||
|
|
||||||
// ontology.define(CONTAINS_GENERIC_NUTS, CONTAINS_NUTS);
|
|
||||||
|
|
||||||
ontology.define(CONTAINS_TOMATOS, CONTAINS_VEGETABLE);
|
ontology.define(CONTAINS_TOMATOS, CONTAINS_VEGETABLE);
|
||||||
ontology.define(CONTAINS_POTATOS, CONTAINS_VEGETABLE);
|
ontology.define(CONTAINS_POTATOS, CONTAINS_VEGETABLE);
|
||||||
return ontology;
|
return ontology;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build an assignment given a variable and a value
|
||||||
|
* @param varible
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private static Assignment build(String varible, String value) {
|
private static Assignment build(String varible, String value) {
|
||||||
return new Assignment(new Variable(varible, DOMAIN), value);
|
return new Assignment(new Variable(varible, DOMAIN), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve onthology by adding category for
|
||||||
|
* each ingredient and add all categories
|
||||||
|
* that are not in a dish as false.
|
||||||
|
*
|
||||||
|
* @param point
|
||||||
|
* @param onto
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static DataPoint normalize(DataPoint point, Ontology onto) {
|
public static DataPoint normalize(DataPoint point, Ontology onto) {
|
||||||
// resolve onthology
|
// resolve onthology
|
||||||
DataPoint normPoint = new DataPoint(point);
|
DataPoint normPoint = new DataPoint(point);
|
||||||
@@ -104,6 +113,15 @@ public class FoodNetBuilder {
|
|||||||
return normPoint;
|
return normPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build training data from survey.
|
||||||
|
*
|
||||||
|
* @param survey
|
||||||
|
* @param recipeBook
|
||||||
|
* @param startIndex
|
||||||
|
* @param endIndex
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static DataSet getSurveyDataSet(Survey survey,
|
public static DataSet getSurveyDataSet(Survey survey,
|
||||||
RecipeBook recipeBook, int startIndex, int endIndex) {
|
RecipeBook recipeBook, int startIndex, int endIndex) {
|
||||||
DataSet data = FoodExampleBuilder.examples();
|
DataSet data = FoodExampleBuilder.examples();
|
||||||
@@ -120,6 +138,16 @@ public class FoodNetBuilder {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a data point with as rating.
|
||||||
|
* If something rated above a threshold
|
||||||
|
* it is true, else it is false
|
||||||
|
*
|
||||||
|
* @param recipeBook
|
||||||
|
* @param recipeName
|
||||||
|
* @param weight
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static DataPoint createDataPoint(RecipeBook recipeBook,
|
public static DataPoint createDataPoint(RecipeBook recipeBook,
|
||||||
String recipeName, int weight) {
|
String recipeName, int weight) {
|
||||||
DataPoint point = new DataPoint();
|
DataPoint point = new DataPoint();
|
||||||
@@ -153,17 +181,19 @@ public class FoodNetBuilder {
|
|||||||
point.add(build(CONTAINS_TOMATOS, TRUE_VALUE));
|
point.add(build(CONTAINS_TOMATOS, TRUE_VALUE));
|
||||||
} else {
|
} else {
|
||||||
point.add(build(CONTAINS_TOMATOS, FALSE_VALUE));
|
point.add(build(CONTAINS_TOMATOS, FALSE_VALUE));
|
||||||
} }
|
}
|
||||||
/*
|
}
|
||||||
* if (ingredients.contains(TYPE.GENERIC_NUTS)) {
|
|
||||||
* point.add(build(CONTAINS_GENERIC_NUTS, TRUE_VALUE)); }
|
|
||||||
*/
|
|
||||||
|
|
||||||
point.add(build(TASTE, "" + weight));
|
point.add(build(TASTE, "" + weight));
|
||||||
|
|
||||||
return point;
|
return point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vegetarian control variable
|
||||||
|
*
|
||||||
|
* @param survey
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static ProbabilityDistribution vegi(Survey survey) {
|
public static ProbabilityDistribution vegi(Survey survey) {
|
||||||
String names[] = {CONTAINS_MEAT, SOMEONE_VEGETARIAN};
|
String names[] = {CONTAINS_MEAT, SOMEONE_VEGETARIAN};
|
||||||
ProbabilityTable table = new ProbabilityTable(names);
|
ProbabilityTable table = new ProbabilityTable(names);
|
||||||
@@ -174,15 +204,8 @@ public class FoodNetBuilder {
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* public static ProbabilityDistribution allergicNuts(Survey survey) {
|
* Initialize probability distributions
|
||||||
* String names[] = { SOMEONE_ALLERGIC_NUTS }; ProbabilityTable table = new
|
|
||||||
* ProbabilityTable(names); if (survey.isDiner("allergic-nuts")) {
|
|
||||||
* table.setProbabilityForAssignment("true;", new Probability(1));
|
|
||||||
* table.setProbabilityForAssignment("false;", new Probability(0)); } else {
|
|
||||||
* table.setProbabilityForAssignment("true;", new Probability(0));
|
|
||||||
* table.setProbabilityForAssignment("false;", new Probability(1)); } return
|
|
||||||
* table; }
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static ProbabilityDistribution beef() {
|
public static ProbabilityDistribution beef() {
|
||||||
@@ -203,16 +226,6 @@ public class FoodNetBuilder {
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* public static ProbabilityDistribution genericNuts() { String names[] = {
|
|
||||||
* CONTAINS_NUTS, CONTAINS_GENERIC_NUTS }; ProbabilityTable table = new
|
|
||||||
* ProbabilityTable(names); return table; }
|
|
||||||
*
|
|
||||||
* public static ProbabilityDistribution nuts() { String names[] = {
|
|
||||||
* SOMEONE_ALLERGIC_NUTS, CONTAINS_NUTS }; ProbabilityTable table = new
|
|
||||||
* ProbabilityTable(names); return table; }
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static ProbabilityDistribution tomatos() {
|
public static ProbabilityDistribution tomatos() {
|
||||||
String names[] = { CONTAINS_VEGETABLE, CONTAINS_TOMATOS };
|
String names[] = { CONTAINS_VEGETABLE, CONTAINS_TOMATOS };
|
||||||
ProbabilityTable table = new ProbabilityTable(names);
|
ProbabilityTable table = new ProbabilityTable(names);
|
||||||
@@ -242,31 +255,19 @@ public class FoodNetBuilder {
|
|||||||
int startIndex, int endIndex) {
|
int startIndex, int endIndex) {
|
||||||
BayesNet net = new BayesNet(VARIABLES);
|
BayesNet net = new BayesNet(VARIABLES);
|
||||||
|
|
||||||
// net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN),
|
// initialize distributions
|
||||||
// vegi(survey));
|
|
||||||
// net.setDistribution(new Variable(SOMEONE_ALLERGIC_NUTS, DOMAIN),
|
|
||||||
// allergicNuts(survey));
|
|
||||||
|
|
||||||
net.setDistribution(new Variable(CONTAINS_MEAT, DOMAIN), meat());
|
net.setDistribution(new Variable(CONTAINS_MEAT, DOMAIN), meat());
|
||||||
// net.setDistribution(new Variable(CONTAINS_NUTS, DOMAIN), nuts());
|
net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN),vegi(survey));
|
||||||
net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN),
|
net.setDistribution(new Variable(CONTAINS_VEGETABLE, DOMAIN),vegetables());
|
||||||
vegi(survey));
|
|
||||||
net.setDistribution(new Variable(CONTAINS_VEGETABLE, DOMAIN),
|
|
||||||
vegetables());
|
|
||||||
net.setDistribution(new Variable(CONTAINS_BEEF, DOMAIN), beef());
|
net.setDistribution(new Variable(CONTAINS_BEEF, DOMAIN), beef());
|
||||||
net.setDistribution(new Variable(CONTAINS_PORK, DOMAIN), pork());
|
net.setDistribution(new Variable(CONTAINS_PORK, DOMAIN), pork());
|
||||||
net.setDistribution(new Variable(CONTAINS_POTATOS, DOMAIN), potatos());
|
net.setDistribution(new Variable(CONTAINS_POTATOS, DOMAIN), potatos());
|
||||||
net.setDistribution(new Variable(CONTAINS_TOMATOS, DOMAIN), tomatos());
|
net.setDistribution(new Variable(CONTAINS_TOMATOS, DOMAIN), tomatos());
|
||||||
// net.setDistribution(new Variable(CONTAINS_GENERIC_NUTS, DOMAIN),
|
|
||||||
// genericNuts());
|
|
||||||
|
|
||||||
net.setDistribution(new Variable(TASTE, RATING_DOMAIN), taste());
|
net.setDistribution(new Variable(TASTE, RATING_DOMAIN), taste());
|
||||||
|
|
||||||
Ontology ontology = createOntology();
|
Ontology ontology = createOntology();
|
||||||
// for (String category : ontology.getClasses()) {
|
|
||||||
// net.connect(category, SOMEONE_VEGETARIAN);
|
// Build net
|
||||||
// /*net.connect(category, SOMEONE_ALLERGIC_NUTS);*/
|
|
||||||
// }
|
|
||||||
net.connect(SOMEONE_VEGETARIAN, CONTAINS_MEAT);
|
net.connect(SOMEONE_VEGETARIAN, CONTAINS_MEAT);
|
||||||
|
|
||||||
for (String thing : OBSERVED) {
|
for (String thing : OBSERVED) {
|
||||||
|
|||||||
Reference in New Issue
Block a user