RecommenderFilterSalaryResult

package org.andy.mymahout.recommendation.job;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.impl.common.FastIDSet;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.IDRescorer;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;


public class RecommenderFilterSalaryResult {


    final static int NEIGHBORHOOD_NUM = 2;
    final static int RECOMMENDER_NUM = 3;


    public static void main(String[] args) throws TasteException, IOException {
        String file = "datafile/job/pv.csv";
        DataModel dataModel = RecommendFactory.buildDataModelNoPref(file);
        RecommenderBuilder rb1 = RecommenderEvaluator.userCityBlock(dataModel);
        RecommenderBuilder rb2 = RecommenderEvaluator.itemLoglikelihood(dataModel);


        Map<Long, Double> averSalary = getAverSalary("datafile/job/job.csv", dataModel);
        
        LongPrimitiveIterator iter = dataModel.getUserIDs();
        while (iter.hasNext()) {
            long uid = iter.nextLong();
            System.out.print("userCityBlock    =>");
            filterSalary(uid, rb1, dataModel, averSalary);
            System.out.print("itemLoglikelihood=>");
            filterSalary(uid, rb2, dataModel, averSalary);
        }
    }


    public static void filterSalary(long uid, RecommenderBuilder recommenderBuilder, DataModel dataModel, Map<Long, Double> averSalary) throws TasteException, IOException {
            Set<Long> jobids = getSalaryJobID(uid, "datafile/job/job.csv", averSalary);
        IDRescorer rescorer = new JobRescorer(jobids);
        List<RecommendedItem> list = recommenderBuilder.buildRecommender(dataModel).recommend(uid, RECOMMENDER_NUM, rescorer);
        RecommendFactory.showItems(uid, list, false);
    }


    public static Set<Long> getSalaryJobID(long uid, String file, Map<Long, Double> averSalary) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File(file)));
        Set<Long> jobids = new HashSet<Long>();
        String s = null;
        while ((s = br.readLine()) != null) {
                String[] cols = s.split(",");
                double salary = Double.valueOf(cols[2]);
                if (salary < averSalary.get(uid)) {
                        jobids.add(Long.parseLong(cols[0]));
                }
        }
        br.close();
        return jobids;
    }
    
    // 获取每个用户的基准比较工资:aver(浏览过的工资)*0.8
    public static Map<Long, Double> getAverSalary(String file, DataModel dataModel) 
                            throws NumberFormatException, IOException, TasteException{
            Map<Long, Double> salaries = new HashMap<Long, Double>();
            BufferedReader br = new BufferedReader(new FileReader(new File(file)));
        String s = null;
        while ((s = br.readLine()) != null) {
                String[] cols = s.split(",");
                salaries.put(Long.parseLong(cols[0]), Double.valueOf(cols[2]));
        }
        br.close();


            Map<Long, Double> averSalaries = new HashMap<Long, Double>();
            LongPrimitiveIterator iter = dataModel.getUserIDs();
            while (iter.hasNext()) {
            long uid = iter.nextLong();
            FastIDSet items = dataModel.getItemIDsFromUser(uid);
            LongPrimitiveIterator itemsIter = items.iterator();
            double sum = 0;
            int count = 0;
            double aver = 0.0;
            while (itemsIter.hasNext()) {
                long item = itemsIter.nextLong();
                double salary = salaries.get(item);
                sum += salary;
                count ++;
            }
            if(count > 0) aver = 0.8*sum/count;
            averSalaries.put(uid, aver);
            }
            return averSalaries;
    }


}

posted @ 2014-04-20 16:58  JamesFan  阅读(211)  评论(0编辑  收藏  举报