def get_ranked_ingreds_from_cooc(ingred): ingreds = get_json('all_ingreds_filtered.json') ingred_to_ix = {k: v for v, k in enumerate(ingreds)} ix_to_ingred = {v: k for v, k in enumerate(ingreds)} cooc = np.array(get_json('cooc.json')) ingred_ix = ingred_to_ix[ingred] ranked_ixs = np.argsort(cooc[ingred_ix]) ranked_ixs = np.flip(ranked_ixs) ranked_ingreds = {} for ranked_ix in ranked_ixs: cooccurrences = cooc[ingred_ix, ranked_ix] if cooccurrences == 0: break ranked_ingreds[ix_to_ingred[ranked_ix]] = cooccurrences return ranked_ingreds
def make_recipe_matrix(): '''2D matrix whose rows are ingredients and cols are recipes titles. A 1 denotes the occurence of an ingredient in a given recipe.''' ingreds = get_json('all_ingreds_filtered.json') recipes = get_json('recipe_data_filtered.json') titles = [] for recipe in recipes: titles.append(recipe['title']) df = pd.DataFrame(0, ingreds, titles) ingreds = set(ingreds) for recipe in recipes: recipe_ingreds = set(recipe['ingreds']) matches = recipe_ingreds & ingreds if len(matches) > 0: df.loc[list(matches), recipe['title']] += 1 return df.to_numpy()
def write_recipe_matrix(outfile='recipe_matrix.json'): '''2D matrix whose rows are ingredients and cols are recipes. A 1 denotes the occurence of an ingredient in a given recipe.''' ingreds = helper.get_json('all_ingreds_filtered.json') recipes = helper.get_json('recipe_data_filtered.json') titles = [] for recipe in recipes: titles.append(recipe['title']) df = pd.DataFrame(0, ingreds, titles) ingreds = set(ingreds) for recipe in recipes: recipe_ingreds = set(recipe['ingreds']) matches = recipe_ingreds & ingreds if len(matches) > 0: df.loc[list(matches), recipe['title']] = 1 data = df.to_numpy() data = data.tolist() helper.write_json(data, outfile, 'w')
def write_all_ingreds(recipe_file_name, ingred_file_name): """Save json of all ingreds in recipe_file_name to ingred_file_name.""" data = helper.get_json(recipe_file_name) ingreds = [] for recipe in data: ingreds.append(recipe['ingreds']) ingreds = [ingred for sublist in ingreds for ingred in sublist] ingreds = list(set(ingreds)) ingreds.sort() helper.write_json(ingreds, ingred_file_name, 'w') return ingreds
def write_recipe_data_filtered(infile, outfile): """Filter recipes from infile and save to outfile as json.""" data = helper.get_json(infile) with open('approved_ingreds', 'r', encoding="utf8") as f: approved_ingreds = set(f.read().splitlines()) ingred_filters = generate_ingred_filters(approved_ingreds) # Remove duplicate recipes df = pd.DataFrame(data) df_unique = df[~df['title'].duplicated()] data = df_unique.to_dict('records') for recipe in data: filtered_ingreds = filter_naive(recipe['ingreds'], ingred_filters) recipe['ingreds'] = filtered_ingreds helper.write_json(data, outfile, 'w')
def write_all_ingreds_lemma(infile='all_ingreds_filtered.json', outfile='static/all_ingreds_lemma.json'): """Save json of lemmatization of ingreds in infile to outfile.""" ingreds = helper.get_json(infile) ingreds = [lemmatize(ingred) for ingred in ingreds] helper.write_json(ingreds, outfile, 'w')
c_property = df['c_property'] rew = df['rew'] rew_clean = df['rew_clean'] rew_latlong = df['latlong'] # Load the model model = get_model() gbr = model['gbr'] f1 = model['f1n'] f2 = model['f2n'] f3 = model['f3n'] f4 = model['f4n'] stackedmodel = model['stackedmodel'] geo_json = get_json() recommender = get_recommender() #variables neighbourhoods = list(np.array(area['Neighbourhood_Name'])) n_values = list(np.array(area['N_Value'])) options = [{ 'label': name, 'value': value } for name, value in zip(neighbourhoods, n_values)] mapbox_access_token = "pk.eyJ1IjoiamFja3AiLCJhIjoidGpzN0lXVSJ9.7YK6eRwUNFwd3ODZff6JvA" year = 2020 GoogleMaps(app, key="AIzaSyAvzm0x2kmJoeqX6VY83lMa8pPRDDjH4sY") cols = list(rew.columns) type_list = cols[cols.index('House'):cols.index('Multifamily') + 1]
import http import random from flask import Flask, jsonify, render_template, request, session import matrix from helper import get_json app = Flask(__name__) app.config.from_object('config') VERSION_STR = '?v=0.61' ALL_INGREDS = [ ingred.lower() for ingred in get_json('./static/all_ingreds_lemma.json') ] # TODO: # Try render_template instead of js stuff? Just use js for fetch? # Or even move that straight to html with method="POST" action="/search"? # - Get seperate JQuery source seperate from datatables bundle. # load JQuery first, then typeahead, then datatables # for faster load time. # - allow multiple inputs # Emojis, anyone? @app.route("/") def index(): init_session_vars() remove_invalid_session_ingreds() cur_ingreds = get_session_var('cur_ingreds')
import matplotlib.pyplot as plt import networkx as nx import numpy as np from helper import get_json if __name__ == "__main__": cooc = get_json('cooc.json') ingreds = get_json('all_ingreds_filtered.json') G = nx.from_numpy_array(np.array(cooc)) nx.draw(G) plt.show()
def main(): ingreds = 'onion' recipe_matrix = np.array(get_json('recipe_matrix.json')) all_ingreds = get_json('all_ingreds_filtered.json') get_ranked_ingreds_naive(ingreds, recipe_matrix, all_ingreds)
import numpy as np from sklearn.metrics.pairwise import cosine_similarity as cs from helper import get_json, timer '''2D matrix whose rows are ingredients and cols are recipes. A 1 denotes the occurence of an ingredient in a given recipe.''' RECIPE_MATRIX = np.array(get_json('recipe_matrix.json')) INGRED_AXIS = 0 RECIPE_AXIS = 1 ALL_INGREDS = get_json('all_ingreds_filtered.json') INGRED_TO_IX = {k: i for i, k in enumerate(ALL_INGREDS)} IX_TO_INGRED = {i: k for i, k in enumerate(ALL_INGREDS)} RECIPE_DATA = get_json('recipe_data_filtered.json') IX_TO_RECIPE = {i: (r['title'], r['url']) for i, r in enumerate(RECIPE_DATA)} def get_recommended(input_ingreds): if isinstance(input_ingreds, str): input_ingreds = [input_ingreds] match_recipe_ixs = get_match_recipe_ixs(input_ingreds) ranked_ingreds = get_ranked_ingreds(input_ingreds, match_recipe_ixs) match_recipes = get_match_recipes(match_recipe_ixs) return ranked_ingreds, match_recipes def get_ranked_ingreds(input_ingreds, match_recipe_ixs):