generated from COP3530/P3-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipeStorage.cpp
652 lines (603 loc) · 22.5 KB
/
recipeStorage.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#include "recipeStorage.h"
#include <fstream>
#include <sstream>
/* read file in */
void recipeStorage::readFile() {
std::ifstream data("../RecipeNLG_dataset.csv");
// check if file opened successfully
if (!data.is_open()) {
std::cerr << "Error opening test file.\n";
}
// read and scrap first line which has no recipe data
string scrap;
std::getline(data, scrap);
// read all other recipe lines
string line;
string cell;
int indexTest=0;
while (std::getline(data, line)) {
string name;
std::stringstream lineStream(line); // put line into a string stream
recipeData* recipe = new recipeData; // new recipe struc allocated to heap
/* load title + save as var (comma to comma) */
std::getline(lineStream, scrap, ',');
std::getline(lineStream, name, ',');
recipe->recipeName = name;
/* load ingredients including measurements */
string fullIng = readBrackSeg(lineStream);
std::istringstream fullIngStream(fullIng);
bool moreIng = true;
while(moreIng) { // keep adding ingredients until end
string oneIng;
oneIng = readQuoteSeg(fullIngStream);
if(oneIng.empty()) {
moreIng = false;
}
else {
recipe->ingMeasurements.push_back(oneIng);
}
}
/* load directions */
string fullDir = readBrackSeg(lineStream);
std::istringstream dirStream(fullDir);
bool moreSteps = true;
int i = 1;
while(moreSteps) {
string oneStep;
oneStep = readQuoteSeg(dirStream);
// keep adding steps until the end
if(oneStep.empty()) {
moreSteps = false;
}
else {
string labeledStep = "(" + std::to_string(i ) + ") " + oneStep;
recipe->directions.push_back(labeledStep);
}
i++;
}
/* load NER aka ingredients and
* add to ingredient-recipe map */
string ingList = readBrackSeg(lineStream);
std::istringstream ingStream(ingList);
bool moreIngredients = true;
while(moreIngredients) { // keep adding ingredients until end
string oneIng;
oneIng = readQuoteSeg(ingStream);
if(oneIng.empty()) {
moreIngredients = false;
}
else {
recipe->ingList.insert(oneIng);
// add recipe to corresponding ingredient map
if (ingredientMap.find(oneIng) == ingredientMap.end()) {
unordered_set<string> recipeList;
recipeList.insert(recipe->recipeName);
ingredientMap[oneIng] = recipeList;
}
else {
//if(ingredientMap[oneIng])
ingredientMap[oneIng].insert(recipe->recipeName);
}
}
}
recipeMap[recipe->recipeName] = recipe;
indexTest++;
}
data.close();
}
/* read a segment starting with '[' and ending with ']' */
string recipeStorage::readBrackSeg(std::istream& input) {
std::string result;
char ch;
while (input.get(ch) && ch != '['); // read until '['
while (input.get(ch) && ch != ']') {
result += ch;
}
return result;
}
/* read a segment starting with '[' and ending with ']' */
string recipeStorage::readQuoteSeg(std::istringstream& inputStream) {
std::string result = "";
char ch;
while (inputStream.get(ch) && ch != '"'); // read until '['
inputStream.get(ch); // read second '"'
while (inputStream.get(ch) && ch != '"') {
result += ch;
}
inputStream.get(ch); // read second '"'
return result;
}
int recipeStorage::percentIngMatch(string recipe) {
int numChosenIng = 0;
for (string ingredient : recipeMap[recipe]->ingList) {
if (chosenIng.find(ingredient) != chosenIng.end()) {
numChosenIng++;
}
}
return ((100*numChosenIng) / recipeMap[recipe]->ingList.size());
}
void recipeStorage::ingreUpdater() {
chosenRecipe.clear();
// updates chosen recipes based on chosen and restricted ingredients
for (string ingredient : chosenIng) {
//for chosenRecipe
for (string recipe: ingredientMap[ingredient]) {
chosenRecipe.insert(recipe);
//std::cout << "Inserted" << std::endl;
}
}
for (string ingredient: restrictedIng) {
//for chosenRecipe
for (string recipe: ingredientMap[ingredient]) {
chosenRecipe.erase(recipe);
}
}
// clear all vectors
leastIngS.clear();
leastIngR.clear();
leastStepsS.clear();
leastStepsR.clear();
recipePercentS.clear();
recipePercentR.clear();
sortedRecipes.clear();
// sets up vectors for sorting recipes
for (string recipe : chosenRecipe) {
// least ingredient vectors
leastIngS.push_back({recipe, recipeMap[recipe]->ingList.size()});
leastIngR.push_back({recipe, recipeMap[recipe]->ingList.size()});
// least steps vectors
leastStepsS.push_back({recipe, recipeMap[recipe]->directions.size()});
leastStepsR.push_back({recipe, recipeMap[recipe]->directions.size()});
// best ingredient percent match vectors
recipePercentS.push_back({recipe, percentIngMatch(recipe)});
recipePercentR.push_back({recipe, percentIngMatch(recipe)});
// general vector
sortedRecipes.push_back(recipe);
}
}
void recipeStorage::generateRecipeSubset(vector<bool> chosenIngre, vector<bool> restrictedIngre) {
// order of ingredients
// 0.banana, 1.beef, 2.carrot, 3.cheese, 4.chicken, 5.coconut, 6.cucumber,
// 7.egg, 8.milk, 9.mushroom, 10.pork, 11.potato, 12.peanut butter, 13.strawberry, 14.tomato
chosenIngre.clear(); // reset quiz choices
restrictedIngre.clear();
if (chosenIngre[0]) { // add based off what the user has chosen to either chosen ing or restricted ing
chosenIng.insert("bananas");
}
if (chosenIngre[1]) {
chosenIng.insert("beef");
chosenIng.insert("ground beef");
chosenIng.insert("corned beef");
}
if (chosenIngre[2]) {
chosenIng.insert("carrot");
chosenIng.insert("carrots");
chosenIng.insert("carrot chunks");
}
if (chosenIngre[3]) {
chosenIng.insert("cheese");
chosenIng.insert("Cheddar cheese");
chosenIng.insert("cheddar cheese");
chosenIng.insert("Parmesan cheese");
chosenIng.insert("Mozzarella cheese");
chosenIng.insert("Ricotta cheese");
chosenIng.insert("American cheese");
chosenIng.insert("sharp cheese");
chosenIng.insert("grated cheese");
chosenIng.insert("cottage cheese");
}
if (chosenIngre[4]) {
chosenIng.insert("chicken");
chosenIng.insert("chicken breasts");
chosenIng.insert("chicken cutlets");
}
if (chosenIngre[5]) {
chosenIng.insert("coconut");
chosenIng.insert("moist coconut");
chosenIng.insert("flaked coconut");
}
if (chosenIngre[6]) {
chosenIng.insert("cucumbers");
}
if (chosenIngre[7]) {
chosenIng.insert("egg");
chosenIng.insert("eggs");
chosenIng.insert("egg yolk");
chosenIng.insert("egg white");
}
if (chosenIngre[8]) {
chosenIng.insert("milk");
chosenIng.insert("condensed milk");
chosenIng.insert("buttermilk");
}
if (chosenIngre[9]) {
chosenIng.insert("mushrooms");
chosenIng.insert("fresh mushrooms");
chosenIng.insert("cream of mushroom soup");
}
if (chosenIngre[11]) {
chosenIng.insert("pork");
}
if (chosenIngre[12]) {
chosenIng.insert("potatoes");
}
if (chosenIngre[10]) {
chosenIng.insert("peanut butter");
}
if (chosenIngre[13]) {
chosenIng.insert("strawberry");
chosenIng.insert("strawberries");
chosenIng.insert("strawberry jello");
}
if (chosenIngre[14]) {
chosenIng.insert("tomato");
chosenIng.insert("tomatoes");
chosenIng.insert("tomato paste");
chosenIng.insert("tomato juice");
chosenIng.insert("tomato sauce");
chosenIng.insert("Italian tomatoes");
}
// restricted ingredients
if (restrictedIngre[0]) {
restrictedIng.insert("bananas");
}
if (restrictedIngre[1]) {
restrictedIng.insert("beef");
restrictedIng.insert("ground beef");
restrictedIng.insert("corned beef");
restrictedIng.insert("beef stock");
restrictedIng.insert("beef bouillon");
restrictedIng.insert("Armour dried beef");
}
if (restrictedIngre[2]) {
restrictedIng.insert("carrot");
restrictedIng.insert("carrots");
restrictedIng.insert("carrot chunks");
}
if (restrictedIngre[3]) {
restrictedIng.insert("cheese");
restrictedIng.insert("Cheddar cheese");
restrictedIng.insert("cheddar cheese");
restrictedIng.insert("Parmesan cheese");
restrictedIng.insert("Mozzarella cheese");
restrictedIng.insert("Ricotta cheese");
restrictedIng.insert("American cheese");
restrictedIng.insert("sharp cheese");
restrictedIng.insert("grated cheese");
restrictedIng.insert("cottage cheese");
}
if (restrictedIngre[4]) {
restrictedIng.insert("cream of chicken soup");
restrictedIng.insert("chicken broth");
restrictedIng.insert("chicken");
restrictedIng.insert("chicken breasts");
restrictedIng.insert("chicken cutlets");
}
if (restrictedIngre[5]) {
restrictedIng.insert("coconut");
restrictedIng.insert("moist coconut");
restrictedIng.insert("flaked coconut");
}
if (restrictedIngre[6]) {
restrictedIng.insert("cucumbers");
}
if (restrictedIngre[7]) {
restrictedIng.insert("egg");
restrictedIng.insert("eggs");
restrictedIng.insert("egg yolk");
restrictedIng.insert("egg white");
}
if (restrictedIngre[8]) {
restrictedIng.insert("milk");
restrictedIng.insert("condensed milk");
restrictedIng.insert("buttermilk");
}
if (restrictedIngre[9]) {
restrictedIng.insert("mushrooms");
restrictedIng.insert("fresh mushrooms");
restrictedIng.insert("cream of mushroom soup");
}
if (restrictedIngre[10]) {
restrictedIng.insert("pork");
}
if (restrictedIngre[11]) {
restrictedIng.insert("potatoes");
}
if (restrictedIngre[12]) {
restrictedIng.insert("peanut butter");
}
if (restrictedIngre[13]) {
restrictedIng.insert("strawberry");
restrictedIng.insert("strawberries");
restrictedIng.insert("strawberry jello");
}
if (restrictedIngre[14]) {
restrictedIng.insert("tomato");
restrictedIng.insert("tomatoes");
restrictedIng.insert("tomato paste");
restrictedIng.insert("tomato juice");
restrictedIng.insert("tomato sauce");
restrictedIng.insert("Italian tomatoes");
}
this->ingreUpdater();
}
// sorting algorithms
/*
* used these sources to create the shell sort:
* https://www.programiz.com/dsa/shell-sort
* https://www.geeksforgeeks.org/shellsort/
* used these sources to create the radix (and counting) sort:
* https://www.programiz.com/dsa/radix-sort
* https://www.geeksforgeeks.org/counting-sort/
*/
double recipeStorage::leastIngShell() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//the gap starts at the vector size
int gap = leastIngS.size()/2;
while (gap > 0) {
//does an insertions sort for this gap size
for (int i = gap; i < leastIngS.size(); i++) {
pair<string, int> temp = leastIngS[i];
int j;
//second condition in the second statement makes it least to greatest
for (j = i; j >= gap && leastIngS[j-gap].second > temp.second; j -= gap) {
leastIngS[j] = leastIngS[j-gap];
}
leastIngS[j] = temp;
}
//gap halves through every passthrough
gap /= 2;
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < leastIngS.size(); i++) {
sortedRecipes[i] = leastIngS[i].first;
}
return duration; // returns time for sorting
}
double recipeStorage::leastIngRadix() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//finds the maximum number
int max = leastIngR[0].second;
for (pair<string, int> pairItr : leastIngR) {
max = std::max(max, pairItr.second);
}
// run helper sort function for each place of each recipe's number of ingredients
for (int placeVal = 1; max/placeVal > 0; placeVal *= 10) {
leastIngCountingSort(placeVal);
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < leastIngR.size(); i++) {
sortedRecipes[i] = leastIngR[i].first;
}
return duration; // returns time for sorting
}
void recipeStorage::leastIngCountingSort(int placeVal) {
int count[] = {0,0,0,0,0,0,0,0,0,0}; // one slot for each digit 0-9
// initialize array of same size and type of leastIng - used to temporarily store sorted results
//will be used to store the ouput that goes into leastFreq
pair<string, int> output[leastIngR.size()];
//gets the count of every element
for (int i = 0; i < leastIngR.size(); i++) {
count[(leastIngR[i].second / placeVal) % 10] = count[(leastIngR[i].second / placeVal) % 10] + 1; // increment count at the index of the digit
}
//makes the cumulative count and sorts the elements; this is where greatest to least or vice versa is decided
for (int j = 1; j < 10; j++) {
count[j] += count[j-1];
}
//elements are placed into the sorted order
for (int k = leastIngR.size()-1; k >= 0; k--) {
output[count[(leastIngR[k].second / placeVal) % 10] - 1] = leastIngR[k];
count[(leastIngR[k].second / placeVal) % 10]--;
}
//transfers output to leastIng
for (int l = 0; l < leastIngR.size(); l++) {
leastIngR[l] = output[l];
}
}
double recipeStorage::leastStepsShell() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//the gap starts at the vector size
int gap = leastStepsS.size();
while (gap > 0) {
//does an insertions sort for this gap size
for (int i = gap; i < leastStepsS.size(); i++) {
pair<string, int> temp = leastStepsS[i];
int j;
//second condition in the second statement makes it least to greatest
for (j = i; j > gap && leastStepsS[j-gap].second > temp.second; j -= gap) {
leastStepsS[j] = leastStepsS[j-gap];
}
leastStepsS[j] = temp;
}
//gap halves through every passthrough
gap /= 2;
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < leastStepsS.size(); i++) {
sortedRecipes[i] = leastStepsS[i].first;
}
return duration; // returns time for sorting
}
double recipeStorage::leastStepsRadix() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//finds the maximum number
int max = leastStepsR[0].second;
for (pair<string, int> pairItr : leastStepsR) {
max = std::max(max, pairItr.second);
}
// run helper sort function for each place of each recipe's number of ingredients
for (int placeVal = 1; max/placeVal > 0; placeVal *= 10) {
leastStepsCountingSort(placeVal);
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < leastStepsR.size(); i++) {
sortedRecipes[i] = leastStepsR[i].first;
}
return duration; // returns time for sorting
}
void recipeStorage::leastStepsCountingSort(int placeVal) {
int count[] = {0,0,0,0,0,0,0,0,0,0}; // one slot for each digit 0-9
// initialize array of same size and type of leastIng - used to temporarily store sorted results
// will be used to store the ouput that goes into leastSteps
pair<string, int> output[leastStepsR.size()];
//gets the count of every element
for (int i = 0; i < leastStepsR.size(); i++) {
count[(leastStepsR[i].second / placeVal) % 10] = count[(leastStepsR[i].second / placeVal) % 10] + 1;
}
//makes the cumulative count and sorts the elements; this is where greatest to least or vice versa is decided
for (int j = 1; j < 10; j++) {
count[j] += count[j-1];
}
//elements are placed into the sorted order
for (int k = leastStepsR.size()-1; k >= 0; k--) {
output[count[(leastStepsR[k].second / placeVal) % 10] - 1] = leastStepsR[k];
count[(leastStepsR[k].second / placeVal) % 10]--;
}
//transfers output to leastSteps
for (int l = 0; l < leastStepsR.size(); l++) {
leastStepsR[l] = output[l];
}
}
double recipeStorage::recipePercentShell() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//the gap starts at the vector size
int gap = recipePercentS.size();
while (gap > 0) {
//does an insertions sort for this gap size
for (int i = gap; i < recipePercentS.size(); i++) {
pair<string, int> temp = recipePercentS[i];
int j;
//second condition in the second statement makes it greatest to least
for (j = i; j >= gap && recipePercentS[j-gap].second < temp.second; j -= gap) {
recipePercentS[j] = recipePercentS[j-gap];
}
recipePercentS[j] = temp;
}
//gap halves through every passthrough
gap /= 2;
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < recipePercentS.size(); i++) {
sortedRecipes[i] = recipePercentS[i].first;
}
return duration; // returns the time for sorting
}
double recipeStorage::recipePercentRadix() {
//start the clock
auto start = std::chrono::high_resolution_clock::now();
//finds the maximum number
int max = recipePercentR[0].second;
for (pair<string, int> pairItr : recipePercentR) {
max = std::max(max, pairItr.second);
}
// run helper sort function for each place of each recipe's number of ingredients
for (int placeVal = 1; max/placeVal > 0; placeVal *= 10) {
recipePercentCountingSort(placeVal);
}
//stops the clock
auto stop = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count();
// updates general sorted recipes list
sortedRecipes.clear();
sortedRecipes.resize(chosenRecipe.size());
for (int i = 0; i < leastIngR.size(); i++) {
sortedRecipes[i] = leastIngR[i].first;
}
return duration; // returns time for sorting
}
void recipeStorage::recipePercentCountingSort(int placeVal) {
//initializes count
int count[] = {0,0,0,0,0,0,0,0,0,0}; // one slot for each digit 0-9
// initialize array of same size and type of recipePercent - used to temporarily store sorted results
//will be used to store the ouput that goes into recipePercent
pair<string, int> output[recipePercentR.size()];
//gets the count of every element
for (int i = 0; i < recipePercentR.size(); i++) {
count[(recipePercentR[i].second / placeVal) % 10] = count[(recipePercentR[i].second / placeVal) % 10] + 1; // increment count at the index of the digit
}
//makes the cumulative count and sorts the elements; this is where greatest to least or vice versa is decided
for (int j = 8; j >= 0; j--) {
count[j] += count[j+1];
}
//elements are placed into the sorted order
for (int k = recipePercentR.size()-1; k >= 0; k--) {
output[count[(recipePercentR[k].second / placeVal) % 10] - 1] = recipePercentR[k];
count[(recipePercentR[k].second / placeVal) % 10]--;
}
//transfers output to recipePercent
for (int l = 0; l < recipePercentR.size(); l++) {
recipePercentR[l] = output[l];
}
}
string recipeStorage::recipeDetails(string recipeName) { // formats and returns a selected recipe as one string
string details = "";
details += recipeName + "\n\n";
details += "Ingredients\n";
for (string ingredient : recipeMap[recipeName]->ingMeasurements) {
details += ingredient + "\n";
}
details += "\nDirections\n";
for (string direction : recipeMap[recipeName]->directions) {
while (direction.length() > 70) {
details += direction.substr(0, 70) + "\n";
direction = direction.substr(70);
}
details += direction + "\n";
}
return details;
}
string recipeStorage::halfRecipeDetails(string recipeName){ // formatting for a selected recipe as one string, the half card
string details = "";
details += recipeName + "\n\n";
details += "Ingredients\n";
int count = 0;
for (string ingredient : recipeMap[recipeName]->ingMeasurements) {
if(count > 3){
break;
}
while (ingredient.length() > 30) {
details += ingredient.substr(0, 30) + "\n";
ingredient = ingredient.substr(30);
}
details += ingredient + "\n";
count ++;
}
return details;
}
// destructor
void recipeStorage::destructMap() {
for (auto& pair : recipeMap) {
delete pair.second;
pair.second = nullptr;
}
}