• 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;
        }
    
    
    }

  • 相关阅读:
    阻止事件冒泡
    移动端开发
    angular6 管道多参数传输 Pipe
    在div上添加小三角
    angular6 使用@Input() @Output()
    angular6 想要获取页面某些事件 如 点击 window宽高等等
    如何将项目添加到git上
    使用vue-cli 搭建element-admin后台
    bootstrap4 常用样式类名 (供自己参考)
    键盘按下 keyCode 的值
  • 原文地址:https://www.cnblogs.com/jamesf/p/4751592.html
Copyright © 2020-2023  润新知