SlopeOneRecommender无法正常工作

时间:2015-03-07 10:48:18

标签: java maven mahout mahout-recommender

我正在关注piero giacomelli的“Apache Mahout Cookbook”一书。现在,当我使用netbeans作为IDE下载maven源时,我猜测源是来自mahout版本1.0而不是0.8,因为它仅在SlopeOneRecommender导入中显示错误。 这是完整的代码 -

    package com.packtpub.mahout.cookbook.chapter01;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.cli2.OptionException;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;

public class App {



    static final String inputFile = "/home/hadoop/ml-1m/ratings.dat";
    static final String outputFile = "/home/hadoop/ml-1m/ratings.csv";

    public static void main( String[] args ) throws IOException, TasteException, OptionException
    {
        CreateCsvRatingsFile();


        // create data source (model) - from the csv file
        File ratingsFile = new File(outputFile);
        DataModel model = new FileDataModel(ratingsFile);

        // create a simple recommender on our data
        CachingRecommender cachingRecommender = new CachingRecommender(new SlopeOneRecommender(model));

        // for all users
        for (LongPrimitiveIterator it = model.getUserIDs(); it.hasNext();){
            long userId = it.nextLong();

            // get the recommendations for the user
            List<RecommendedItem> recommendations = cachingRecommender.recommend(userId, 10);

            // if empty write something
            if (recommendations.size() == 0){
                System.out.print("User ");
                System.out.print(userId);
                System.out.println(": no recommendations");
            }

            // print the list of recommendations for each
            for (RecommendedItem recommendedItem : recommendations) {
                System.out.print("User ");
                System.out.print(userId);
                System.out.print(": ");
                System.out.println(recommendedItem);
            }
        }
    }

    private static void CreateCsvRatingsFile() throws FileNotFoundException, IOException {

        BufferedReader br = new BufferedReader(new FileReader(inputFile));
        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));


        String line = null;
        String line2write = null;
        String[] temp;


        int i = 0;
        while (
                    (line = br.readLine()) != null
                && i < 1000
                ){
            i++;
            temp = line.split("::");
            line2write = temp[0] + "," + temp[1];
            bw.write(line2write);
            bw.newLine();
            bw.flush();

        }
        br.close();
        bw.close();
    }
}

错误仅在import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;

上显示

因此在我用这个创建对象的那一行。显示的错误是包不存在。 请帮忙。是因为我正在使用更新版本的mahout吗?我甚至不确定我是使用0.8版本还是更高版本,因为我按照书中给出的所有链接。

2 个答案:

答案 0 :(得分:0)

自v0.8以来,SlopeOneRecommender已从mahout中删除。如果要使用它,可以切换到0.7等版本。

<dependency>
    <groupId>org.apache.mahout</groupId>
    <artifactId>mahout-core</artifactId>
    <version>0.7</version>
</dependency>

请参阅http://permalink.gmane.org/gmane.comp.apache.mahout.user/20282

答案 1 :(得分:0)

完全,
自v0.8以来,SlopeOneRecommender已从mahout中删除。所以要么你回到0.7版本。或者,如果您的目的只是尝试mahout,您可以尝试使用其他推荐人,例如:ItemAverageRecommender