English 中文(简体)
搜索相似的分组;包括差异和分数(即类似的食谱)
原标题:Searching for similar groupings; including diff and score (ie. Similar Recipes)

我正试图找到最好的方法来确定一组项目(在本例中,鳄梨酱配方中的成分)与所有项目组(一张表中的配方;链接到另一张配料表)的相似程度。

例如;我有以下鳄梨酱配方:

3 Avocados
1 Vine-Ripened Tomatoes
1 Red Onion
3 Jalapenos
1 Sea Salt
1 Pepper

我想在我所有食谱的表格中运行这个食谱,以确定是否有另一个食谱与它相似(基于配料和计数),并按其相似程度排序。此外,我希望它能识别差异(无论只是配料计数的差异,还是配料的不同)。

可能的输出是:

3 Avocados
(- 1 Vine-Ripened Tomatoes)
1 Red Onion
3 Jalapenos
1 Sea Salt
(- 1 Pepper)
(+ Tobasco)
89.5% Identical

这也可以用来确定以下用例:“给我冰箱里的食材列表;我可以做什么吃?”。

感谢您为我指明正确方向提供的帮助。

问题回答

在我的脑海中,我可以看到通过字符串匹配会出现的一些问题:

  • 3 Avocados and 2 Avocados both use avocado, but the strings are not a match.
  • 1 tbsp salt and 15ml salt refer to the same quantity of salt but the strings are not a match.

您可能需要保存一个配方成分表,其中还存储标准化的数量(即,在放入数据库之前,所有内容都将转换为特定的单位)。我在这里假设您已经有了一个食谱的表和一个配料表,这两个表在这里都用作外键(使其成为联接表

CREATE TABLE recipe_ingredients (
  recipe_id INT NOT NULL,
  ingredient_id INT NOT NULL,
  quantity DECIMAL NOT NULL,
  PRIMARY KEY (recipe_id, ingredient_id),
  FOREIGN KEY recipe_id REFERENCES recipes (id),
  FOREIGN KEY ingredient_id REFERENCES ingredient (id)
)

然后,在确定匹配时,您可以使用确定哪种配方包含您想要的最多成分(这会忽略数量):

SELECT ri.recipe_id, COUNT(ri.ingredient_id) AS num_common_ingredients
FROM ingredients AS i
RIGHT JOIN recipe_ingredients AS ri
  ON ri.ingredient_id = i.id
WHERE i.id IN (?) -- list of ingredient IDs being searched for
GROUP BY ri.recipe_id
ORDER BY COUNT(ri.ingredient_id) DESC

COUNT最高的行具有最大的相似性(因为这意味着有最多的常见成分)。

为了确定数量之间的相似性,一旦您的食谱与大多数成分匹配,您就可以将给定的数量与recipe_ingredients中指定的数量进行比较。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签