Exemplo n.º 1
0
    def search_ingredient(self, ingredient_content):
        ing_words = (filter(lambda c: c.isalnum() or c == ' ',
                            ingredient_content)).split()
        ing_results = {}
        for word in ing_words:
            word_result_ids = recipesearch.query_string(word.lower())
            for ing_id in word_result_ids:
                if not ing_id in ing_results:
                    ing_results[ing_id] = 1
                else:
                    ing_results[ing_id] += 1

        sorted_ing_ids = (sorted([k for k in ing_results],
                                 key=lambda x: ing_results[x]))
        sorted_ing_ids.reverse()
        threshold = len(ing_words)
        result_ingredients = []
        while (threshold > 0):
            threshold_ids = [
                k for k in ing_results if ing_results[k] == threshold
            ]
            sql_str = "(" + ", ".join([str(x) for x in threshold_ids
                                       ]) + ")" if threshold_ids else "(0)"
            query = "SELECT ingredients.id, ingredients.content FROM ingredients WHERE id IN %s;" % sql_str
            self.cur.execute(query)
            result_ingredients.extend(self.cur.fetchall())
            threshold -= 1
        return result_ingredients
Exemplo n.º 2
0
    def get_recommended_recipes(self, user_id):
        item_projections = {}
        self.cur.execute(
            "SELECT items.id, items.name FROM user_item_mapping, items WHERE user_item_mapping.item_id = items.id AND user_item_mapping.user_id = %s;",
            (user_id, ))
        for record in self.cur:
            ing_words = (filter(lambda c: c.isalnum() or c == ' ',
                                record.name)).split()
            ing_results = {}
            for word in ing_words:
                word_result_ids = recipesearch.query_string(word.lower())
                for ing_id in word_result_ids:
                    if not ing_id in ing_results:
                        ing_results[ing_id] = 1
                    else:
                        ing_results[ing_id] += 1
            word_threshold = len(ing_words)
            sufficient_ing_ids = [
                k for k in ing_results if ing_results[k] == word_threshold
            ]
            item_projections[record.id] = set(sufficient_ing_ids)

        all_projections = set()
        for k in item_projections:
            all_projections = all_projections.union(item_projections[k])

        recipe_sets = {}
        self.cur.execute(
            "SELECT ingredients.id, ingredients.recipe_id FROM ingredients;")
        for record in self.cur:
            if not record.recipe_id in recipe_sets:
                recipe_sets[record.recipe_id] = set()
            recipe_sets[record.recipe_id].add(record.id)

        recipe_measures = {}
        for k in recipe_sets:
            recipe_measures[k] = [
                len(recipe_sets[k]),
                len(recipe_sets[k].intersection(all_projections))
            ]

        threshold = 0
        min_matches = 1
        result_recipes = []
        while (threshold > -10):
            threshold_ids = [
                k for k in recipe_measures
                if recipe_measures[k][1] >= min_matches and (
                    recipe_measures[k][1] - recipe_measures[k][0]) == threshold
            ]
            sql_str = "(" + ", ".join([str(x) for x in threshold_ids
                                       ]) + ")" if threshold_ids else "(0)"
            query = "SELECT recipes.id, recipes.name FROM recipes WHERE id IN %s;" % sql_str
            self.cur.execute(query)
            result_recipes.extend(self.cur.fetchall())
            threshold -= 1
        return result_recipes
Exemplo n.º 3
0
    def get_recommended_recipes(self, user_id):
        item_projections = {}
        self.cur.execute("SELECT items.id, items.name FROM user_item_mapping, items WHERE user_item_mapping.item_id = items.id AND user_item_mapping.user_id = %s;", (user_id,))
        for record in self.cur:
            ing_words = (filter(lambda c: c.isalnum() or c == ' ', record.name)).split()
            ing_results = {}
            for word in ing_words:
                word_result_ids = recipesearch.query_string(word.lower())
                for ing_id in word_result_ids:
                    if not ing_id in ing_results:
                        ing_results[ing_id] = 1
                    else:
                        ing_results[ing_id] += 1
            word_threshold = len(ing_words)
            sufficient_ing_ids = [k for k in ing_results if ing_results[k] == word_threshold]
            item_projections[record.id] = set(sufficient_ing_ids)

        all_projections = set()
        for k in item_projections:
            all_projections = all_projections.union(item_projections[k])

        recipe_sets = {}
        self.cur.execute("SELECT ingredients.id, ingredients.recipe_id FROM ingredients;")
        for record in self.cur:
            if not record.recipe_id in recipe_sets:
                recipe_sets[record.recipe_id] = set()
            recipe_sets[record.recipe_id].add(record.id)

        recipe_measures = {}
        for k in recipe_sets:
            recipe_measures[k] = [len(recipe_sets[k]), len(recipe_sets[k].intersection(all_projections))]

        threshold = 0
        min_matches = 1
        result_recipes = []
        while(threshold > -10):
            threshold_ids = [k for k in recipe_measures if recipe_measures[k][1] >= min_matches and (recipe_measures[k][1] - recipe_measures[k][0]) == threshold]
            sql_str = "(" + ", ".join([str(x) for x in threshold_ids]) + ")" if threshold_ids else "(0)"
            query = "SELECT recipes.id, recipes.name FROM recipes WHERE id IN %s;" % sql_str
            self.cur.execute(query)
            result_recipes.extend(self.cur.fetchall())
            threshold -= 1
        return result_recipes
Exemplo n.º 4
0
    def search_ingredient(self, ingredient_content):
        ing_words = (filter(lambda c: c.isalnum() or c == ' ', ingredient_content)).split()
        ing_results = {}
        for word in ing_words:
            word_result_ids = recipesearch.query_string(word.lower())
            for ing_id in word_result_ids:
                if not ing_id in ing_results:
                    ing_results[ing_id] = 1
                else:
                    ing_results[ing_id] += 1

        sorted_ing_ids = (sorted([k for k in ing_results], key=lambda x: ing_results[x]))
        sorted_ing_ids.reverse()
        threshold = len(ing_words)
        result_ingredients = []
        while(threshold > 0):
            threshold_ids = [k for k in ing_results if ing_results[k] == threshold]
            sql_str = "(" + ", ".join([str(x) for x in threshold_ids]) + ")" if threshold_ids else "(0)"
            query = "SELECT ingredients.id, ingredients.content FROM ingredients WHERE id IN %s;" % sql_str
            self.cur.execute(query)
            result_ingredients.extend(self.cur.fetchall())
            threshold -= 1
        return result_ingredients
Exemplo n.º 5
0
import recipesearch

print recipesearch.query_string("baked")
print recipesearch.query_string("an")
Exemplo n.º 6
0
import recipesearch

print recipesearch.query_string("baked")
print recipesearch.query_string("cheese")
print recipesearch.query_string("fish")