开发者

PHP Algorithm Help

开发者 https://www.devze.com 2023-03-10 12:17 出处:网络
I\'m working on a PHP algorithm to match compatibility based on responses to questions in a form. In this situation, user A and user B are asked the same exact questions.Let\'s say these are a few of

I'm working on a PHP algorithm to match compatibility based on responses to questions in a form.

In this situation, user A and user B are asked the same exact questions. Let's say these are a few of the questions:

Cleanliness
    No preference
    Somewhat Clean
    Tidy but cluttered
    Strictly organized


Person B Cleanliness Preference
    Whatever
    Can't be too different
    Must Match

Room Temperature
    Cold
    Comfortable
    Hot

Now there would be more questions, but it continues on this similar trend. There are questions about you specifically, and then there are questions that pertain to Person B (and how you want them开发者_开发技巧 to respond)

I want the algorithm to match people based on their similarity in responses, and obviously the conditional statements for person B will add more weight to their answers to those questions.

I thought maybe of assigning a point value to each answer, but I'm still not sure how I'd make that work.

Does anyone have any suggestions?


Line up all your "fields" you are asking your users in the questionnaire. Either make your ranges very limited (always, sometimes, never) and assign them constant values like 1, 2, 3. Loop through everyone to match responses together. Generate a list of potential compatible matches - which you can then sort as your application demands.

Sample

define('FEMALE', 0);
define('MALE', 1);

$user1 = array(
 'name' => 'John Doe',
 'gender' => MALE,
 'interseted_in' => FEMALE,
 'cleanliness' => 3,
// etc
);

$potentials = array();
foreach($users as $u)
{
  if($user['gender'] != $user1['interested_in'])
  {
    continue;
  }

  // More checking
  // ...
  // At the end of checking, add to the potentials.
  $potentials[] = $u;
}


I would look into how okcupid does matching: http://www.okcupid.com/faaaq

  • http://www.quora.com/How-would-you-technically-implement-a-matching-algorithm-like-OkCupid
  • http://www.barsoom.org/okcupid


A point value system COULD work. Here is my thinking:

For each value they answer a question for, that answer gets an ID in the database. When you are trying to "match" or "compare" two people, you add up how many answers they have in common by comparing the answer IDs. Then its up to you to determine if they have enough in common.. For example, if they have 8/10 answers in common, they could be a match.

mysql> SELECT * FROM users LIMIT 1;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | Dalton Conley |
+----+---------------+

mysql> SELECT * FROM questions;
+----+--------------------------+
| id | value                    |
+----+--------------------------+
|  1 | Do you like to program?  |
+----+--------------------------+

mysql> SELECT * FROM answers;
+----+-------------+-------+
| id | question_id | value |
+----+-------------+-------+
|  1 |           1 | yes   |
|  2 |           1 | no    |
+----+-------------+-------+

mysql> SELECT * FROM user_answers;
+---------+-------------+-----------+
| user_id | question_id | answer_id |
+---------+-------------+-----------+
|       1 |           1 |         2 |
+---------+-------------+-----------+
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号