电影推荐系统java源码
时间: 2023-11-28 09:02:21 浏览: 95
基于协同过滤算法的电影推荐系统源码+数据库(java毕业设计).zip
3星 · 编辑精心推荐
电影推荐系统是一个通过分析用户的兴趣和电影的特征,为用户推荐个性化电影的系统。下面是一个简单的电影推荐系统的Java源码:
```java
import java.util.*;
class Movie {
private String title;
private List<String> genres;
public Movie(String title, List<String> genres) {
this.title = title;
this.genres = genres;
}
public String getTitle() {
return title;
}
public List<String> getGenres() {
return genres;
}
}
class User {
private List<String> watchedMovies;
public User() {
watchedMovies = new ArrayList<>();
}
public void addWatchedMovie(String movieTitle) {
watchedMovies.add(movieTitle);
}
public List<String> getWatchedMovies() {
return watchedMovies;
}
}
public class MovieRecommender {
private List<Movie> movieList;
private List<User> userList;
public MovieRecommender() {
movieList = new ArrayList<>();
userList = new ArrayList<>();
}
public void addMovie(Movie movie) {
movieList.add(movie);
}
public void addUser(User user) {
userList.add(user);
}
public List<Movie> getRecommendedMovies(User user, int numMovies) {
Map<Movie, Integer> movieScores = new HashMap<>();
for (Movie movie : movieList) {
int score = 0;
for (String genre : movie.getGenres()) {
if (!user.getWatchedMovies().contains(movie.getTitle()) && user.getWatchedMovies().contains(genre)) {
score++;
}
}
movieScores.put(movie, score);
}
List<Movie> recommendedMovies = new ArrayList<>(movieScores.keySet());
recommendedMovies.sort((m1, m2) -> movieScores.get(m2) - movieScores.get(m1));
return recommendedMovies.subList(0, Math.min(numMovies, recommendedMovies.size()));
}
public static void main(String[] args) {
// 创建电影推荐系统
MovieRecommender recommender = new MovieRecommender();
// 添加电影
recommender.addMovie(new Movie("Titanic", Arrays.asList("Drama", "Romance")));
recommender.addMovie(new Movie("The Matrix", Arrays.asList("Action", "Sci-fi")));
recommender.addMovie(new Movie("Avatar", Arrays.asList("Action", "Adventure", "Fantasy")));
// 添加用户和用户的观影历史
User user = new User();
user.addWatchedMovie("Drama");
user.addWatchedMovie("Romance");
recommender.addUser(user);
// 获取推荐电影
List<Movie> recommendedMovies = recommender.getRecommendedMovies(user, 2);
// 输出推荐电影
for (Movie movie : recommendedMovies) {
System.out.println(movie.getTitle());
}
}
}
```
这个源码实现了一个简单的电影推荐系统,并通过用户的观影历史和电影的类型来进行推荐。在示例中,我们创建了一个电影推荐系统,添加了几部电影,并添加了一个用户和用户的观影历史。然后,我们从用户观影历史和电影类型中计算得分,并按得分对电影进行排序,最后返回按得分排序过的推荐电影列表。
注意:以上仅为一个简单的示例代码,实际的电影推荐系统通常需要更多的数据和复杂的算法来实现更准确的个性化推荐。
阅读全文