-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecipeDataSet.go
executable file
·182 lines (140 loc) · 3.88 KB
/
RecipeDataSet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* TODO:
* 1. Store more of the meta data rather then recalculating it
*/
// RecipeDataSet
package main
import (
"math"
)
type Data struct {
recipes []*Recipe
//Some meta-data for ingredients
ingredientInfo map[string]*ingredientEntry
//The number of unique ingredients
ingredientCount int
}
type ingredientEntry struct {
//The name of the ingredient
ingredient string
//The number of times this ingredient appears accross all recipes and cuisines
count int
//The number of times this ingredient appears in each cuisine
cuisineCounts []int
//TODO
entropy float64
}
func newDataSet() *Data {
var ds Data
ds.ingredientInfo = make(map[string]*ingredientEntry)
ds.recipes = make([]*Recipe, 0)
return &ds
}
func (ds *Data) addRecipe(newRecipe *Recipe) {
for _, ingr := range newRecipe.getIngredients() {
if val, found := ds.ingredientInfo[ingr]; found {
val.incrementCount(newRecipe.Cuisine)
} else {
ds.ingredientInfo[ingr] = newIngredientEntry(ingr)
}
ds.ingredientCount++
}
ds.recipes = append(ds.recipes, newRecipe)
}
func (ds *Data) calculateMetaData() {
for _, val := range ds.ingredientInfo {
val.calculateEntropy()
}
}
//Weight just uses one ingredients entropy
func (ds *Data) getEntropyWeight(recpipeID int) float64 {
var entropy float64 = 2
for _, ingr := range ds.recipes[recpipeID].getIngredients() {
if ds.ingredientInfo[ingr].entropy < entropy {
entropy = ds.ingredientInfo[ingr].entropy
}
}
return 1.5 - entropy
}
func (ds *Data) getMaxDiffProb(ingrName string) float64 {
ie := ds.ingredientInfo[ingrName]
min := 99999.0
max := -999999.0
for i := 1; i <= 7; i++ {
current := ie.getProbability(i)
if current < min {
min = current
}
if current > max {
max = current
}
}
return max - min
}
func (ds *Data) getProbability(ingrName string, cuisine int) float64 {
ie := ds.ingredientInfo[ingrName]
return ie.getProbability(cuisine)
}
func (ds *Data) Weight(index int) float64 {
return ds.getEntropyWeight(index)
}
func (ds *Data) GetEntry(index int) interface{} {
return ds.recipes[index]
}
func (ds *Data) Size() int {
return len(ds.recipes)
}
func (ds *Data) GetClass(index int) int {
return ds.recipes[index].Cuisine
}
func newIngredientEntry(name string) *ingredientEntry {
ie := ingredientEntry{ingredient: name}
ie.cuisineCounts = make([]int, 8) //TODO remove hard coding
return &ie
}
func (ie *ingredientEntry) incrementCount(cuisine int) {
ie.count++
ie.cuisineCounts[cuisine]++
}
func (ie *ingredientEntry) getProbability(cuisine int) float64 {
return float64(ie.cuisineCounts[cuisine]) / float64(ie.count)
}
func (ie *ingredientEntry) calculateEntropy() float64 {
ie.entropy = 0
for _, cousineCount := range ie.cuisineCounts {
var pr float64 = float64(cousineCount) / float64(ie.count)
ie.entropy -= pr * math.Log2(pr)
}
return ie.entropy
}
//func (ds *Data) getMaxPr(recipeID int) float64 {
// min := 99999.0
// max := -999999.0
// for _, ingrName := range recp.getIngredients() {
// ingr := ds.ingredientInfo[ingrName]
// for _, count := range ingr.cuisineCounts {
// }
// }
// for (int i = 1; i <= 8; i++) {
// double current = getProbabilityForCuisine(i + 1);
// min = min < current ? min : current;
// max = max > current ? max : current;
// }
// return max - min;
//}
//func (ds *Data) getMaxCountDifference(recipeID int) float64 {
// recp := ds.recipes[recipeID]
// min := 100000
// max := 0
// for _, ingrName := range recp.getIngredients() {
// ingr := ds.ingredientInfo[ingrName]
// for _, count := range ingr.cuisineCounts {
// if count > 0 && count < min {
// min = count
// } else if count > max {
// max = count
// }
// }
// }
// return 0.5 + (float64(max-min) / float64(max))
//}