You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.1 KiB
119 lines
4.1 KiB
package com.crawler.analysis;
|
|
|
|
import com.crawler.model.Movie;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class MovieAnalyzer {
|
|
// 统计电影评分分布
|
|
public static Map<Double, Integer> analyzeRatingDistribution(List<Movie> movieList) {
|
|
Map<Double, Integer> 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<String, Integer> analyzeYearDistribution(List<Movie> movieList) {
|
|
Map<String, Integer> 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<String, Integer> analyzeGenreDistribution(List<Movie> movieList) {
|
|
Map<String, Integer> 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<String, Integer> analyzeCountryDistribution(List<Movie> movieList) {
|
|
Map<String, Integer> 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<String, Integer> analyzeDirectorWorks(List<Movie> movieList) {
|
|
Map<String, Integer> 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.<String, Integer>comparingByValue().reversed())
|
|
.collect(Collectors.toMap(
|
|
Map.Entry::getKey,
|
|
Map.Entry::getValue,
|
|
(e1, e2) -> e1,
|
|
LinkedHashMap::new
|
|
));
|
|
}
|
|
|
|
// 计算平均评分
|
|
public static double calculateAverageRating(List<Movie> movieList) {
|
|
return movieList.stream()
|
|
.filter(Objects::nonNull)
|
|
.mapToDouble(Movie::getRating)
|
|
.average()
|
|
.orElse(0.0);
|
|
}
|
|
|
|
// 计算评分与年份的相关性(简单计算)
|
|
public static Map<String, Double> analyzeYearRatingCorrelation(List<Movie> movieList) {
|
|
Map<String, List<Double>> 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<String, Double> yearAverageRatingMap = new TreeMap<>();
|
|
for (Map.Entry<String, List<Double>> entry : yearRatingsMap.entrySet()) {
|
|
String year = entry.getKey();
|
|
List<Double> ratings = entry.getValue();
|
|
double average = ratings.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
|
|
yearAverageRatingMap.put(year, average);
|
|
}
|
|
|
|
return yearAverageRatingMap;
|
|
}
|
|
}
|