-
Notifications
You must be signed in to change notification settings - Fork 5
/
build_recipe_mats.lua
663 lines (627 loc) · 28.2 KB
/
build_recipe_mats.lua
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
653
654
655
656
657
658
659
660
661
662
663
require('torch')
local stringx = require('pl.stringx')
torch.setdefaulttensortype('torch.LongTensor')
cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text('Build torch serialized version of recipes.')
cmd:text()
cmd:option('-inDirectory', '', 'The input recipe directory')
cmd:option('-useDictionary', '', 'Dictionary file')
cmd:option('-listFilename', '', 'The list of recipe filenames in the input directory')
cmd:option('-outDirectory', '', 'The output directory')
cmd:option('-dictionaryCutoff', 5, 'Cutoff for number of times a word must be seen.')
cmd:option('-info', 'emnlp', 'Versioning information')
cmd:option('-devDir', '', 'Dev directory, to ensure no dev titles are used when building training mats')
cmd:option('-testDir', '', 'Test directory, to ensure no test titles are used when building train/dev mats')
opt = cmd:parse(arg)
-----------------------------------------------------
-- Add a word to a dictionary.
-----------------------------------------------------
local function add_to_dictionary(dict, word_id, word)
if dict.symbol_to_index[word] == nil then
word_id = word_id + 1
dict.symbol_to_index[word] = word_id
dict.index_to_symbol[word_id] = word
end
return word_id
end
---------------------------------------------------
-- Count numbers of recipes of different lengths in
-- order to create tensors of the proper size.
---------------------------------------------------
local function count_recipe_lengths(counter)
local nrecipe = 1
counter.cantuse = {}
counter.cantusefull = {}
-- Read in dev directory titles in provided.
-- DO NOT provide for generating the dev set.
local cantuse_title_tokens = {}
if opt.devDir ~= '' then
local filelist = io.open(opt.listFilename, 'r')
for recipefilename in filelist:lines() do
local recipefile = io.open(opt.devDir .. recipefilename, 'r')
for line in recipefile:lines() do
if stringx.startswith(line, "Title:") or stringx.startswith(line, "title:") then
local title = string.sub(line, 8, -1)
local orig_title_tokens = stringx.split(stringx.strip(title:lower()))
local new_tokens = {}
for i=1,#orig_title_tokens do
local token = orig_title_tokens[i]
if token:find("[%a]") ~= nil and token:find("'s") == nil and token ~= 'and' and token ~= 'with' then
table.insert(new_tokens, token)
end
end
if #new_tokens ~= 0 then
table.sort(new_tokens, function(a, b) return a < b end)
local new_title_string = table.concat(new_tokens, " ")
counter.cantuse[new_title_string] = true
local tokens_by_name = {}
for i=1,#new_tokens do
tokens_by_name[new_tokens[i] ] = true
end
table.insert(cantuse_title_tokens, tokens_by_name)
end
end
end
end
end
-- Read in test directory titles in provided.
-- DO NOT provide for generating the test set.
if opt.testDir ~= '' then
local filelist = io.open(opt.listFilename, 'r')
for recipefilename in filelist:lines() do
local recipefile = io.open(opt.testDir .. recipefilename, 'r')
for line in recipefile:lines() do
if stringx.startswith(line, "Title:") or stringx.startswith(line, "title:") then
local title = string.sub(line, 8, -1)
-- Preprocess title to remove certain characters.
local orig_title_tokens = stringx.split(stringx.strip(title:lower()))
local new_tokens = {}
for i=1,#orig_title_tokens do
local token = orig_title_tokens[i]
if token:find("[%a]") ~= nil and token:find("'s") == nil and token ~= 'and' and token ~= 'with' then
table.insert(new_tokens, token)
end
end
if #new_tokens ~= 0 then
table.sort(new_tokens, function(a, b) return a < b end)
local new_title_string = table.concat(new_tokens, " ")
counter.cantuse[new_title_string] = true
local tokens_by_name = {}
for i=1,#new_tokens do
tokens_by_name[new_tokens[i] ] = true
end
table.insert(cantuse_title_tokens, tokens_by_name)
end
end
end
end
end
local filelist = io.open(opt.listFilename, 'r')
for recipefilename in filelist:lines() do
print(recipefilename)
local recipefile = io.open(opt.inDirectory .. recipefilename, 'r')
local sent = nil
local seen_pred = false
local step_table = {}
local recipe_len = 1
local title_tokens = {}
local ing_strings = nil
local switch_evidence = false
local title = nil
local original_title = nil
for line in recipefile:lines() do
if stringx.startswith(line, "Title:") or stringx.startswith(line, "title:") then
title = string.sub(line, 8, -1)
original_title = title
-- Preprocess title to remove certain characters.
local orig_title_tokens = stringx.split(stringx.strip(title:lower()))
for i=1,#orig_title_tokens do
local token = orig_title_tokens[i]
if token:find("[%a]") ~= nil and token:find("'s") == nil and token ~= 'and' and token ~= 'with' then
table.insert(title_tokens, token)
end
end
if #title_tokens ~= 0 then
table.sort(title_tokens, function(a, b) return a < b end)
local new_title_string = table.concat(title_tokens, " ")
if counter.cantuse[new_title_string] == true then
counter.cantusefull[original_title] = true
title = nil
else
local too_much_overlap = false
for c=1,#cantuse_title_tokens do
local bad_tokens = cantuse_title_tokens[c]
local overlap = 0
for i=1,#title_tokens do
if bad_tokens[title_tokens[i] ] == true then
overlap = overlap + 1
end
if overlap >= 3 then
too_much_overlap = true
break
end
end
if too_much_overlap then
break
end
end
if too_much_overlap then
counter.cantusefull[original_title] = true
title = nil
else
end
if title ~= nil then
for i=1,#title_tokens do
local token = title_tokens[i]
counter.title_counts[token] = (counter.title_counts[token] or 0) + 1
end
end
end
end
elseif stringx.startswith(line, "Categories:") or stringx.startswith(line, "categories:") then
elseif stringx.startswith(line, "Servings:") or stringx.startswith(line, "servings:") then
elseif stringx.strip(line) == "" then
elseif stringx.startswith(line, "Ingredients:") and title ~= nil then
local ingline = stringx.strip(string.sub(line, 14, -1)):lower()
local origingline = ingline
-- Preprocess ingredients to remove certain characters.
ingline = string.gsub(ingline, ",", "")
ingline = string.gsub(ingline, ";", "")
ingline = string.gsub(ingline, "%^", "")
ingline = string.gsub(ingline, "%*", "")
ingline = string.gsub(ingline, "%.", "")
ingline = string.gsub(ingline, "%&", "")
ingline = string.gsub(ingline, ":", "")
ingline = string.gsub(ingline, "<", "")
ingline = string.gsub(ingline, ">", "")
ingline = string.gsub(ingline, "~", "")
ingline = string.gsub(ingline, "=", "")
ingline = string.gsub(ingline, "+", "")
ingline = string.gsub(ingline, "%(", "")
ingline = string.gsub(ingline, "%)", "")
ingline = string.gsub(ingline, " %- ", " ")
ingline = string.gsub(ingline, "% or% ", " ")
ingline = string.gsub(ingline, "%-lrb%-", "")
ingline = string.gsub(ingline, "%-LRB%-", "")
ingline = string.gsub(ingline, "%-rrb%-", "")
ingline = string.gsub(ingline, "%-RRB%-", "")
ingline = string.gsub(ingline, " ", " ")
ingline = string.gsub(ingline, " ", " ")
ingline = string.gsub(ingline, " ", " ")
ing_strings = stringx.split(ingline, '\t')
elseif stringx.startswith(line, "END") then
if title ~= nil and #step_table ~= 0 then
counter.nrecipes = counter.nrecipes + 1
for i=1,#ing_strings do
local ing = ing_strings[i]
local ing_tokens = stringx.split(ing)
local new_length = 0
local new_tokens = {}
for j=1,#ing_tokens do
local token = ing_tokens[j]
-- Only use word tokens in the ingredient and remove amount information.
-- 2 character tokens below are MealMaster amount codes
if token:find("[%a]") ~= nil then
if token ~= "x" and token ~= "sm" and token ~= "md" and token ~= "lg" and token ~= "cn" and token ~= "pk"
and token ~= "pn" and token ~= "dr" and token ~= "ds" and token ~= "ct" and token ~= "bn" and token ~= "sl"
and token ~= "ea" and token ~= "t" and token ~= "ts" and token ~= "T" and token ~= "tb" and token ~= "fl"
and token ~= "c" and token ~= "pt" and token ~= "qt" and token ~= "ga" and token ~= "oz" and token ~= "lb"
and token ~= "ml" and token ~= "cb" and token ~= "cl" and token ~= "dl" and token ~= "l" and token ~= "mg"
and token ~= "cg" and token ~= "dg" and token ~= "g" and token ~= "kg" then
counter.ing_counts[token] = (counter.ing_counts[token] or 0) + 1
table.insert(new_tokens, token)
new_length = new_length + 1
end
end
end
if #new_tokens ~= 0 then
table.sort(new_tokens, function(a, b) return a < b end)
local new_ing_string = table.concat(new_tokens, " ")
counter.ing_length_counts[#new_tokens] = (counter.ing_length_counts[#new_tokens] or 0) + 1
counter.lost_map[ing] = new_ing_string
else
counter.ing_length_counts[1] = (counter.ing_length_counts[1] or 0) + 1
counter.lost_map[ing] = "<unk>"
end
end
local max_step_len = 0
for s=1,#step_table do
local step = step_table[s]
-- add 1 for </s> or </text>
local step_tokens = stringx.split(step)
local clean_token_count = 0
for t=1,#step_tokens do
local token = step_tokens[t]
if token ~= '' then
counter.word_counts[token] = (counter.word_counts[token] or 0) + 1
clean_token_count = clean_token_count + 1
end
end
recipe_len = recipe_len + clean_token_count + 1
end
counter.num_steps_counts[#step_table] = (counter.num_steps_counts[#step_table] or 0) + 1
counter.full_recipe_len_counts[recipe_len] = (counter.full_recipe_len_counts[recipe_len] or 0) + 1
counter.title_length_counts[#title_tokens] = (counter.title_length_counts[#title_tokens] or 0) + 1
counter.ing_number_counts[#ing_strings] = (counter.ing_number_counts[#ing_strings] or 0) + 1
nrecipe = nrecipe + 1
end
title = nil
sent = nil
original_title = nil
seen_pred = false
step_table = {}
title_tokens = {}
ing_strings = nil
recipe_len = 1
switch_evidence = false
elseif switch_evidence then
switch_evidence = false
else
sent = line:lower()
switch_evidence = true
table.insert(step_table, stringx.strip(sent))
end
end
end
return counter
end
local function create_dictionary(counter)
local dict = {symbol_to_index = {},
index_to_symbol = {}}
local title_dict = {symbol_to_index = {},
index_to_symbol = {}}
local ing_dict = {symbol_to_index = {},
index_to_symbol = {}}
local word_id = 0
word_id = add_to_dictionary(dict, word_id, '<unk>')
word_id = add_to_dictionary(dict, word_id, '</text>')
word_id = add_to_dictionary(dict, word_id, '<text>')
word_id = add_to_dictionary(dict, word_id, '\n')
for word,count in pairs(counter.word_counts) do
if count >= opt.dictionaryCutoff then
word_id = add_to_dictionary(dict, word_id, word)
end
end
print('dictionary size: ' .. word_id)
counter.word_counts = {}
local title_id = 0
title_id = add_to_dictionary(title_dict, title_id, '<unk>')
for word,count in pairs(counter.title_counts) do
if count >= opt.dictionaryCutoff then
title_id = add_to_dictionary(title_dict, title_id, word)
end
end
print('title dictionary size: ' .. title_id)
counter.title_counts = {}
local ing_id = 0
ing_id = add_to_dictionary(ing_dict, ing_id, '<unk>')
for word,count in pairs(counter.ing_counts) do
if count >= opt.dictionaryCutoff then
ing_id = add_to_dictionary(ing_dict, ing_id, word)
end
end
counter.ing_counts = {}
return dict, ing_dict, title_dict
end
local function add_to_dictionaries(counter, dict, ing_dict, title_dict)
local word_id = 0
for _ in pairs(dict.index_to_symbol) do
word_id = word_id + 1
end
for word,count in pairs(counter.word_counts) do
if count >= opt.dictionaryCutoff then
word_id = add_to_dictionary(dict, word_id, word)
end
end
print('dictionary size: ' .. word_id)
counter.word_counts = {}
local title_id = 0
for _ in pairs(title_dict.index_to_symbol) do
title_id = title_id + 1
end
for word,count in pairs(counter.title_counts) do
if count >= opt.dictionaryCutoff then
title_id = add_to_dictionary(title_dict, title_id, word)
end
end
print('title dictionary size: ' .. title_id)
counter.title_counts = {}
local ing_id = 0
for _ in pairs(ing_dict.index_to_symbol) do
ing_id = ing_id + 1
end
for word,count in pairs(counter.ing_counts) do
if count >= opt.dictionaryCutoff then
ing_id = add_to_dictionary(ing_dict, ing_id, word)
end
end
counter.ing_counts = {}
return dict, ing_dict, title_dict
end
local function build_recipe_matrix(counter, dict, ing_dict, title_dict)
local full_recipe_to_pos = {}
local recipe_mat = {}
local of_recipe_len = {}
for length,count in pairs(counter.full_recipe_len_counts) do
recipe_mat[length] = torch.zeros(count, length, 4):float()
full_recipe_to_pos[length] = torch.zeros(count):long()
of_recipe_len[length] = 1
end
local title_mat = {}
local true_title_mat = {}
local of_title_len = {}
for length,count in pairs(counter.title_length_counts) do
title_mat[length] = torch.zeros(count, length):long()
of_title_len[length] = 1
end
local ing_mat = {}
local of_ing_len = {}
for length,count in pairs(counter.ing_length_counts) do
ing_mat[length] = torch.zeros(count, length):long()
of_ing_len[length] = 1
end
local ings_mat = {}
local true_ings_mat = {}
local of_ing_num = {}
for length,count in pairs(counter.ing_number_counts) do
ings_mat[length] = torch.zeros(count, length, 2):long()
of_ing_num[length] = 1
end
local pos = torch.zeros(counter.nrecipes, 6):long()
local unk_index = dict.symbol_to_index['<unk>']
local ing_unk_index = ing_dict.symbol_to_index['<unk>']
local title_unk_index = title_dict.symbol_to_index['<unk>']
local start_index = dict.symbol_to_index['<text>']
local end_index = dict.symbol_to_index['</text>']
local line_break_index = dict.symbol_to_index['\n']
local nrecipe = 1
local filelist = io.open(opt.listFilename, 'r')
for recipefilename in filelist:lines() do
print(recipefilename)
local recipefile = io.open(opt.inDirectory .. recipefilename, 'r')
local sent = nil
local title_tokens = {}
local ing_strings = nil
local seen_pred = false
local step_table = {}
local recipe_len = 1
local prob_str_table = {}
local title = nil
local switch_prob = false
for line in recipefile:lines() do
if stringx.startswith(line, "Title:") or stringx.startswith(line, "title:") then
title = string.sub(line, 8, -1)
if counter.cantusefull[title] == nil then
local orig_title_tokens = stringx.split(stringx.strip(title:lower()))
for i=1,#orig_title_tokens do
local token = orig_title_tokens[i]
if token:find("[%a]") ~= nil and token:find("'s") == nil and token ~= 'and' and token ~= 'with' then
table.insert(title_tokens, token)
end
end
else
title = nil
end
elseif stringx.startswith(line, "Categories:") or stringx.startswith(line, "categories:") then
elseif stringx.startswith(line, "Servings:") or stringx.startswith(line, "servings:") then
elseif stringx.startswith(line, "Ingredients:") then
local ingline = stringx.strip(string.sub(line, 14, -1)):lower()
ingline = string.gsub(ingline, ",", "")
ingline = string.gsub(ingline, ";", "")
ingline = string.gsub(ingline, "%^", "")
ingline = string.gsub(ingline, "%*", "")
ingline = string.gsub(ingline, "%.", "")
ingline = string.gsub(ingline, "%&", "")
ingline = string.gsub(ingline, ":", "")
ingline = string.gsub(ingline, "<", "")
ingline = string.gsub(ingline, ">", "")
ingline = string.gsub(ingline, "~", "")
ingline = string.gsub(ingline, "=", "")
ingline = string.gsub(ingline, "+", "")
ingline = string.gsub(ingline, "%(", "")
ingline = string.gsub(ingline, "%)", "")
ingline = string.gsub(ingline, " %- ", " ")
ingline = string.gsub(ingline, " or ", " ")
ingline = string.gsub(ingline, "%-lrb%-", "")
ingline = string.gsub(ingline, "%-LRB%-", "")
ingline = string.gsub(ingline, "%-rrb%-", "")
ingline = string.gsub(ingline, "%-RRB%-", "")
ingline = string.gsub(ingline, " ", " ")
ingline = string.gsub(ingline, " ", " ")
ingline = string.gsub(ingline, " ", " ")
ing_strings = stringx.split(ingline, '\t')
elseif stringx.strip(line) == "" then
elseif stringx.startswith(line, "END") then
if title ~= nil and #step_table ~= 0 then
for i=1,#step_table do
local step = step_table[i]
local step_tokens = stringx.split(step)
for p=1,#step_tokens do
local token = step_tokens[p]
if token ~= '' then
recipe_len = recipe_len + 1
end
end
recipe_len = recipe_len + 1
end
local nbin = of_recipe_len[recipe_len]
pos[nrecipe][1] = recipe_len
pos[nrecipe][2] = nbin
of_recipe_len[recipe_len] = nbin + 1
recipe_mat[recipe_len][nbin][1][1] = start_index
recipe_mat[recipe_len][nbin][1][2] = 0.0
recipe_mat[recipe_len][nbin][1][3] = 0.0
recipe_mat[recipe_len][nbin][1][4] = 0.0
full_recipe_to_pos[recipe_len][nbin] = nrecipe
local tbin = of_title_len[#title_tokens]
of_title_len[#title_tokens] = tbin + 1
local cube = false
for i=1,#title_tokens do
local token = title_tokens[i]
if token == 'cube' then
cube = true
end
end
for i=1,#title_tokens do
local token = title_tokens[i]
local tindex = title_dict.symbol_to_index[token] or 0
title_mat[#title_tokens][tbin][i] = tindex
end
local full_title = table.concat(title_tokens, " ")
true_title_mat[nrecipe] = full_title
pos[nrecipe][3] = #title_tokens
pos[nrecipe][4] = tbin
local isbin = of_ing_num[#ing_strings]
of_ing_num[#ing_strings] = isbin + 1
local all_ings = table.concat(ing_strings, '\t')
true_ings_mat[nrecipe] = all_ings
for i=1,#ing_strings do
local ing = ing_strings[i]
local new_ing = counter.lost_map[ing]
-- print(new_ing)
local tokens = stringx.split(new_ing, " ")
local ibin = of_ing_len[#tokens]
of_ing_len[#tokens] = ibin + 1
ings_mat[#ing_strings][isbin][i][1] = #tokens
ings_mat[#ing_strings][isbin][i][2] = ibin
for j,token in ipairs(tokens) do
local tindex = ing_dict.symbol_to_index[token] or ing_unk_index
ing_mat[#tokens][ibin][j] = tindex
end
end
pos[nrecipe][5] = #ing_strings
pos[nrecipe][6] = isbin
local index = 2
for i=1,#step_table do
local step = step_table[i]
step = string.gsub(step, " = ", " ")
step = string.gsub(step, " ~ ", " ")
step = string.gsub(step, " + ", " ")
step = string.gsub(step, "=", "")
step = string.gsub(step, "~", "")
step = string.gsub(step, "+", "")
step = string.gsub(step, " ", " ")
step = string.gsub(step, " ", " ")
step = string.gsub(step, " ", " ")
step = string.gsub(step, " ", " ")
local evid_strs = prob_str_table[i]
local step_tokens = stringx.split(step)
local evidence = stringx.split(evid_strs)
if i ~= 1 then
recipe_mat[recipe_len][nbin][index][1] = line_break_index
recipe_mat[recipe_len][nbin][index][2] = 0.0
recipe_mat[recipe_len][nbin][index][3] = 0.0
recipe_mat[recipe_len][nbin][index][4] = 0.0
index = index + 1
end
for j=1,#step_tokens do
local token = step_tokens[j]
if token ~= '=' and token ~= '~' and token ~= '+' then
token = string.gsub(token, "=", "")
token = string.gsub(token, "+", "")
token = string.gsub(token, "~", "")
if token ~= '' then
local tindex = dict.symbol_to_index[token] or unk_index
local evid_split = stringx.split(evidence[j], '_')
recipe_mat[recipe_len][nbin][index][1] = tindex
if tindex == unk_index then
recipe_mat[recipe_len][nbin][index][2] = 0.0
recipe_mat[recipe_len][nbin][index][3] = 0.0
recipe_mat[recipe_len][nbin][index][4] = 0.0
elseif tonumber(evid_split[2]) == -1 or tonumber(evid_split[3]) == -1 then
-- -1 represents "all ingredients" token. For now, this is ignored.
recipe_mat[recipe_len][nbin][index][2] = 0.0
recipe_mat[recipe_len][nbin][index][3] = 0.0
recipe_mat[recipe_len][nbin][index][4] = 0.0
else
recipe_mat[recipe_len][nbin][index][2] = tonumber(evid_split[1])
recipe_mat[recipe_len][nbin][index][3] = tonumber(evid_split[2])
recipe_mat[recipe_len][nbin][index][4] = tonumber(evid_split[3])
end
index = index + 1
end
end
end
end
recipe_mat[recipe_len][nbin][index][1] = end_index
recipe_mat[recipe_len][nbin][index][2] = 0.0
recipe_mat[recipe_len][nbin][index][3] = 0.0
recipe_mat[recipe_len][nbin][index][4] = 0.0
nrecipe = nrecipe + 1
end
title = nil
sent = nil
title_tokens = {}
ing_strings = nil
seen_pred = false
recipe_len = 1
step_table = {}
prob_str_table = {}
switch_prob = false
elseif switch_prob then
table.insert(prob_str_table, line)
switch_prob = false
else
sent = line:lower()
switch_prob = true
table.insert(step_table, stringx.strip(sent))
end
end
end
print(nrecipe)
return recipe_mat, full_recipe_to_pos, title_mat, true_title_mat, ings_mat, true_ings_mat, ing_mat, pos
end
local function main()
local recipe_counter = {
nrecipes = 0,
num_steps_counts = {},
full_recipe_len_counts = {},
title_length_counts = {},
ing_number_counts = {},
ing_length_counts = {},
word_counts = {},
ing_counts = {},
title_counts = {},
lost_map = {}}
local dict = {}
local ing_dict = {}
local title_dict = {}
if opt.useDictionary ~= '' then
print('Using dictionary ' .. opt.useDictionary)
dict = torch.load(opt.useDictionary .. '.dict.torch');
ing_dict = torch.load(opt.useDictionary .. '.itemdict.torch');
title_dict = torch.load(opt.useDictionary .. '.goaldict.torch');
recipe_counter.lost_map = dict.lost_map
end
print('Counting recipe lengths...')
recipe_counter = count_recipe_lengths(recipe_counter)
if opt.useDictionary == '' then
dict, ing_dict, title_dict = create_dictionary(recipe_counter)
dict.ing_counts = recipe_counter.ing_counts
dict.lost_map = recipe_counter.lost_map
dict.word_counts = recipe_counter.word_counts
elseif opt.dictionaryCutoff == 0 then
dict, ing_dict, title_dict = add_to_dictionaries(recipe_counter, dict, ing_dict, title_dict)
end
print('Building recipe matrices...')
local recipe_mat, recipe_to_pos, title_mat, true_title_mat, ings_mat, true_ings_mat, ing_mat, pos = build_recipe_matrix(recipe_counter, dict, ing_dict, title_dict)
if opt.useDictionary ~= '' then
dict.ing_counts = nil
dict.lost_map = nil
dict.word_counts = nil
end
torch.save(opt.outDirectory .. opt.info .. '.dict.torch', dict)
torch.save(opt.outDirectory .. opt.info .. '.itemdict.torch', ing_dict)
torch.save(opt.outDirectory .. opt.info .. '.goaldict.torch', title_dict)
torch.save(opt.outDirectory .. 'text.' .. opt.info .. '.mat.torch', recipe_mat)
torch.save(opt.outDirectory .. 'text_to_pos.' .. opt.info .. '.mat.torch', recipe_to_pos)
torch.save(opt.outDirectory .. 'goal.' .. opt.info .. '.mat.torch', title_mat)
torch.save(opt.outDirectory .. 'true_goal.' .. opt.info .. '.mat.torch', true_title_mat)
torch.save(opt.outDirectory .. 'items.' .. opt.info .. '.mat.torch', ings_mat)
torch.save(opt.outDirectory .. 'true_items.' .. opt.info .. '.mat.torch', true_ings_mat)
torch.save(opt.outDirectory .. 'item.' .. opt.info .. '.mat.torch', ing_mat)
torch.save(opt.outDirectory .. 'offset.' .. opt.info .. '.mat.torch', pos)
end
main()