PHP 的加权搜索算法

发布于 2024-12-05 14:44:16 字数 584 浏览 3 评论 0原文

问题

我的表中有 5 个项目[1],每个项目有 4 个属性(红、绿、蓝、黄)。
每个属性的分数可以在 1 到 9 之间[2]

在我的网站上执行搜索时,用户可以通过为每个属性提供 1 到 9 之间的分数来指定每个属性与搜索结果的相关程度。

我应该使用什么算法来根据用户偏好计算和排序结果?

资源

[1] - CREATE TABLE items( id INT NOT NULL AUTO_INCRMENT , name VARCHAR(128) , red INT , green INT , blue INT , Yellow INT , PRIMARY KEY (id) );

[2] - 插入项目(名称、红色、绿色、蓝色、黄色)值('随机 1', 4, 1, 9, 4), ('随机 2', 1, 1, 2, 9), ('随机 3', 5, 7, 6, 3), ('随机 4' , 2, 2, 8, 1);

Problem

I have 5 items in a table [1], each item has 4 attributes (red, green, blue, yellow).
Each attribute can be given a score between 1 and 9 [2].

When performing a search on my website users can specify how relevant each attribute is to the search results by giving each attribute a score between 1 and 9.

What algorithm should I use to calculate and order the results based on the users preference?

Resources

[1] - CREATE TABLE items( id INT NOT NULL AUTO_INCREMENT , name VARCHAR(128) , red INT , green INT , blue INT , yellow INT , PRIMARY KEY (id) );

[2] - INSERT INTO items (NAME, red, green, blue, yellow) VALUES ('Random 1', 4, 1, 9, 4), ('Random 2', 1, 1, 2, 9), ('Random 3', 5, 7, 6, 3), ('Random 4', 2, 2, 8, 1);

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

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

发布评论

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

评论(4

爱已欠费 2024-12-12 14:44:16

将搜索标点符号计算为字段并按其对查询进行排序

SELECT *, (red * @rw) AS w1, (green * @gw) AS w2, (blue* @bw) AS w3, (yellow * @yw) AS w4, (w1 + w2 + w3 + w4) AS result 
  FROM items ORDER BY result DESC; 

@rw =>;红色重量,@bw =>蓝色重量等...

Calculate the search punctuation as a field and sort the query by it

SELECT *, (red * @rw) AS w1, (green * @gw) AS w2, (blue* @bw) AS w3, (yellow * @yw) AS w4, (w1 + w2 + w3 + w4) AS result 
  FROM items ORDER BY result DESC; 

@rw => red weight, @bw => blue weight, etc...

我三岁 2024-12-12 14:44:16

把它加起来

<?php
require('connect.php') //your db connection data
$redWeight=$_REQUEST['howeverYouPassedTheRedWeighting'];
$blueWeight=$_REQUEST['howeverYouPassedTheBlueWeighting'];
$yellowWeight=$_REQUEST['howeverYouPassedTheYelloWeighting'];
$greenWeight=$_REQUEST['howeverYouPassedTheGreenWeighting'];
$query="SELECT name, ( red*$redWeight+green*$greenWeight+blue*$blueWeight+yellow*$yellowWeight ) AS value FROM items ORDER BY value DESC";
$res=mysql_query($query);
//etc.
?>

just total it up

<?php
require('connect.php') //your db connection data
$redWeight=$_REQUEST['howeverYouPassedTheRedWeighting'];
$blueWeight=$_REQUEST['howeverYouPassedTheBlueWeighting'];
$yellowWeight=$_REQUEST['howeverYouPassedTheYelloWeighting'];
$greenWeight=$_REQUEST['howeverYouPassedTheGreenWeighting'];
$query="SELECT name, ( red*$redWeight+green*$greenWeight+blue*$blueWeight+yellow*$yellowWeight ) AS value FROM items ORDER BY value DESC";
$res=mysql_query($query);
//etc.
?>
幸福%小乖 2024-12-12 14:44:16

抱歉,我没有直接的答案。这是一个非常有趣的话题。您可以使用与欧几里德距离或皮尔逊相关相关的东西。您可以在与集体智慧相关的书籍中找到更多信息。

当然,实施这样的事情会更困难,但我们会更加准确和精确。我推荐这些书:

智能网络算法

集体智慧编程:构建智能网络2.0 应用

Sorry but i've not a direct answer. This is a very interesting topic. You can use something related to the euclidean distance, or Pearson correlation. You can find more in books related to Collective Intelligence.

Of course it's more difficult to implement things like these, but your results we'll be much more accurate and precise. I recommend these books:

Algorithms of the Intelligent Web

Programming Collective Intelligence: Building Smart Web 2.0 Applications

り繁华旳梦境 2024-12-12 14:44:16

我不确定我是否理解你的意思,但是这样怎么样:

$user_defined_relevance = array ('red' => 1, 'blue' => 3, 'green' => 2, 'yellow' => 7);
arsort($user_defined_relevance); //this sorts the array by value descending while maintaining index association

$query = 'SELECT * FROM items ORDER BY '.implode(array_keys($user_defined_relevance), ',  DESC');
//will make SELECT * FROM items ORDER BY yellow DESC, blue DESC, green DESC, red DESC;
mysql_query($query);
 ...

I'm not sure if I get what you mean right, but how about this:

$user_defined_relevance = array ('red' => 1, 'blue' => 3, 'green' => 2, 'yellow' => 7);
arsort($user_defined_relevance); //this sorts the array by value descending while maintaining index association

$query = 'SELECT * FROM items ORDER BY '.implode(array_keys($user_defined_relevance), ',  DESC');
//will make SELECT * FROM items ORDER BY yellow DESC, blue DESC, green DESC, red DESC;
mysql_query($query);
 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文