-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
95 lines (79 loc) · 2.39 KB
/
generator.py
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
# karl ramberg, paul ramberg, trenton morgan, isaih slater
# pickhacks 2019 - 3/1/19
import json
import random
import itertools
from Food import Food
from pprint import PrettyPrinter
pp = PrettyPrinter()
diet = []
altDiet = []
pairs = []
def getTokens(path):
return open(path).read().splitlines()
def setDiet():
for x in range(5):
if x == 0:
filename = "meats.txt"
elif x == 1:
filename = "carbs.txt"
elif x == 2:
filename = "vegetables.txt"
elif x == 3:
filename = "fruits.txt"
else:
filename = "spices.txt"
tokens = getTokens("food/" + filename)
for token in tokens:
if x == 0:
weight = 0.6
category = "meats"
probability = 0.05
elif x == 1:
weight = 0.35
category = "carbs"
probability = 0.05
elif x == 2:
weight = 0.15
category = "vegetables"
probability = 0.05
elif x == 3:
weight = 0.15
category = "fruits"
probability = 0.05
else:
weight = 0.05
category = "spices"
probability = 0.05
diet.append(Food(token, category, weight, probability))
return diet
def newRecipe():
recipe = []
altDiet = diet
totalWeight = 0.0
while totalWeight < 1.0 and len(recipe) < 5:
foodIndex = random.randint(0, len(altDiet)-1)
if(altDiet[foodIndex].probability > random.uniform(0.0, 1.0)):
findPairs(altDiet[foodIndex])
recipe.append(altDiet[foodIndex])
totalWeight += altDiet[foodIndex].weight
return json.dumps([ob.__dict__ for ob in recipe])
def addPairs(ingredients):
names = []
for ingredient in ingredients:
names.append(ingredient["name"])
pairs.append(list(itertools.combinations(names, 2)))
print(pairs)
def findPairs(food):
for pair in pairs:
if food.name in pair:
if pair[0] == food.name:
altDiet[findFood(pair[1])].probability += 0.1
else:
altDiet[findFood(pair[0])].probability += 0.1
def findFood(name):
for i in len(diet):
if diet[i].name == name:
return i
if __name__ == '__main__':
generateJsonFile()