org.codelibs.elasticsearch.taste.recommender.ItemAverageRecommender Maven / Gradle / Ivy
/**
* 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.codelibs.elasticsearch.taste.recommender;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.codelibs.elasticsearch.taste.common.FastByIDMap;
import org.codelibs.elasticsearch.taste.common.FastIDSet;
import org.codelibs.elasticsearch.taste.common.FullRunningAverage;
import org.codelibs.elasticsearch.taste.common.LongPrimitiveIterator;
import org.codelibs.elasticsearch.taste.common.RefreshHelper;
import org.codelibs.elasticsearch.taste.common.Refreshable;
import org.codelibs.elasticsearch.taste.common.RunningAverage;
import org.codelibs.elasticsearch.taste.exception.NoSuchUserException;
import org.codelibs.elasticsearch.taste.model.DataModel;
import org.codelibs.elasticsearch.taste.model.PreferenceArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
/**
*
* A simple recommender that always estimates preference for an item to be the average of all known preference
* values for that item. No information about users is taken into account. This implementation is provided for
* experimentation; while simple and fast, it may not produce very good recommendations.
*
*/
public final class ItemAverageRecommender extends AbstractRecommender {
private static final Logger log = LoggerFactory
.getLogger(ItemAverageRecommender.class);
private final FastByIDMap itemAverages;
private final ReadWriteLock buildAveragesLock;
private final RefreshHelper refreshHelper;
public ItemAverageRecommender(final DataModel dataModel) {
super(dataModel);
itemAverages = new FastByIDMap();
buildAveragesLock = new ReentrantReadWriteLock();
refreshHelper = new RefreshHelper(new Callable