All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codelibs.elasticsearch.taste.recommender.UserBasedRecommenderBuilder Maven / Gradle / Ivy

package org.codelibs.elasticsearch.taste.recommender;

import java.util.HashMap;
import java.util.Map;

import org.codelibs.elasticsearch.taste.exception.TasteException;
import org.codelibs.elasticsearch.taste.model.DataModel;
import org.codelibs.elasticsearch.taste.model.IndexInfo;
import org.codelibs.elasticsearch.taste.neighborhood.UserNeighborhood;
import org.codelibs.elasticsearch.taste.neighborhood.UserNeighborhoodFactory;
import org.codelibs.elasticsearch.taste.similarity.UserSimilarity;
import org.codelibs.elasticsearch.util.settings.SettingsUtils;

public class UserBasedRecommenderBuilder extends AbstractRecommenderBuilder {

    public UserBasedRecommenderBuilder(final IndexInfo indexInfo,
            final Map rootSettings) {
        super(indexInfo, rootSettings);
    }

    @Override
    public Recommender buildRecommender(final DataModel dataModel) {
        final Map similaritySettings = SettingsUtils.get(
                rootSettings, "similarity", new HashMap());
        similaritySettings.put(DATA_MODEL_ATTR, dataModel);
        final UserSimilarity similarity = createSimilarity(similaritySettings);

        final Map neighborhoodSettings = SettingsUtils.get(
                rootSettings, "neighborhood", new HashMap());
        neighborhoodSettings.put(DATA_MODEL_ATTR, dataModel);
        neighborhoodSettings.put(USER_SIMILARITY_ATTR, similarity);
        final UserNeighborhood neighborhood = createUserNeighborhood(neighborhoodSettings);

        return new GenericUserBasedRecommender(dataModel, neighborhood,
                similarity);
    }

    protected UserNeighborhood createUserNeighborhood(
            final Map neighborhoodSettings) {
        final String factoryName = SettingsUtils
                .get(neighborhoodSettings, "factory",
                        "org.codelibs.elasticsearch.taste.neighborhood.NearestNUserNeighborhoodFactory");
        try {
            final Class clazz = Class.forName(factoryName);
            final UserNeighborhoodFactory userNeighborhoodFactory = (UserNeighborhoodFactory) clazz
                    .newInstance();
            userNeighborhoodFactory.init(neighborhoodSettings);
            return userNeighborhoodFactory.create();
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException e) {
            throw new TasteException("Could not create an instance of "
                    + factoryName, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy