使用 ruby​​ 根据其包含的成分查找类似的食谱

发布于 2024-08-27 23:59:41 字数 64 浏览 7 评论 0原文

我有一系列食谱,每个食谱都有多种成分。该信息存储在连接表中。给一个食谱,我想根据成分找到类似的食谱。我该怎么做呢?

I have a collection of recipes, each having a number of ingredients. This information is stored in a join table. Give a recipe, I'd like to find recipes similar to it based on ingredients. How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-09-03 23:59:41

假设一个食谱有 3 种常见成分,则被认为是相似的。

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients

  # with three similar ingredients
  def similar(n=3)
    Recipe.find(
      RecipeIngredient.count(
        :joins      => "join recipe_ingredients B ON B.recipe_id = #{self.id}",
        :conditions => "recipe_ingredients.recipe_id != B.recipe_id AND
                        recipe_ingredients.ingredient_id = B.ingredient_id",
        :group      => "recipe_ingredients.recipe_id",
        :having     => "count(*) >= #{n}"
      ).keys
    )
  end
end

class RecipeIngredient  < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
end

class Ingredient < ActiveRecord::Base
  has_many :recipe_ingredients
end

给定一个食谱,您可以获得类似的食谱,如下所示:

recipe.similar    # 3 similar ingredients
recipe.similar(4) # 4 similar ingredients

Let's assume a recipe is considered similar when it has 3 common ingredients.

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients

  # with three similar ingredients
  def similar(n=3)
    Recipe.find(
      RecipeIngredient.count(
        :joins      => "join recipe_ingredients B ON B.recipe_id = #{self.id}",
        :conditions => "recipe_ingredients.recipe_id != B.recipe_id AND
                        recipe_ingredients.ingredient_id = B.ingredient_id",
        :group      => "recipe_ingredients.recipe_id",
        :having     => "count(*) >= #{n}"
      ).keys
    )
  end
end

class RecipeIngredient  < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
end

class Ingredient < ActiveRecord::Base
  has_many :recipe_ingredients
end

Given a recipe you can get similar recipes as follows:

recipe.similar    # 3 similar ingredients
recipe.similar(4) # 4 similar ingredients
小猫一只 2024-09-03 23:59:41
recipe = Reciepe.first
ingredients = recipe.ingredients
# Find out reciepes with at least one ingredient similar
reciepes = ingredients.each{|in| in.reciepes}
# find out reciepes with at least {count %} ingredients similar
count = 0.5 # 50%
number = (count*ingredients.size).to_i
more_recipies = recipies.select{|r| (r.ingridients&ingredients).size >= number)}

未测试

recipe = Reciepe.first
ingredients = recipe.ingredients
# Find out reciepes with at least one ingredient similar
reciepes = ingredients.each{|in| in.reciepes}
# find out reciepes with at least {count %} ingredients similar
count = 0.5 # 50%
number = (count*ingredients.size).to_i
more_recipies = recipies.select{|r| (r.ingridients&ingredients).size >= number)}

not tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文