Search in sources :

Example 1 with RecommendedMovie

use of com.po.RecommendedMovie in project Movie by batsqd.

the class ImproveSlopeOne method getPredictMovies.

//返回的map<Movie_Id,Score>
public List<RecommendedMovie> getPredictMovies(int userid, int mums) {
    double[][] primitiveRatingData = ReadRatingData.readRatingFile(Configs.rating_file_path, Configs.countUsers, Configs.countItems);
    Node[][] MovieFansDataset = getMovieFansDataset(Configs.user_file_path, Configs.countUsers, Configs.num_of_feature, primitiveRatingData);
    HashMap<String, double[][]> getDev_avg = getDev_avg(MovieFansDataset);
    double[][] dev_avg = getDev_avg.get("dev_avg");
    List<RecommendedMovie> list = new ArrayList<RecommendedMovie>();
    for (int movie_id = 0; movie_id < Configs.countItems; movie_id++) {
        //应该注意的是在评价矩阵中user_id 0开始,数据库user_id 1开始
        double recommend_value = getPredictUseriToItemj(userid - 1, movie_id, primitiveRatingData, dev_avg);
        if (recommend_value >= 3) {
            //电影预测值大于3,随机选择list中5部电影推送给用户;
            RecommendedMovie recommendedMovie = new RecommendedMovie();
            recommendedMovie.setMovie_id(movie_id);
            recommendedMovie.setRecommend_vale(recommend_value);
            list.add(recommendedMovie);
        }
    //System.out.println(list.size());
    //在这里使用二叉堆选择排名较高的电影,可能要考虑多线程
    }
    List<RecommendedMovie> selectMoviesByRandomToUser = new ArrayList<RecommendedMovie>();
    //randomCommon(0, list.size(), 5) 产生【0 list.size()-1】中的任何随机数5个
    List<Integer> indexs = RandomNumsGenerator.randomCommon(0, list.size(), 5);
    for (int i = 0; i < indexs.size(); i++) {
        selectMoviesByRandomToUser.add(list.get(indexs.get(i)));
    }
    return selectMoviesByRandomToUser;
//打印所有推荐的电影
/*
	    for(int i=0;i<list.size();i++){
	    	System.out.println(list.get(i));
	    }*/
/*
	    ComparatorRecommendedMovie comparator=new ComparatorRecommendedMovie();
	    Collections.sort(list, comparator);
	    
	   List<RecommendedMovie> top_nums_movies=new ArrayList<RecommendedMovie>();
	   for(int i=0;i<mums;i++){
		   //推荐值<2.6不向用户推荐
		   if(list.get(i).getRecommend_vale()>2.6){
			   top_nums_movies.add(list.get(i));
		   }
		   
	   }
	   for(int i=0;i<top_nums_movies.size();i++){
	    	System.out.println(top_nums_movies.get(i).getRecommend_vale());
	    }		
		return top_nums_movies;
		*/
}
Also used : RecommendedMovie(com.po.RecommendedMovie) ComparatorRecommendedMovie(com.util.ComparatorRecommendedMovie) ArrayList(java.util.ArrayList)

Example 2 with RecommendedMovie

use of com.po.RecommendedMovie in project Movie by batsqd.

the class MovieController method getRecommendedMoviesByOurAlgorithm.

/////////////////////////////////////////////////////
@RequestMapping("getRecommendedMoviesByOurAlgorithm.action")
public ModelAndView getRecommendedMoviesByOurAlgorithm(HttpSession session) throws Exception {
    User user = (User) session.getAttribute("user");
    int user_id = user.getUserid();
    int recommend_movie_num = Configs.recommend_movie_num;
    ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
    WatchMovieMapper watchMovieMapper = (WatchMovieMapper) ac.getBean("watchMovieMapper");
    int numsOfRatedMoviesByUserId = watchMovieMapper.numsOfRatedMoviesByUserId(user_id);
    ModelAndView modelAndView = new ModelAndView();
    if (numsOfRatedMoviesByUserId < 5) {
        modelAndView.addObject("numsOfRatedMoviesByUserId", numsOfRatedMoviesByUserId);
        modelAndView.setViewName("/WEB-INF/jsp/guessYouLike.jsp");
        return modelAndView;
    } else {
        //超过5人评价,才做预测
        ImproveSlopeOne improveSlopeOne = new ImproveSlopeOne();
        List<RecommendedMovie> recommendedMovieslist = improveSlopeOne.getPredictMovies(user_id, recommend_movie_num);
        //System.out.println("=========================================");
        //System.out.println("推荐电影的数量:"+recommendedMovieslist.size());
        //System.out.println(recommendedMovieslist.toString());
        //System.out.println("==========================================");
        MovieMapper movieMapper = (MovieMapper) ac.getBean("movieMapper");
        RecommendMapper recommendMapper = (RecommendMapper) ac.getBean("recommendMapper");
        ArrayList<Movie> moviesList = new ArrayList<Movie>();
        for (int i = 0; i < recommendedMovieslist.size(); i++) {
            double movie_recommend_value = recommendedMovieslist.get(i).getRecommend_vale();
            int recommendedMovie_id = recommendedMovieslist.get(i).getMovie_id();
            HashMap<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("movie_id", recommendedMovie_id);
            parameter.put("user_id", user_id);
            parameter.put("recommend_value", movie_recommend_value);
            //最好处理下异常
            //只是保存user_id movie_id recommend_value timestamp
            recommendMapper.saveRecommendedMovies(parameter);
            Movie movie = movieMapper.selectMovieById(recommendedMovie_id);
            if (movie != null) {
                moviesList.add(movie);
            }
        }
        modelAndView.addObject("moviesList", moviesList);
        modelAndView.setViewName("/WEB-INF/jsp/guessYouLike.jsp");
        return modelAndView;
    }
}
Also used : RecommendedMovie(com.po.RecommendedMovie) Movie(com.po.Movie) User(com.po.User) ImproveSlopeOne(com.recommender.ImproveSlopeOne) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) RecommendedMovie(com.po.RecommendedMovie) ApplicationContext(org.springframework.context.ApplicationContext) WatchMovieMapper(com.mapper.WatchMovieMapper) RecommendMapper(com.mapper.RecommendMapper) MovieMapper(com.mapper.MovieMapper) WatchMovieMapper(com.mapper.WatchMovieMapper) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with RecommendedMovie

use of com.po.RecommendedMovie in project Movie by batsqd.

the class ComparatorRecommendedMovie method compare.

@Override
public int compare(Object o1, Object o2) {
    // TODO Auto-generated method stub
    RecommendedMovie recommendedMovie1 = (RecommendedMovie) o1;
    RecommendedMovie recommendedMovie2 = (RecommendedMovie) o2;
    if (recommendedMovie1.getRecommend_vale() > recommendedMovie2.getRecommend_vale()) {
        return -1;
    } else if (recommendedMovie1.getRecommend_vale() < recommendedMovie2.getRecommend_vale()) {
        return 1;
    } else {
        return 0;
    }
}
Also used : RecommendedMovie(com.po.RecommendedMovie)

Example 4 with RecommendedMovie

use of com.po.RecommendedMovie in project Movie by batsqd.

the class TestImprovedSlopOne method main.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ImproveSlopeOne improveSlopeOne = new ImproveSlopeOne();
    List<RecommendedMovie> list = improveSlopeOne.getPredictMovies(1, 5);
    for (int i = 0; i < list.size(); i++) {
        System.out.println("movie_id:" + list.get(i).getMovie_id() + " recommended_value:" + list.get(i).getRecommend_vale());
    }
    System.out.println("=========list=============");
    System.out.println(list.size());
    System.out.println("=========list=============");
}
Also used : RecommendedMovie(com.po.RecommendedMovie) ImproveSlopeOne(com.recommender.ImproveSlopeOne)

Aggregations

RecommendedMovie (com.po.RecommendedMovie)4 ImproveSlopeOne (com.recommender.ImproveSlopeOne)2 ArrayList (java.util.ArrayList)2 MovieMapper (com.mapper.MovieMapper)1 RecommendMapper (com.mapper.RecommendMapper)1 WatchMovieMapper (com.mapper.WatchMovieMapper)1 Movie (com.po.Movie)1 User (com.po.User)1 ComparatorRecommendedMovie (com.util.ComparatorRecommendedMovie)1 HashMap (java.util.HashMap)1 ApplicationContext (org.springframework.context.ApplicationContext)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1