Example usage for org.apache.mahout.cf.taste.similarity UserSimilarity setPreferenceInferrer

List of usage examples for org.apache.mahout.cf.taste.similarity UserSimilarity setPreferenceInferrer

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.similarity UserSimilarity setPreferenceInferrer.

Prototype

void setPreferenceInferrer(PreferenceInferrer inferrer);

Source Link

Document

Attaches a PreferenceInferrer to the UserSimilarity implementation.

Usage

From source file:cf.wikipedia.WikipediaTasteUserDemo.java

License:Apache License

public static void main(String[] args)
        throws IOException, TasteException, SAXException, ParserConfigurationException {
    String recsFile = args[0];/*from  ww  w.  ja  v a2  s  .  co  m*/
    String docIdsTitle = args[1];
    Integer neighborhoodSize = Integer.parseInt(args[2]);
    Long userId = Long.parseLong(args[3]);
    boolean printCommonalities = Boolean.parseBoolean(args[4]);
    InputSource is = new InputSource(new FileInputStream(docIdsTitle));
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    SAXParser sp = factory.newSAXParser();
    WikiContentHandler handler = new WikiContentHandler();
    sp.parse(is, handler);
    //create the data model
    FileDataModel dataModel = new FileDataModel(new File(recsFile));
    System.out.println("Data Model: Users: " + dataModel.getNumUsers() + " Items: " + dataModel.getNumItems());

    UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(dataModel);
    // Optional:
    userSimilarity.setPreferenceInferrer(new AveragingPreferenceInferrer(dataModel));
    //Get a neighborhood of users
    UserNeighborhood neighborhood = new NearestNUserNeighborhood(neighborhoodSize, userSimilarity, dataModel);
    //Create the recommender
    Recommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, userSimilarity);
    System.out.println("-----");
    System.out.println("User: " + userId);
    //Print out the users own preferences first
    TasteUtils.printPreferences(dataModel, userId, handler.map);
    if (printCommonalities) {
        long[] users = neighborhood.getUserNeighborhood(userId);
        for (int i = 0; i < users.length; i++) {
            long neighbor = users[i];
            System.out.println("Neighbor: " + neighbor);
            TasteUtils.printCommonalities(dataModel, userId, neighbor, handler.map);
        }

        System.out.println("");
    }
    //Get the top 5 recommendations
    List<RecommendedItem> recommendations = recommender.recommend(userId, 5);
    TasteUtils.printRecs(recommendations, handler.map);
}