Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.mahout.cf.taste.impl.model;
 
 

This org.apache.mahout.cf.taste.model.DataModel decorator class is useful in a situation where you wish to recommend to a user that doesn't really exist yet in your actual org.apache.mahout.cf.taste.model.DataModel. For example maybe you wish to recommend DVDs to a user who has browsed a few titles on your DVD store site, but, the user is not yet registered.

This enables you to temporarily add a temporary user to an existing org.apache.mahout.cf.taste.model.DataModel in a way that recommenders can then produce recommendations anyway. To do so, wrap your real implementation in this class:

 DataModel realModel = ...;
 DataModel plusModel = new PlusAnonymousUserDataModel(realModel);
 ...
 ItemSimilarity similarity = new LogLikelihoodSimilarity(realModel); // not plusModel
 

But, continue to use realModel as input to other components. To recommend, first construct and set the temporary user information on the model and then simply call the recommender. The synchronized block exists to remind you that this is of course not thread-safe. Only one set of temp data can be inserted into the model and used at one time.

 Recommender recommender = ...;
 ...
 synchronized(...) {
   PreferenceArray tempPrefs = ...;
   plusModel.setTempPrefs(tempPrefs);
   recommender.recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 10);
 }
 
 
 public final class PlusAnonymousUserDataModel implements DataModel {
 
   public static final long TEMP_USER_ID = .;
 
   private final DataModel delegate;
   private PreferenceArray tempPrefs;
   private final FastIDSet prefItemIDs;
 
   public PlusAnonymousUserDataModel(DataModel delegate) {
     this. = delegate;
     this. = new FastIDSet();
   }
 
   public void setTempPrefs(PreferenceArray prefs) {
     this. = prefs;
     this..clear();
     for (int i = 0; i < prefs.length(); i++) {
       this..add(prefs.getItemID(i));
     }
   }
 
   @Override
   }
 
   @Override
   public PreferenceArray getPreferencesFromUser(long userIDthrows TasteException {
     if (userID == ) {
       return ;
     } else {
       return .getPreferencesFromUser(userID);
     }
   }
 
   @Override
   public FastIDSet getItemIDsFromUser(long userIDthrows TasteException {
    if (userID == ) {
      return ;
    } else {
      return .getItemIDsFromUser(userID);
    }
  }
    return .getItemIDs();
    // Yeah ignoring items that only the plus-one user knows about... can't really happen
  }
  public PreferenceArray getPreferencesForItem(long itemIDthrows TasteException {
    PreferenceArray delegatePrefs = .getPreferencesForItem(itemID);
    for (int i = 0; i < .length(); i++) {
      if (.getItemID(i) == itemID) {
        int length = delegatePrefs.length();
        PreferenceArray newPreferenceArray = new GenericItemPreferenceArray(length + 1);
        for (int j = 0; j < lengthj++) {
          newPreferenceArray.setUserID(jdelegatePrefs.getUserID(j));
          newPreferenceArray.setItemID(jdelegatePrefs.getItemID(j));
          newPreferenceArray.setValue(jdelegatePrefs.getValue(j));
        }
        newPreferenceArray.setUserID(length.getUserID(i));
        newPreferenceArray.setItemID(length.getItemID(i));
        newPreferenceArray.setValue(length.getValue(i));
        newPreferenceArray.sortByUser();
        return newPreferenceArray;
      }
    }
    return delegatePrefs;
  }
  public Float getPreferenceValue(long userIDlong itemIDthrows TasteException {
    if (userID == ) {
      for (int i = 0; i < .length(); i++) {
        if (.getItemID(i) == itemID) {
          return .getValue(i);
        }
      }
      return null;
    } else {
      return .getPreferenceValue(userIDitemID);
    }
  }
  public int getNumItems() throws TasteException {
    return .getNumItems();
  }
  public int getNumUsers() throws TasteException {
    return .getNumUsers() + 1;
  }
  public int getNumUsersWithPreferenceFor(long... itemIDsthrows TasteException {
    boolean hasAll = true;
    for (long itemID : itemIDs) {
      boolean found = false;
      for (int i = 0; i < .length(); i++) {
        if (.getItemID(i) == itemID) {
          found = true;
          break;
        }
      }
      if (!found) {
        hasAll = false;
        break;
      }
    }
    return .getNumUsersWithPreferenceFor(itemIDs) + (hasAll ? 1 : 0);
  }
  public void setPreference(long userIDlong itemIDfloat valuethrows TasteException {
    if (userID == ) {
      throw new UnsupportedOperationException();
    } else {
      .setPreference(userIDitemIDvalue);
    }
  }
  public void removePreference(long userIDlong itemIDthrows TasteException {
    if (userID == ) {
      throw new UnsupportedOperationException();
    } else {
      .removePreference(userIDitemID);
    }
  }
  public void refresh(Collection<RefreshablealreadyRefreshed) {
    .refresh(alreadyRefreshed);
  }
New to GrepCode? Check out our FAQ X