package com.crawler.analysis; import com.crawler.model.Movie; import java.util.*; import java.util.stream.Collectors; public class MovieAnalyzer { // 统计电影评分分布 public static Map analyzeRatingDistribution(List movieList) { Map ratingMap = new TreeMap<>(); for (Movie movie : movieList) { if (movie != null) { double rating = movie.getRating(); ratingMap.put(rating, ratingMap.getOrDefault(rating, 0) + 1); } } return ratingMap; } // 统计电影年份分布 public static Map analyzeYearDistribution(List movieList) { Map yearMap = new TreeMap<>(); for (Movie movie : movieList) { if (movie != null && movie.getYear() != null) { String year = movie.getYear(); yearMap.put(year, yearMap.getOrDefault(year, 0) + 1); } } return yearMap; } // 统计电影类型分布 public static Map analyzeGenreDistribution(List movieList) { Map genreMap = new HashMap<>(); for (Movie movie : movieList) { if (movie != null && movie.getGenre() != null) { String genre = movie.getGenre(); genreMap.put(genre, genreMap.getOrDefault(genre, 0) + 1); } } return genreMap; } // 统计电影国家/地区分布 public static Map analyzeCountryDistribution(List movieList) { Map countryMap = new HashMap<>(); for (Movie movie : movieList) { if (movie != null && movie.getCountry() != null) { String country = movie.getCountry(); countryMap.put(country, countryMap.getOrDefault(country, 0) + 1); } } return countryMap; } // 分析导演作品数量排行 public static Map analyzeDirectorWorks(List movieList) { Map directorMap = new HashMap<>(); for (Movie movie : movieList) { if (movie != null && movie.getDirector() != null) { String director = movie.getDirector(); directorMap.put(director, directorMap.getOrDefault(director, 0) + 1); } } // 按作品数量排序 return directorMap.entrySet().stream() .sorted(Map.Entry.comparingByValue().reversed()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )); } // 计算平均评分 public static double calculateAverageRating(List movieList) { return movieList.stream() .filter(Objects::nonNull) .mapToDouble(Movie::getRating) .average() .orElse(0.0); } // 计算评分与年份的相关性(简单计算) public static Map analyzeYearRatingCorrelation(List movieList) { Map> yearRatingsMap = new TreeMap<>(); for (Movie movie : movieList) { if (movie != null && movie.getYear() != null) { String year = movie.getYear(); double rating = movie.getRating(); yearRatingsMap.computeIfAbsent(year, k -> new ArrayList<>()).add(rating); } } // 计算每年的平均评分 Map yearAverageRatingMap = new TreeMap<>(); for (Map.Entry> entry : yearRatingsMap.entrySet()) { String year = entry.getKey(); List ratings = entry.getValue(); double average = ratings.stream().mapToDouble(Double::doubleValue).average().orElse(0.0); yearAverageRatingMap.put(year, average); } return yearAverageRatingMap; } }