Skip to content

Commit

Permalink
For in-stock items, allow specification of source and destination ite…
Browse files Browse the repository at this point in the history
…ms separately.
  • Loading branch information
redcat9 committed Mar 17, 2014
1 parent bc73b71 commit c642d6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
8 changes: 5 additions & 3 deletions macros/recommender_alternatives.pig
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ define recsys__GetItemItemRecommendations_DiversifyItemItem(user_item_signals, m
*
* Input:
* user_item_signals: { (user:chararray, item:chararray, weight:float) }
* available_items: { (item:chararray) }
* source_items: { (item:chararray) }
* dest_items: { (item:chararray) }
* Output:
* item_item_recs: { (item_A:chararray, item_B:chararray, weight:float, raw_weight:float, rank:int) }
*/
define recsys__GetItemItemRecommendations_WithAvailableItems(user_item_signals, available_items) returns item_item_recs {
define recsys__GetItemItemRecommendations_WithAvailableItems(user_item_signals, source_items, dest_items) returns item_item_recs {

-- Convert user_item_signals to an item_item_graph
ii_links_raw, item_weights = recsys__BuildItemItemGraph(
Expand All @@ -153,7 +154,8 @@ define recsys__GetItemItemRecommendations_WithAvailableItems(user_item_signals,
-- calls different macro from standard recsys code
$item_item_recs = recsys__BuildItemItemRecommendationsFromGraph_withAvailableItems(
ii_links,
$available_items, -- Added on to represent items in stock or not
$source_items, -- Items that should have recommendations generated for them
$dest_items, --Items that can be recommended
$NUM_RECS_PER_ITEM,
$NUM_RECS_PER_ITEM
);
Expand Down
26 changes: 15 additions & 11 deletions macros/recsys_alternatives.pig
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,26 @@ returns item_recs {
/*
* This is an alternative of recsys__BuildItemItemRecommendationsFromGraph.
*
* This takes an additional input of a set of available items to handle the case where not every
* item is in stock or needs a recommendation; but the links to those items may still be valuable
* in the shortest paths traversal.
* This takes an additional input of a set of available source items and available destination
* items, to handle the case where not every item is in stock or needs a recommendation; but the
* links to those items may still be valuable in the shortest paths traversal and when linking
* back to users.
*
* Input:
* Same inputs as recsys__BuildItemItemRecommendationsFromGraph.
* available_items: { (item:chararray) }
* source_items: { (item:chararray) }
* dest_items: { (item:chararray) }
*
* Output:
* Same output as recsys__BuildItemItemRecommendationsFromGraph.
*/
define recsys__BuildItemItemRecommendationsFromGraph_withAvailableItems(
ii_links, available_items, initial_nhood_size, num_recs)
ii_links, source_items, dest_items, initial_nhood_size, num_recs)
returns item_recs {

graph, paths = recsys__InitShortestPaths_FromAvailableItems($ii_links,
$available_items,
$source_items,
$dest_items,
$initial_nhood_size);

two_step_terms = foreach (join graph by item_B, paths by item_A) generate
Expand Down Expand Up @@ -138,14 +141,15 @@ returns item_recs {
*
* Input:
* ii_links: { (item_A:chararray, item_B:chararray, weight:float, raw_weight:float) }
* available_items: { (item:chararray) }
* source_items: { (item:chararray) }
* dest_items: { (item:chararray) }
* num_recs: int
*
* Output:
* graph: { (item_A:chararray, item_B:chararray, dist:float, raw_weight:float) }
* paths: { (item_A:chararray, item_B:chararray, dist:float, raw_weight:float) }
*/
define recsys__InitShortestPaths_FromAvailableItems(ii_links, available_items, num_recs)
define recsys__InitShortestPaths_FromAvailableItems(ii_links, source_items, dest_items, num_recs)
returns graph, paths {

distance_mat = foreach $ii_links generate
Expand All @@ -158,7 +162,7 @@ returns graph, paths {
as (item_A, item_B, dist, raw_weight);
}

$graph = foreach (join $available_items by item, graph_tmp by item_A) generate
$graph = foreach (join $source_items by item, graph_tmp by item_A) generate
item_A as item_A, item_B as item_B,
dist as dist, raw_weight as raw_weight;

Expand All @@ -168,11 +172,11 @@ returns graph, paths {
self_loops = foreach dest_verts generate
id as item_A, id as item_B, 0.0f as dist, null as raw_weight;
raw_paths = union graph_copy, self_loops;
$paths = foreach (join raw_paths by item_B, $available_items by item) generate
$paths = foreach (join raw_paths by item_B, $dest_items by item) generate
raw_paths::item_A as item_A,
raw_paths::dist as dist,
raw_paths::raw_weight as raw_weight,
available_items::item as item_B;
raw_paths::item_B as item_B;
};


Expand Down
2 changes: 1 addition & 1 deletion pigscripts/techniques/in-stock-technique.pig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ available_items = foreach (filter inventory_input by stock > 0) generate
/******* Use Mortar recommendation engine to convert signals to recommendations **********/

-- Use of non standard Mortar Recommendation engine macro
item_item_recs = recsys__GetItemItemRecommendations_WithAvailableItems(user_signals, available_items);
item_item_recs = recsys__GetItemItemRecommendations_WithAvailableItems(user_signals, available_items, available_items);
user_item_recs = recsys__GetUserItemRecommendations(user_signals, item_item_recs);


Expand Down

0 comments on commit c642d6b

Please sign in to comment.