Example usage for org.apache.mahout.cf.taste.impl.recommender GenericBooleanPrefItemBasedRecommender GenericBooleanPrefItemBasedRecommender

List of usage examples for org.apache.mahout.cf.taste.impl.recommender GenericBooleanPrefItemBasedRecommender GenericBooleanPrefItemBasedRecommender

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.recommender GenericBooleanPrefItemBasedRecommender GenericBooleanPrefItemBasedRecommender.

Prototype

public GenericBooleanPrefItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity) 

Source Link

Usage

From source file:de.apaxo.bedcon.FacebookRecommender.java

License:Open Source License

/**
 * This function will init the recommender
 * it will load the CSV file from the resource folder,
 * parse it and create the necessary data structures
 * to create a recommender.//from www  . j  a  v a  2 s .co m
 * The 
 */
@PostConstruct
public void initRecommender() {

    try {
        // get the file which is part of the WAR as
        URL url = getClass().getClassLoader().getResource(DATA_FILE_NAME);

        // create a file out of the resource
        File data = new File(url.toURI());

        // create a map for saving the preferences (likes) for
        // a certain person
        Map<Long, List<Preference>> preferecesOfUsers = new HashMap<Long, List<Preference>>();

        // use a CSV parser for reading the file
        // use UTF-8 as character set
        CSVParser parser = new CSVParser(new InputStreamReader(new FileInputStream(data), "UTF-8"));

        // parse out the header
        // we are not using the header
        String[] header = parser.getLine();

        // should output person name
        log.fine(header[0] + " " + header[1]);

        String[] line;

        // go through every line
        while ((line = parser.getLine()) != null) {

            String person = line[0];
            String likeName = line[1];

            // other lines contained but not used
            // String category = line[2];
            // String id = line[3];
            // String created_time = line[4];

            // create a long from the person name
            long userLong = thing2long.toLongID(person);

            // store the mapping for the user
            thing2long.storeMapping(userLong, person);

            // create a long from the like name
            long itemLong = thing2long.toLongID(likeName);

            // store the mapping for the item
            thing2long.storeMapping(itemLong, likeName);

            List<Preference> userPrefList;

            // if we already have a userPrefList use it
            // otherwise create a new one.
            if ((userPrefList = preferecesOfUsers.get(userLong)) == null) {
                userPrefList = new ArrayList<Preference>();
                preferecesOfUsers.put(userLong, userPrefList);
            }
            // add the like that we just found to this user
            userPrefList.add(new GenericPreference(userLong, itemLong, 1));
            log.fine("Adding " + person + "(" + userLong + ") to " + likeName + "(" + itemLong + ")");
        }

        // create the corresponding mahout data structure from the map
        FastByIDMap<PreferenceArray> preferecesOfUsersFastMap = new FastByIDMap<PreferenceArray>();
        for (Entry<Long, List<Preference>> entry : preferecesOfUsers.entrySet()) {
            preferecesOfUsersFastMap.put(entry.getKey(), new GenericUserPreferenceArray(entry.getValue()));
        }

        // create a data model 
        dataModel = new GenericDataModel(preferecesOfUsersFastMap);

        // Instantiate the recommender
        recommender = new GenericBooleanPrefItemBasedRecommender(dataModel,
                new LogLikelihoodSimilarity(dataModel));
    } catch (URISyntaxException e) {
        log.log(Level.SEVERE, "Problem with the file URL", e);
    } catch (FileNotFoundException e) {
        log.log(Level.SEVERE, DATA_FILE_NAME + " was not found", e);
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error during reading line of file", e);
    }
}

From source file:edu.nudt.c6.datasetlinking.mahout.MyRecommenderBuilder.java

License:Apache License

@Override
public Recommender buildRecommender(DataModel dataModel) throws TasteException {
    if (recommenderType == RECOMMENDER.ITEM) {
        ItemSimilarity itemSimilarity = null;
        switch (similarityType) {
        case PEARSON:
            itemSimilarity = new PearsonCorrelationSimilarity(dataModel);
            break;
        case PEARSON_WEIGHTED:
            itemSimilarity = new PearsonCorrelationSimilarity(dataModel, Weighting.WEIGHTED);
            break;
        case COSINE:
            itemSimilarity = new UncenteredCosineSimilarity(dataModel);
            break;
        case TANIMOTO:
            itemSimilarity = new TanimotoCoefficientSimilarity(dataModel);
            break;
        case LOGLIKELIHOOD:
            itemSimilarity = new LogLikelihoodSimilarity(dataModel);
            break;
        case CITYBLOCK:
            itemSimilarity = new CityBlockSimilarity(dataModel);
            break;
        case EUCLIDEAN:
            itemSimilarity = new EuclideanDistanceSimilarity(dataModel);
            break;
        case EUCLIDEAN_WEIGHTED:
            itemSimilarity = new EuclideanDistanceSimilarity(dataModel, Weighting.WEIGHTED);
            break;
        case DATASET_VOCABULARY_COSINE:
            try {
                itemSimilarity = new DatasetVocabularySimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//w ww.j  ava  2 s.c  o m
            }
            break;
        case DATASET_CLASS_COSINE:
            try {
                itemSimilarity = new DatasetClassSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DATASET_PPROPERTY_COSINE_SUBJECTS:
            try {
                itemSimilarity = new DatasetPropertySubjectsSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DATASET_PPROPERTY_COSINE_TRIPLES:
            try {
                itemSimilarity = new DatasetPropertyTriplesSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        default:
            itemSimilarity = new EuclideanDistanceSimilarity(dataModel);
        }

        if (pref) {
            return new GenericItemBasedRecommender(dataModel, itemSimilarity);
        } else {
            return new GenericBooleanPrefItemBasedRecommender(dataModel, itemSimilarity);
        }

    } else if (recommenderType == RECOMMENDER.USER) {
        UserSimilarity userSimilarity = null;
        switch (similarityType) {
        case PEARSON:
            userSimilarity = new PearsonCorrelationSimilarity(dataModel);
            break;
        case PEARSON_WEIGHTED:
            userSimilarity = new PearsonCorrelationSimilarity(dataModel, Weighting.WEIGHTED);
            break;
        case COSINE:
            userSimilarity = new UncenteredCosineSimilarity(dataModel);
            break;
        case SPEARMAN:
            userSimilarity = new SpearmanCorrelationSimilarity(dataModel);
            break;
        case TANIMOTO:
            userSimilarity = new TanimotoCoefficientSimilarity(dataModel);
            break;
        case LOGLIKELIHOOD:
            userSimilarity = new LogLikelihoodSimilarity(dataModel);
            break;
        case CITYBLOCK:
            userSimilarity = new CityBlockSimilarity(dataModel);
            break;
        case EUCLIDEAN:
            userSimilarity = new EuclideanDistanceSimilarity(dataModel);
            break;
        case EUCLIDEAN_WEIGHTED:
            userSimilarity = new EuclideanDistanceSimilarity(dataModel, Weighting.WEIGHTED);
            break;
        case DATASET_VOCABULARY_COSINE:
            try {
                userSimilarity = new DatasetVocabularySimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DATASET_CLASS_COSINE:
            try {
                userSimilarity = new DatasetClassSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DATASET_PPROPERTY_COSINE_SUBJECTS:
            try {
                userSimilarity = new DatasetPropertySubjectsSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DATASET_PPROPERTY_COSINE_TRIPLES:
            try {
                userSimilarity = new DatasetPropertyTriplesSimilarity(dataModel);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        default:
            userSimilarity = new EuclideanDistanceSimilarity(dataModel);
        }

        UserNeighborhood userNeighborhood = null;
        switch (neighborhoodType) {
        case NEAREST:
            userNeighborhood = new NearestNUserNeighborhood(this.nearestNum, userSimilarity, dataModel);
            break;
        case THRESHOLD:
        default:
            userNeighborhood = new ThresholdUserNeighborhood(this.neighborThreshold, userSimilarity, dataModel);
        }

        if (pref) {
            return new GenericUserBasedRecommender(dataModel, userNeighborhood, userSimilarity);
        } else {
            return new GenericBooleanPrefUserBasedRecommender(dataModel, userNeighborhood, userSimilarity);
        }
    } else if (recommenderType == RECOMMENDER.SVD) {
        AbstractFactorizer factorizer = null;

        switch (SVDfactorizerType) {
        case RatingSGD:
            factorizer = new RatingSGDFactorizer(dataModel, factorNum, iterationNum);
            break;
        case ALSWR:
            factorizer = new ALSWRFactorizer(dataModel, factorNum, lambda, iterationNum);
            break;
        case SVDPlusPlus:
            factorizer = new SVDPlusPlusFactorizer(dataModel, factorNum, iterationNum);
            break;
        case ParallelSGD:
            factorizer = new ParallelSGDFactorizer(dataModel, factorNum, lambda, iterationNum);
            break;
        case MyRatingSGD:
            factorizer = new MyRatingSGDFactorizer(dataModel, factorNum, iterationNum);
            break;
        }

        return new SVDRecommender(dataModel, factorizer);

    } else if (recommenderType == RECOMMENDER.LINKDOCUMENT) {

    } else if (recommenderType == RECOMMENDER.CollaborativeRanking) {
        AbstractCRFactorizer factorizer = null;

        switch (CRFactorizerType) {
        case BasicLFM:
            try {
                factorizer = new BasicLFMFactorizer(dataModel, factorNum, iterationNum);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case LFMTrans:
            try {
                factorizer = new LFMTransFactorizer(dataModel, factorNum, iterationNum, learningRate);
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        return new CollaborativeRankingRecommender(dataModel, factorizer);
    } else if (recommenderType == RECOMMENDER.Random) {
        return new RandomRecommender(dataModel);
    } else if (recommenderType == RECOMMENDER.ItemAverage) {
        return new ItemAverageRecommender(dataModel);
    } else if (recommenderType == RECOMMENDER.ItemUserAverage) {
        return new ItemUserAverageRecommender(dataModel);
    }

    return null;

}

From source file:org.zaizi.mahout.config.ItemBasedConfiguration.java

License:Open Source License

public Recommender configure(DataModel dataModel) throws TasteException {
    ItemSimilarity itemSimilarity = itemSimilarityConfiguration.getItemSimilarity(dataModel);

    switch (ratingScheme) {
    case BOOLEAN_PREF:
        return new GenericBooleanPrefItemBasedRecommender(dataModel, itemSimilarity);
    case SCORE_PREF:
        return new GenericItemBasedRecommender(dataModel, itemSimilarity);
    default:/* www . ja v  a  2 s .  c om*/
        throw new TasteException("Only Boolean or Score rating supported.");
    }

}

From source file:tv.icntv.recommend.algorithm.test.RecommendFactory.java

License:Apache License

public static RecommenderBuilder itemRecommender(final ItemSimilarity is, boolean pref) throws TasteException {
    return pref ? new RecommenderBuilder() {
        @Override/*w ww  . j  a  va 2  s . c  o m*/
        public Recommender buildRecommender(DataModel model) throws TasteException {
            return new GenericItemBasedRecommender(model, is);
        }
    } : new RecommenderBuilder() {
        @Override
        public Recommender buildRecommender(DataModel model) throws TasteException {
            return new GenericBooleanPrefItemBasedRecommender(model, is);
        }
    };
}