From 98792a00210c0fba1a2e6d463d4211b64e6cccba Mon Sep 17 00:00:00 2001 From: dkohl Date: Mon, 12 Mar 2012 22:08:09 -0400 Subject: [PATCH] CLeaning up --- src/dkohl/bayes/builders/FoodNetBuilder.java | 127 ++++++++++--------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/src/dkohl/bayes/builders/FoodNetBuilder.java b/src/dkohl/bayes/builders/FoodNetBuilder.java index 249e829..9bc22ad 100644 --- a/src/dkohl/bayes/builders/FoodNetBuilder.java +++ b/src/dkohl/bayes/builders/FoodNetBuilder.java @@ -25,7 +25,6 @@ public class FoodNetBuilder { public static final String TASTE = "Taste"; 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_VEGETABLE = "Vegetable"; @@ -33,9 +32,6 @@ public class FoodNetBuilder { public static final String CONTAINS_PORK = TYPE.PORK.toString(); public static final String CONTAINS_TOMATOS = TYPE.TOMATO.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 FALSE_VALUE = "false"; @@ -45,24 +41,24 @@ public class FoodNetBuilder { public static final String RATING_DOMAIN[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; - private static final String[] VARIABLES = { SOMEONE_VEGETARIAN, /* - * SOMEONE_ALLERGIC_NUTS - * , - */ + private static final String[] VARIABLES = { SOMEONE_VEGETARIAN, CONTAINS_BEEF, CONTAINS_MEAT, CONTAINS_PORK, CONTAINS_POTATOS, - CONTAINS_TOMATOS, CONTAINS_VEGETABLE, /* - * CONTAINS_NUTS, - * CONTAINS_GENERIC_NUTS, - */TASTE }; + CONTAINS_TOMATOS, CONTAINS_VEGETABLE, TASTE }; 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() { HashSet classes = new HashSet(); classes.add(CONTAINS_MEAT); - // classes.add(CONTAINS_NUTS); classes.add(CONTAINS_VEGETABLE); Ontology ontology = new Ontology(classes); @@ -70,17 +66,30 @@ public class FoodNetBuilder { ontology.define(CONTAINS_PORK, CONTAINS_MEAT); ontology.define(CONTAINS_BEEF, CONTAINS_MEAT); - // ontology.define(CONTAINS_GENERIC_NUTS, CONTAINS_NUTS); - ontology.define(CONTAINS_TOMATOS, CONTAINS_VEGETABLE); ontology.define(CONTAINS_POTATOS, CONTAINS_VEGETABLE); return ontology; } + /** + * Build an assignment given a variable and a value + * @param varible + * @param value + * @return + */ private static Assignment build(String varible, String 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) { // resolve onthology DataPoint normPoint = new DataPoint(point); @@ -103,7 +112,16 @@ public class FoodNetBuilder { } return normPoint; } - + + /** + * Build training data from survey. + * + * @param survey + * @param recipeBook + * @param startIndex + * @param endIndex + * @return + */ public static DataSet getSurveyDataSet(Survey survey, RecipeBook recipeBook, int startIndex, int endIndex) { DataSet data = FoodExampleBuilder.examples(); @@ -120,6 +138,16 @@ public class FoodNetBuilder { 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, String recipeName, int weight) { DataPoint point = new DataPoint(); @@ -153,17 +181,19 @@ public class FoodNetBuilder { point.add(build(CONTAINS_TOMATOS, TRUE_VALUE)); } else { 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)); return point; } + /** + * Vegetarian control variable + * + * @param survey + * @return + */ public static ProbabilityDistribution vegi(Survey survey) { String names[] = {CONTAINS_MEAT, SOMEONE_VEGETARIAN}; ProbabilityTable table = new ProbabilityTable(names); @@ -173,18 +203,11 @@ public class FoodNetBuilder { table.setProbabilityForAssignment("false;false;", new Probability(1)); return table; } - - /* - * public static ProbabilityDistribution allergicNuts(Survey survey) { - * 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; } + + /** + * Initialize probability distributions */ - + public static ProbabilityDistribution beef() { String names[] = { CONTAINS_MEAT, CONTAINS_BEEF }; ProbabilityTable table = new ProbabilityTable(names); @@ -203,16 +226,6 @@ public class FoodNetBuilder { 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() { String names[] = { CONTAINS_VEGETABLE, CONTAINS_TOMATOS }; ProbabilityTable table = new ProbabilityTable(names); @@ -241,32 +254,20 @@ public class FoodNetBuilder { public static BayesNet createDishNet(Survey survey, RecipeBook recipeBook, int startIndex, int endIndex) { BayesNet net = new BayesNet(VARIABLES); - - // net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN), - // vegi(survey)); - // net.setDistribution(new Variable(SOMEONE_ALLERGIC_NUTS, DOMAIN), - // allergicNuts(survey)); - + + // initialize distributions 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(CONTAINS_VEGETABLE, DOMAIN), - vegetables()); + net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN),vegi(survey)); + net.setDistribution(new Variable(CONTAINS_VEGETABLE, DOMAIN),vegetables()); net.setDistribution(new Variable(CONTAINS_BEEF, DOMAIN), beef()); net.setDistribution(new Variable(CONTAINS_PORK, DOMAIN), pork()); net.setDistribution(new Variable(CONTAINS_POTATOS, DOMAIN), potatos()); 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()); Ontology ontology = createOntology(); - // for (String category : ontology.getClasses()) { - // net.connect(category, SOMEONE_VEGETARIAN); - // /*net.connect(category, SOMEONE_ALLERGIC_NUTS);*/ - // } + + // Build net net.connect(SOMEONE_VEGETARIAN, CONTAINS_MEAT); for (String thing : OBSERVED) {