Skip to content

Commit

Permalink
Simplified recommender wrapping methods (created an AbstractRecommend…
Browse files Browse the repository at this point in the history
…er). Related with issue #6.
  • Loading branch information
abellogin authored and recommend2 committed Sep 17, 2013
1 parent b5121c1 commit 942e310
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 157 deletions.
43 changes: 43 additions & 0 deletions src/main/java/net/recommenders/plista/rec/AbstractRecommender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandr
*/
public abstract class AbstractRecommender implements Recommender {

private Recommender wrapped;

public AbstractRecommender(Recommender wrapped) {
this.wrapped = wrapped;
}

public List<Long> recommend(Message input, Integer limit) {
return wrapped.recommend(input, limit);
}

public void init() {
wrapped.init();
}

public void impression(Message _impression) {
wrapped.impression(_impression);
}

public void click(Message _feedback) {
wrapped.click(_feedback);
}

public void update(Message _update) {
wrapped.update(_update);
}

public void setProperties(Properties properties) {
wrapped.setProperties(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @author alejandr
*/
public interface ConstrainedRecommender extends Recommender{
public interface ConstrainedRecommender extends Recommender {

public List<Long> recommend(final Message _input, Set<Long> constraints);
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandr
*/
public class ContestLuceneRecommenderWithUserFilter implements Recommender {

private Recommender wrapped;
public class ContestLuceneRecommenderWithUserFilter extends AbstractRecommender implements Recommender {

public ContestLuceneRecommenderWithUserFilter() {
this.wrapped = new UserFilterWrapper(new WrappableRecommender(new LuceneRecommender()));
}

public List<Long> recommend(Message input, Integer limit) {
return wrapped.recommend(input, limit);
}

public void init() {
wrapped.init();
}

public void impression(Message _impression) {
wrapped.impression(_impression);
}

public void click(Message _feedback) {
wrapped.click(_feedback);
}

public void update(Message _update) {
wrapped.update(_update);
}

public void setProperties(Properties properties) {
wrapped.setProperties(properties);
super(new UserFilterWrapper(new WrappedRecommender(new LuceneRecommender())));
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandr
*/
public class PRCategoryRecommenderWithUserFilter implements Recommender {

private Recommender wrapped;
public class PRCategoryRecommenderWithUserFilter extends AbstractRecommender implements Recommender {

public PRCategoryRecommenderWithUserFilter() {
this.wrapped = new UserFilterWrapper(new WrappableRecommender(new PRCategoryBasedRecommender()));
}

public List<Long> recommend(Message input, Integer limit) {
return wrapped.recommend(input, limit);
}

public void init() {
wrapped.init();
}

public void impression(Message _impression) {
wrapped.impression(_impression);
}

public void click(Message _feedback) {
wrapped.click(_feedback);
}

public void update(Message _update) {
wrapped.update(_update);
}

public void setProperties(Properties properties) {
wrapped.setProperties(properties);
super(new UserFilterWrapper(new WrappedRecommender(new PRCategoryBasedRecommender())));
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandr
*/
public class PROtherCategoryRecommenderWithUserFilter implements Recommender {

private Recommender wrapped;
public class PROtherCategoryRecommenderWithUserFilter extends AbstractRecommender implements Recommender {

public PROtherCategoryRecommenderWithUserFilter() {
this.wrapped = new UserFilterWrapper(new WrappableRecommender(new PROtherCategoryBasedRecommender()));
}

public List<Long> recommend(Message input, Integer limit) {
return wrapped.recommend(input, limit);
}

public void init() {
wrapped.init();
}

public void impression(Message _impression) {
wrapped.impression(_impression);
}

public void click(Message _feedback) {
wrapped.click(_feedback);
}

public void update(Message _update) {
wrapped.update(_update);
}

public void setProperties(Properties properties) {
wrapped.setProperties(properties);
super(new UserFilterWrapper(new WrappedRecommender(new PROtherCategoryBasedRecommender())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class UserFilterMahoutWrapper implements Recommender {
private static final Logger logger = Logger.getLogger(UserFilterMahoutWrapper.class);
private static final long UPDATE_TIME = 10L * 60 * 1000; // 10 minutes
private static final int NUM_THREADS = 5;
private WrappableRecommenderIF rec;
private WrappableRecommender rec;
// number of days taken into account for the data model
private int numberOfDays;
private Map<Long, DataModel> domainModels;
private long lastUpdate;
private ExecutorService pool;

public UserFilterMahoutWrapper(WrappableRecommenderIF rec) {
public UserFilterMahoutWrapper(WrappableRecommender rec) {
this.rec = rec;

this.pool = Executors.newFixedThreadPool(NUM_THREADS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
public class UserFilterWrapper implements Recommender {

private static final Logger logger = Logger.getLogger(UserFilterWrapper.class);
private WrappableRecommenderIF rec;
private WrappableRecommender rec;
private Map<Long, InteractionModel> domainModels;

public UserFilterWrapper(WrappableRecommenderIF rec) {
public UserFilterWrapper(WrappableRecommender rec) {
this.rec = rec;

this.domainModels = new ConcurrentHashMap<Long, InteractionModel>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,16 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandro
* Interface to allow recommenders to be wrapped by other classes, like filters
*
* @author alejandr
*/
public class WrappableRecommender implements Recommender, WrappableRecommenderIF {

private Recommender wrapped;
private int factor;

public WrappableRecommender(Recommender wrapped) {
this(wrapped, 10);
}

public WrappableRecommender(Recommender wrapped, int factor) {
this.wrapped = wrapped;
this.factor = factor;
}

@Override
public List<Long> recommendAll(Message message, Integer limit) {
return wrapped.recommend(message, (factor * limit));
}

public List<Long> recommend(Message message, Integer limit) {
return wrapped.recommend(message, limit);
}

public void init() {
wrapped.init();
}

public void impression(Message _impression) {
wrapped.impression(_impression);
}

public void click(Message _feedback) {
wrapped.click(_feedback);
}

public void update(Message _update) {
wrapped.update(_update);
}
public interface WrappableRecommender extends Recommender {

public void setProperties(Properties properties) {
wrapped.setProperties(properties);
}
public List<Long> recommendAll(final Message input, final Integer limit);
}

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/java/net/recommenders/plista/rec/WrappedRecommender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.recommenders.plista.rec;

import java.util.List;
import java.util.Properties;
import net.recommenders.plista.client.Message;
import net.recommenders.plista.recommender.Recommender;

/**
*
* @author alejandro
*/
public class WrappedRecommender extends AbstractRecommender implements Recommender, WrappableRecommender {

private Recommender wrapped;
private int factor;

public WrappedRecommender(Recommender wrapped) {
this(wrapped, 10);
}

public WrappedRecommender(Recommender wrapped, int factor) {
super(wrapped);
this.wrapped = wrapped;
this.factor = factor;
}

@Override
public List<Long> recommendAll(Message message, Integer limit) {
return wrapped.recommend(message, (factor * limit));
}

public List<Long> recommend(Message message, Integer limit) {
return wrapped.recommend(message, limit);
}
}

0 comments on commit 942e310

Please sign in to comment.