Use with Flag module

kidrobot - September 3, 2009 - 13:41
Project:Recommender API
Version:6.x-2.0-beta3
Component:Code
Category:feature request
Priority:normal
Assigned:danithaca
Status:active
Description

I'd like to use the Recommender API with the Flag module to generate node output similar to the Similar Groups module. ie. Users who flagged this node also flagged these nodes. The flag_content table in the database has columns for uid ($field_mouse) and content_id ($field_cheese) and I can generate the number of users who flagged each node via Views. So in this case what should $field_weight be?

#1

danithaca - September 3, 2009 - 16:35
Assigned to:Anonymous» danithaca

I'll take a look soon.

#2

danithaca - September 21, 2009 - 19:54
Status:active» fixed

This is a good question. There are 2 ways you can do this:

1) Use any table field that has the constant 1. For example, you can use {node}->status. Basically, the $field_weight variable tells RecAPI that how important a $mouse_id<->$cheese_id pair would be. In your case, suppose a user can make 3 flags on a node, then the weight would be 3. But since a user can only flag once on one node, the weight should always be the constant 1.

2) Alternatively, you can use a SQL query in the $field_table variable for maximum flexibility. For an example, you can refer to http://drupal.org/project/uc_rec. A code snippet is copied here.

<?php
  $sql
= "SELECT o.uid, p.nid, COUNT(*) as weight FROM {uc_order_products} p INNER JOIN {uc_orders} o
          ON p.order_id=o.order_id GROUP BY o.uid, p.nid"
;
 
recommender_item2item('uc_rec', $sql, 'uid', 'nid', 'weight', array(
   
'missing' => 'zero', 'performance' => 'memory', 'duplicate' => 'remove', 'lowerbound' => 0, 'knn' => 10));
?>

Remember to use the param 'missing'=>'zero'. That tells RecAPI to treat all the missing uid<->content_id pairs as 0 flags.

If you submit you code here, I can take a look too.

Please feel free to ask other questions. As of now, I'll change the issue status to 'fixed'.

#3

kidrobot - September 22, 2009 - 12:34

I managed to get a block of results by aping the Similar Groups module and calling recommender_similarity_classical. The following returns results:

<?php

function similarobjects_run_recommender() {

       
$sql = "SELECT f.content_id, f.uid, COUNT(*) AS weight
        FROM {node} n
        INNER JOIN {flag_content} f ON f.content_id = n.nid
        GROUP BY f.content_id, f.uid"
;
       
     
recommender_similarity_classical('similarobjects', $sql, 'content_id', 'uid', 'weight', array(
     
'missing' => 'zero', 'performance' => 'auto'));
}


function
similarobjects_get_objects($nid) {
 
$items = recommender_top_similarity('similarobjects', $nid, 6, 'similarobjects_check_correlation');
  return
$items;
}
?>

But...the recommender_item2items used in the UC module is closer to what I am looking for but calling recommender_item2items with the same SQL returns no results.

Any idea why that would be?

#4

kidrobot - September 22, 2009 - 15:47
Status:fixed» active

#5

danithaca - September 23, 2009 - 15:01

recommender_item2item is very similar to recommender_classical or recommender_user2user. Only difference is that item2item tries find similarities between items (or "cheese" in my terminology), where as user2user finds similarity between users (or "mouse" in my terminology) and then give recommendations.
So, you probably need to switch $field_mouse and $field_cheese in item2item. Code might look like

<?php
 
//recommender_similarity_classical('similarobjects', $sql, 'content_id', 'uid', 'weight', array(
     
'missing' => 'zero', 'performance' => 'auto'));
 
// note the switch of order of content_id and uid
 
recommender_similarity_item2item('similarobjects', $sql, 'uid', 'content_id', 'weight', array(
     
'missing' => 'zero', 'performance' => 'auto'));
?>

 
 

Drupal is a registered trademark of Dries Buytaert.