用Java编写一个基于用户的协同过滤算法的试题推荐
时间: 2023-10-20 10:23:58 浏览: 87
协同过滤算法是推荐系统中常用的一种算法,它基于用户历史行为数据,通过计算用户之间的相似度,来推荐给用户可能感兴趣的物品。下面是一个基于Java语言实现的基于用户的协同过滤算法的试题推荐程序。
首先,我们需要定义一些数据结构:
```java
public class User {
private int id;
private List<Question> questions = new ArrayList<>();
public User(int id) {
this.id = id;
}
public void addQuestion(Question question) {
questions.add(question);
}
public List<Question> getQuestions() {
return questions;
}
}
public class Question {
private int id;
private List<User> users = new ArrayList<>();
public Question(int id) {
this.id = id;
}
public void addUser(User user) {
users.add(user);
}
public List<User> getUsers() {
return users;
}
}
```
然后,我们可以定义一个类来存储用户和试题的评分信息:
```java
public class Rating {
private User user;
private Question question;
private int score;
public Rating(User user, Question question, int score) {
this.user = user;
this.question = question;
this.score = score;
}
public User getUser() {
return user;
}
public Question getQuestion() {
return question;
}
public int getScore() {
return score;
}
}
```
接下来,我们可以定义一个算法类来计算用户之间的相似度,并根据相似度来推荐题目:
```java
public class CollaborativeFiltering {
private List<User> users;
private List<Question> questions;
private List<Rating> ratings;
public CollaborativeFiltering(List<User> users, List<Question> questions, List<Rating> ratings) {
this.users = users;
this.questions = questions;
this.ratings = ratings;
}
public List<Question> recommendQuestions(User user) {
Map<Question, Integer> scores = new HashMap<>();
Map<User, Integer> similarities = new HashMap<>();
for (Question question : questions) {
int score = 0;
int count = 0;
for (Rating rating : ratings) {
if (rating.getUser() != user || rating.getQuestion() != question) {
continue;
}
score += rating.getScore();
count++;
}
if (count > 0) {
scores.put(question, score / count);
}
}
for (User other : users) {
if (other == user) {
continue;
}
int similarity = 0;
for (Question question : questions) {
int score1 = 0;
int score2 = 0;
for (Rating rating : ratings) {
if (rating.getUser() == user && rating.getQuestion() == question) {
score1 = rating.getScore();
}
if (rating.getUser() == other && rating.getQuestion() == question) {
score2 = rating.getScore();
}
}
similarity += (score1 - score2) * (score1 - score2);
}
similarities.put(other, similarity);
}
List<Question> recommended = new ArrayList<>();
for (Question question : questions) {
if (scores.containsKey(question)) {
continue;
}
int score = 0;
int count = 0;
for (User other : users) {
if (other == user || similarities.get(other) == 0) {
continue;
}
for (Question q : other.getQuestions()) {
if (q == question) {
score += similarities.get(other) * q.getUsers().size();
count += similarities.get(other);
break;
}
}
}
if (count > 0) {
recommended.add(question);
}
}
Collections.sort(recommended, new Comparator<Question>() {
@Override
public int compare(Question o1, Question o2) {
int score1 = scores.containsKey(o1) ? scores.get(o1) : 0;
int score2 = scores.containsKey(o2) ? scores.get(o2) : 0;
int count1 = 0;
int count2 = 0;
for (User other : users) {
if (other == user || similarities.get(other) == 0) {
continue;
}
for (Question q : other.getQuestions()) {
if (q == o1) {
count1 += similarities.get(other);
break;
}
if (q == o2) {
count2 += similarities.get(other);
break;
}
}
}
return score2 * count1 - score1 * count2;
}
});
return recommended;
}
}
```
最后,我们可以用这个算法来推荐题目给用户:
```java
public class Main {
public static void main(String[] args) {
User user1 = new User(1);
User user2 = new User(2);
User user3 = new User(3);
Question question1 = new Question(1);
Question question2 = new Question(2);
Question question3 = new Question(3);
Rating rating1 = new Rating(user1, question1, 5);
Rating rating2 = new Rating(user1, question2, 3);
Rating rating3 = new Rating(user2, question2, 4);
Rating rating4 = new Rating(user2, question3, 5);
Rating rating5 = new Rating(user3, question1, 4);
Rating rating6 = new Rating(user3, question3, 2);
user1.addQuestion(question1);
user1.addQuestion(question2);
user2.addQuestion(question2);
user2.addQuestion(question3);
user3.addQuestion(question1);
user3.addQuestion(question3);
question1.addUser(user1);
question1.addUser(user3);
question2.addUser(user1);
question2.addUser(user2);
question3.addUser(user2);
question3.addUser(user3);
List<User> users = Arrays.asList(user1, user2, user3);
List<Question> questions = Arrays.asList(question1, question2, question3);
List<Rating> ratings = Arrays.asList(rating1, rating2, rating3, rating4, rating5, rating6);
CollaborativeFiltering cf = new CollaborativeFiltering(users, questions, ratings);
List<Question> recommended = cf.recommendQuestions(user1);
System.out.println("Recommended questions for user 1:");
for (Question question : recommended) {
System.out.println(question.getId());
}
}
}
```
上面的代码中,我们创建了三个用户和三个题目,并为每个用户和题目赋予了评分信息。然后,我们使用 CollaborativeFiltering 类来计算用户之间的相似度,并根据相似度来推荐题目给用户。在这个例子中,我们为用户1推荐了题目3。
阅读全文