org.apache.mahout.cf.taste.common.TasteException Java Examples

The following examples show how to use org.apache.mahout.cf.taste.common.TasteException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MostPopularItemsServlet.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  MyrrixRecommender recommender = getRecommender();
  RescorerProvider rescorerProvider = getRescorerProvider();
  try {
    IDRescorer rescorer = rescorerProvider == null ? null :
        rescorerProvider.getMostPopularItemsRescorer(recommender, getRescorerParams(request));
    output(request, response, recommender.mostPopularItems(getHowMany(request), rescorer));
  } catch (NotReadyException nre) {
    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nre.toString());
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
  } catch (IllegalArgumentException iae) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.toString());
  }
}
 
Example #2
Source File: MovieUserRecommender.java    From hiped2 with Apache License 2.0 6 votes vote down vote up
private static void recommend(String ratingsFile, int ... userIds)
    throws TasteException, IOException {
  DataModel model = new FileDataModel(new File(ratingsFile));

  UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

  UserNeighborhood neighborhood =
      new NearestNUserNeighborhood(
          100, similarity, model);

  Recommender recommender =  new GenericUserBasedRecommender(
      model, neighborhood, similarity);

  Recommender cachingRecommender = new CachingRecommender(recommender);

  for(int userId: userIds) {
    System.out.println("UserID " + userId);
    List<RecommendedItem> recommendations =
        cachingRecommender.recommend(userId, 2);
    for(RecommendedItem item: recommendations) {
      System.out.println("  item " + item.getItemID() + " score " + item.getValue());
    }
  }
}
 
Example #3
Source File: ReadyServlet.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response) throws IOException {
  boolean isReady;
  try {
    isReady = getRecommender().isReady();
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
    return;
  }
  if (isReady) {
    response.setStatus(HttpServletResponse.SC_OK);
  } else {
    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
  }
}
 
Example #4
Source File: ParameterOptimizerTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testFindGoodParameterValues() throws Exception {
  Map<String,ParameterRange> propertyRanges = Maps.newHashMap();
  propertyRanges.put("model.als.lambda", new ParameterRange(0.0001, 0.1));
  propertyRanges.put("model.features", new ParameterRange(10, 40));
  Callable<Number> evaluator = new Callable<Number>() {
    @Override
    public Number call() throws IOException, TasteException, InterruptedException {
      MyrrixIRStatistics stats = (MyrrixIRStatistics)
          new PrecisionRecallEvaluator().evaluate(new File("testdata/grouplens100K"), 0.9, 0.5, null);
      return stats == null ? null : stats.getMeanAveragePrecision();
    }
  };
  ParameterOptimizer optimizer = new ParameterOptimizer(propertyRanges, evaluator);
  Map<String,Number> parameterValues = optimizer.findGoodParameterValues();
  log.info(String.valueOf(parameterValues));
  assertEquals(0.1, parameterValues.get("model.als.lambda").doubleValue());
  assertEquals(23, parameterValues.get("model.features").intValue());
}
 
Example #5
Source File: EstimatedStrengthEvaluator.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public EvaluationResult evaluate(MyrrixRecommender recommender,
                                 RescorerProvider provider, // ignored
                                 Multimap<Long,RecommendedItem> testData) throws TasteException {
  DoubleWeightedMean score = new DoubleWeightedMean();
  int count = 0;
  for (Map.Entry<Long,RecommendedItem> entry : testData.entries()) {
    long userID = entry.getKey();
    RecommendedItem itemPref = entry.getValue();
    try {
      float estimate = recommender.estimatePreference(userID, itemPref.getItemID());
      Preconditions.checkState(LangUtils.isFinite(estimate));
      score.increment(1.0 - estimate, itemPref.getValue());
    } catch (NoSuchItemException nsie) {
      // continue
    } catch (NoSuchUserException nsue) {
      // continue
    }
    if (++count % 100000 == 0) {
      log.info("Score: {}", score);
    }
  }
  log.info("Score: {}", score);
  return new EvaluationResultImpl(score.getResult());
}
 
Example #6
Source File: ServerRecommender.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public void ingest(File file) throws TasteException {
  Reader reader = null;
  try {
    reader = IOUtils.openReaderMaybeDecompressing(file);
    ingest(reader);
  } catch (IOException ioe) {
    throw new TasteException(ioe);
  } finally {
    try {
      if (reader != null) {
        reader.close();
      }
    } catch (IOException e) {
      // Can't happen, continue
    }
  }
}
 
Example #7
Source File: CLI.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static void doIngest(String[] programArgs,
                             ClientRecommender recommender,
                             TranslatingRecommender translatingRecommender) throws TasteException {
  if (programArgs.length < 2) {
    throw new ArgumentValidationException("args are file1 [file2 [file3...]]");
  }
  for (int i = 1; i < programArgs.length; i++) {
    File ingestFile = new File(programArgs[i]);
    if (translatingRecommender == null) {
      recommender.ingest(ingestFile);
    } else {
      translatingRecommender.ingest(ingestFile);
    }
  }
}
 
Example #8
Source File: MovieUserEvaluator.java    From hiped2 with Apache License 2.0 5 votes vote down vote up
public static void evaluate(String ratingsFile)
    throws TasteException, IOException {
  DataModel model = new FileDataModel(new File(ratingsFile));
  RecommenderEvaluator evaluator =
      new AverageAbsoluteDifferenceRecommenderEvaluator();
  RecommenderBuilder recommenderBuilder = new MyRecommendBuilder();
  evaluator.evaluate(
      recommenderBuilder,
      null,
      model,
      0.95,
      0.05
  );
}
 
Example #9
Source File: UserbasedRecommender.java    From Building-Recommendation-Engines with MIT License 5 votes vote down vote up
public static void main( String[] args ) throws IOException, TasteException
{
	//user based recommender model
	DataModel model = new FileDataModel(new File("data/dataset.csv"));    	
	UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
	UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
	UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
	List<RecommendedItem> recommendations = recommender.recommend(2, 3);
	for (RecommendedItem recommendation : recommendations) {
	  System.out.println(recommendation);
	}
}
 
Example #10
Source File: ItembasedRecommender.java    From Building-Recommendation-Engines with MIT License 5 votes vote down vote up
public static void main(String[] args) throws TasteException, IOException {
	DataModel model = new FileDataModel(new File("data/dataset.csv"));
   	ItemSimilarity similarity = new LogLikelihoodSimilarity(model);
   	//UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
   	GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(model, similarity);
   	List<RecommendedItem> recommendations = recommender.mostSimilarItems(18, 3);
   	for (RecommendedItem recommendation : recommendations) {
   	  System.out.println(recommendation);
   	}

}
 
Example #11
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * Note that {@link IDRescorer} is not supported in the client now and must be null.
 *
 * @return {@link #recommendToAnonymous(long[], float[], int)} if rescorer is null
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #recommendToAnonymous(long[], float[], int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> recommendToAnonymous(long[] itemIDs,
                                                  float[] values,
                                                  int howMany,
                                                  IDRescorer rescorer) throws TasteException {
  if (rescorer != null) {
    throw new UnsupportedOperationException();
  }
  return recommendToAnonymous(itemIDs, values, howMany);
}
 
Example #12
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Rescorer}s are not available at this time in the model.
 *
 * @return {@link #recommend(long, int)} if rescorer is null
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #recommend(long, int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer)
    throws TasteException {
  if (rescorer != null) {
    throw new UnsupportedOperationException();
  }
  return recommend(userID, howMany);
}
 
Example #13
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> recommend(String userID,
                                                 int howMany,
                                                 boolean considerKnownItems,
                                                 String[] rescorerParams) throws TasteException {
  long longUserID = translateUser(userID);
  Collection<RecommendedItem> originals =
      delegate.recommend(longUserID, howMany, considerKnownItems, rescorerParams);
  return translate(originals);
}
 
Example #14
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Long> getUserItemTimestamps(Long u, Long i) {
    if (model == null) {
        generateDatamodel();
    }
    List<Long> t = new ArrayList<>();
    try {
        t.add(model.getPreferenceTime(u, i));
    } catch (TasteException e) {
        e.printStackTrace();
    }
    return t;
}
 
Example #15
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Long> getUserItems(Long u) {
    if (model == null) {
        generateDatamodel();
    }
    try {
        return model.getItemIDsFromUser(u);
    } catch (TasteException e) {
        e.printStackTrace();
    }
    return Collections.emptySet();
}
 
Example #16
Source File: MyrrixRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated do not call; only present to satisfy Mahout API
 * @throws UnsupportedOperationException in general
 */
@Deprecated
@Override
List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                       int howMany,
                                       Rescorer<LongPair> rescorer,
                                       boolean excludeItemIfNotSimilarToAll) throws TasteException;
 
Example #17
Source File: ServerRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public float[] similarityToItem(long toItemID, long... itemIDs) throws TasteException {

  Generation generation = getCurrentGeneration();
  FastByIDMap<float[]> Y = generation.getY();

  float[] similarities = new float[itemIDs.length];
  Lock yLock = generation.getYLock().readLock();
  yLock.lock();
  try {

    float[] toFeatures = Y.get(toItemID);
    if (toFeatures == null) {
      throw new NoSuchItemException(toItemID);
    }
    double toFeaturesNorm = SimpleVectorMath.norm(toFeatures);

    boolean anyFound = false;
    for (int i = 0; i < similarities.length; i++) {
      float[] features = Y.get(itemIDs[i]);
      if (features == null) {
        similarities[i] = Float.NaN;
      } else {
        anyFound = true;
        double featuresNorm = SimpleVectorMath.norm(features);
        similarities[i] = (float) (SimpleVectorMath.dot(features, toFeatures) / (featuresNorm * toFeaturesNorm));
      }
    }
    if (!anyFound) {
      throw new NoSuchItemException(Arrays.toString(itemIDs));
    }

  } finally {
    yLock.unlock();
  }

  return similarities;
}
 
Example #18
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> mostSimilarItems(String[] itemIDs,
                                                        int howMany,
                                                        String[] rescorerParams,
                                                        String contextUserID) throws TasteException {
  long[] longItemIDs = translateItems(itemIDs);
  Collection<RecommendedItem> originals;
  if (contextUserID == null) {
    originals = delegate.mostSimilarItems(longItemIDs, howMany, rescorerParams, null);
  } else {
    originals = delegate.mostSimilarItems(longItemIDs, howMany, rescorerParams, translateUser(contextUserID));
  }
  return translate(originals);
}
 
Example #19
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> recommendedBecause(String userID, String itemID, int howMany)
    throws TasteException {
  long longUserID = translateUser(userID);
  long longItemID = translateItem(itemID);
  Collection<RecommendedItem> originals = delegate.recommendedBecause(longUserID, longItemID, howMany);
  return translate(originals);
}
 
Example #20
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Rescorer}s are not available at this time in the model.
 *
 * @return {@link #mostSimilarItems(long[], int)} if rescorer is null
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #mostSimilarItems(long[], int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany, Rescorer<LongPair> rescorer)
    throws TasteException {
  if (rescorer != null) {
    throw new UnsupportedOperationException();
  }
  return mostSimilarItems(itemIDs, howMany);
}
 
Example #21
Source File: AbstractAllIDsServlet.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  MyrrixRecommender recommender = getRecommender();
  try {
    FastIDSet ids = isUserIDs() ? recommender.getAllUserIDs() : recommender.getAllItemIDs();
    outputIDs(request, response, ids);
  } catch (NotReadyException nre) {
    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nre.toString());
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
  }
}
 
Example #22
Source File: TranslatingRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * @see net.myrrix.client.ClientRecommender#recommendToAnonymous(long[], float[], int, String[], Long)
 */
List<TranslatedRecommendedItem> recommendToAnonymous(String[] itemIDs,
                                                     float[] values,
                                                     int howMany,
                                                     String[] rescorerParams,
                                                     String contextUserID)
    throws TasteException;
 
Example #23
Source File: PreferenceServlet.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
  CharSequence pathInfo = request.getPathInfo();
  if (pathInfo == null) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No path");
    return;
  }
  Iterator<String> pathComponents = SLASH.split(pathInfo).iterator();
  long userID;
  long itemID;
  try {
    userID = Long.parseLong(pathComponents.next());
    itemID = Long.parseLong(pathComponents.next());
  } catch (NoSuchElementException nsee) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());
    return;
  } catch (NumberFormatException nfe) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nfe.toString());
    return;
  }

  MyrrixRecommender recommender = getRecommender();
  try {
    recommender.removePreference(userID, itemID);
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
  }
}
 
Example #24
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public void ingest(Reader reader) throws TasteException {
  File tempFile = null;
  try {
    tempFile = copyAndTranslateToTempFile(reader);
    delegate.ingest(tempFile);
  } catch (IOException ioe) {
    throw new TasteException(ioe);
  } finally {
    if (tempFile != null && tempFile.exists() && !tempFile.delete()) {
      log.warn("Could not delete {}", tempFile);
    }
  }
}
 
Example #25
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
public List<RecommendedItem> recommendToAnonymous(long[] itemIDs, int howMany) throws TasteException {
  return recommendToAnonymous(itemIDs, null, howMany);
}
 
Example #26
Source File: SimilarityToItemServlet.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

  CharSequence pathInfo = request.getPathInfo();
  if (pathInfo == null) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No path");
    return;
  }
  Iterator<String> pathComponents = SLASH.split(pathInfo).iterator();
  long toItemID;
  List<Long> itemIDsList = Lists.newArrayList();
  try {
    toItemID = Long.parseLong(pathComponents.next());
    while (pathComponents.hasNext()) {
      itemIDsList.add(Long.parseLong(pathComponents.next()));
    }
  } catch (NoSuchElementException nsee) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());
    return;
  } catch (NumberFormatException nfe) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nfe.toString());
    return;
  }
  if (itemIDsList.isEmpty()) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No items");
    return;
  }
  long[] itemIDs = new long[itemIDsList.size()];
  for (int i = 0; i < itemIDs.length; i++) {
    itemIDs[i] = itemIDsList.get(i);
  }

  MyrrixRecommender recommender = getRecommender();
  try {
    float[] similarities = recommender.similarityToItem(toItemID, itemIDs);
    Writer out = response.getWriter();
    for (float similarity : similarities) {
      out.write(Float.toString(similarity));
      out.write('\n');        
    }
  } catch (NoSuchItemException nsie) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND, nsie.toString());
  } catch (NotReadyException nre) {
    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nre.toString());
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
  }
}
 
Example #27
Source File: EstimateServlet.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

  CharSequence pathInfo = request.getPathInfo();
  if (pathInfo == null) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No path");
    return;
  }
  Iterator<String> pathComponents = SLASH.split(pathInfo).iterator();
  long userID;
  List<Long> itemIDsList;
  try {
    userID = Long.parseLong(pathComponents.next());
    itemIDsList = Lists.newArrayList();
    while (pathComponents.hasNext()) {
      itemIDsList.add(Long.parseLong(pathComponents.next()));
    }
  } catch (NoSuchElementException nsee) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());
    return;
  } catch (NumberFormatException nfe) {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, nfe.toString());
    return;
  }

  long[] itemIDs = new long[itemIDsList.size()];
  for (int i = 0; i < itemIDs.length; i++) {
    itemIDs[i] = itemIDsList.get(i);
  }

  MyrrixRecommender recommender = getRecommender();
  try {
    float[] estimates = recommender.estimatePreferences(userID, itemIDs);
    Writer out = response.getWriter();
    for (float estimate : estimates) {
      out.write(Float.toString(estimate));
      out.write('\n');
    }
  } catch (NotReadyException nre) {
    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nre.toString());
  } catch (TasteException te) {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
    getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
  }
}
 
Example #28
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<String> getItemCluster(int n) throws TasteException {
  return translate(delegate.getItemCluster(n));
}
 
Example #29
Source File: DataModelWrapper.java    From rival with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getNumItems() throws TasteException {
    return wrapper.getNumItems();
}
 
Example #30
Source File: AbstractEvaluator.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
@Override
public final EvaluationResult evaluate(MyrrixRecommender recommender,
                                       Multimap<Long,RecommendedItem> testData) throws TasteException {
  return evaluate(recommender, null, testData);
}