def read_coocs(): """ Read the file "Cococcs of ingredients" """ raw = dict(hp.dumpRead("Cococcs of ingredients")) coocs = dict() for key in raw: coocs[(key[0][2:-1].lower(), key[1][2:-1].lower())] = raw[key] return dict(sorted(coocs.items(), key=lambda item: item[1], reverse=True))
def generate_ingredients_h(il=[], init=random.choice(hp.dumpRead("model/inglist_final"))): """ Helper function for generate_ingredients() """ coocs = read_coocs() selected = il for key in coocs: if len(il) < 20 and key[0] == init and key[0] not in selected: selected.append(key[1]) return selected
def generate_ingredients(): """ Generate a list of related ingredients It is possible to comment out this code and add a list of ingredients manually. For example: selected = ["pasta", "tomato", "garlic", "oil", "onions", "salt", "pepper"] return selected This way you can use custom ingredients """ first = "" selected = [] # Choose a random, valid first ingredient while first == "" or len(first) > 30: first = random.choice(hp.dumpRead("model/inglist_final")) # Get a list of 20 ingredients that are related to the previous ones # Not all 20 are used, this is just to make sure that the model does not run out of ingredients. # 20 is deduced by: # [maximum length of a sentence] x [maximum percentage of ingredients in a sentence] # 50 x 0.40 = 20 while len(selected) < 20: selected += generate_ingredients_h(init=random.choice(hp.dumpRead("model/inglist_final"))) return selected
def init_vars(): """ Prepare the data for training """ data = hp.dumpRead(DATANAME) words = sorted(list(set(data))) ewords = dict((c, i) for i, c in enumerate(words)) enumbs = dict((i, c) for i, c in enumerate(words)) seq_length = 100 dataX = [] dataY = [] for i in range(0, len(data) - seq_length, 1): seq_in = data[i:i + seq_length] seq_out = data[i + seq_length] dataX.append([ewords[char] for char in seq_in]) dataY.append(ewords[seq_out]) n_patterns = len(dataX) X = np.reshape(dataX, (n_patterns, seq_length, 1)) X = X / float(len(words)) y = np_utils.to_categorical(dataY) return (X, y, dataX, dataY, words, ewords, enumbs)
# -*- coding: utf-8 -*- import helpers as hp a = hp.dumpRead()