-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuMaker.jl
156 lines (121 loc) · 4.01 KB
/
MenuMaker.jl
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
module MenuMaker
using PDFIO
using JuMP
using HiGHS
using ..Config
export makemenu
function makemenu(filename::AbstractString, config::Config.AutoMenu)
file = pdDocOpen(filename)
pagecount = pdDocGetPageCount(file)
menu = []
for i in 1:pagecount
if i ∉ config.schedule.breakfast_days && i ∉ config.schedule.lunch_days
continue
end
page = pdDocGetPage(file, i)
breakfast, lunch = parsepage(page, config.nutrition)
if i ∉ config.schedule.breakfast_days
breakfast = []
end
if i ∉ config.schedule.lunch_days
lunch = []
end
breakfast, lunch = calculateplan(breakfast, lunch, config.nutrition)
push!(menu, (breakfast=breakfast, lunch=lunch))
end
return menu
end
function parsepage(page::PDPage, config::Config.Nutrition)
text = sprint(pdPageExtractText, page)
text = replace(text, r"[\n ]{2,}" => ";", "," => ".")
breakfast = match(r"ЗАВТРАК;(.*);ОБЕД", text)[1]
breakfast = parsefoods(breakfast, config)
lunch = match(r"ОБЕД;(.*);ПОЛДНИК", text)[1]
lunch = parsefoods(lunch, config)
return breakfast, lunch
end
const foodpattern = r"([\d.]+) ([\d.]+) ([\d.]+) ([\d.]+);?([^\d;]+);\d+ руб\.[; ]?(\d+) (?>гр|мл)\."
function parsefoods(menu, config::Config.Nutrition)
foods = NamedTuple[]
for match in eachmatch(foodpattern, menu)
food = makefood(match.captures)
if !(food.name in config.blacklist)
push!(foods, food)
end
end
return foods
end
function makefood(captures::Vector)
values = map(x -> something(tryparse(Float64, x), x), captures)
size = values[6] / 100
return (
name=values[5],
calories=values[1] * size,
protein=values[2] * size,
fat=values[3] * size,
carbs=values[4] * size,
meat=ismeat(values[5])
)
end
const meat_words = [
"котлета",
"филе",
"стейк",
"терияки",
"печень",
"печёночные",
"треска",
"бифштекс",
"рыбные палочки",
"котлет",
"сайда",
"чахохбили",
"бефстроганов",
"наггетсы",
"гуляш",
"тефтели",
"форель",
"куриц",
"судак",
"грудка",
"индейки",
"говядин",
]
function ismeat(name)
lname = lowercase(name)
for word in meat_words
if occursin(word, lname)
return true
end
end
return false
end
function calculateplan(breakfast, lunch, config::Config.Nutrition)
model = Model(HiGHS.Optimizer)
set_silent(model)
include_breakfast = !isempty(breakfast)
include_lunch = !isempty(lunch)
meal_count = include_breakfast + include_lunch
breakfast_dishes, breakfast_calories, breakfast_protein = add_meal_constraints(model, breakfast, config)
lunch_dishes, lunch_calories, lunch_protein = add_meal_constraints(model, lunch, config)
@constraint(model, breakfast_calories + lunch_calories <= config.calories_range[2] * meal_count)
@constraint(model, breakfast_protein + lunch_protein >= config.min_protein * meal_count)
@objective(model, Min, sum(breakfast_dishes) + sum(lunch_dishes))
optimize!(model)
breakfast = breakfast[filter(i -> value(breakfast_dishes[i]) > 0.5, 1:end)]
lunch = lunch[filter(i -> value(lunch_dishes[i]) > 0.5, 1:end)]
return breakfast, lunch
end
function add_meal_constraints(model, meal, config)
if isempty(meal)
return [0], 0, 0
end
meal_range = 1:size(meal, 1)
dishes = @variable(model, [meal_range], Bin)
meal_calories = sum(meal[i].calories * dishes[i] for i in meal_range)
meal_protein = sum(meal[i].protein * dishes[i] for i in meal_range)
@constraint(model, meal_calories >= config.calories_range[1])
@constraint(model, sum(meal[i].meat * dishes[i] for i in meal_range) <= 2)
return dishes, meal_calories, meal_protein
end
end