Skip to content

Commit

Permalink
Revert "introduce builder pattern"
Browse files Browse the repository at this point in the history
This reverts commit b1b21fb.
  • Loading branch information
twobiers committed Jul 3, 2023
1 parent b1b21fb commit a8ca03e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,8 @@ public RecommendationResponse handle(final RecommendationRequest request) {
Collectors.toSet()))).toList();

// 4. Based on those results, calculate a confidence score for each supervisor, organization and project
final var confidenceScoreCalculator = ConfidenceScoreCalculator.builder()
.withSeedTags(request.seedTags())
.withSeedLecturers(matchingSupervisors)
.withSeedOrganizations(matchingOrganizations)
.withSeedProjects(matchingProjects)
.build();
final var confidenceScoreCalculator = new ConfidenceScoreCalculator(
matchingProjects, matchingSupervisors, matchingOrganizations, request.seedTags());

// 5. Return the top results for each category together with the confidence score
final var topLecturers = pickResults(confidenceScoreCalculator.getLecturerConfidenceScores(),
Expand All @@ -92,6 +88,8 @@ public RecommendationResponse handle(final RecommendationRequest request) {
final var topOrganizations = pickResults(
confidenceScoreCalculator.getOrganizationConfidenceScores(),
organizationFacade::get, OrganizationDto::id, request.excludedIds())
.sorted((e1, e2) -> e2.confidenceScore().compareTo(e1.confidenceScore()))
.limit(limit)
.toList();

Comparator<RecommendationResult<ProjectDto>> projectComparator = Comparator.comparing(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class ConfidenceScoreCalculator {
Expand All @@ -24,7 +23,7 @@ public class ConfidenceScoreCalculator {
Map<UUID, Double> organizationConfidenceScores = null;
Map<UUID, Double> projectConfidenceScores = null;

protected ConfidenceScoreCalculator(
public ConfidenceScoreCalculator(
List<ProjectRecommendation> projectRecommendations,
List<LecturerRecommendation> lecturerRecommendations,
List<OrganizationRecommendation> organizationRecommendations,
Expand All @@ -36,43 +35,39 @@ protected ConfidenceScoreCalculator(
this.seedTags = seedTags;
}

public static ConfidenceScoreCalculatorBuilder builder() {
return new ConfidenceScoreCalculatorBuilder();
}

public Map<UUID, Double> getLecturerConfidenceScores() {
if (lecturerConfidenceScores == null) {
this.lecturerConfidenceScores = calculateLecturerConfidenceScores();
calculateLecturerConfidenceScores();
}
return Map.copyOf(lecturerConfidenceScores);
}

public Map<UUID, Double> getOrganizationConfidenceScores() {
if (organizationConfidenceScores == null) {
this.organizationConfidenceScores = calculateOrganizationConfidenceScores();
calculateOrganizationConfidenceScores();
}
return Map.copyOf(organizationConfidenceScores);
}

public Map<UUID, Double> getProjectConfidenceScores() {
if (projectConfidenceScores == null) {
this.projectConfidenceScores = calculateProjectConfidenceScores();
calculateProjectConfidenceScores();
}
return Map.copyOf(projectConfidenceScores);
}

private Map<UUID, Double> calculateLecturerConfidenceScores() {
var results = new HashMap<UUID, Double>();
private void calculateLecturerConfidenceScores() {
this.lecturerConfidenceScores = new HashMap<>();
for (var supervisor : this.lecturerRecommendations) {
final var score =
LECTURER_PROFILE_MATCH_BOOST_FACTOR * overlapCoefficientCalculator.calculate(seedTags,
supervisor.tags());
addConfidenceScore(results, supervisor.id(), score, 1);
addConfidenceScore(this.lecturerConfidenceScores, supervisor.id(), score, 1);
}

var projectScores = this.getProjectConfidenceScores();
if (projectScores == null || projectScores.isEmpty()) {
return results;
return;
}

for (var entry : projectScores.entrySet()) {
Expand All @@ -87,26 +82,24 @@ private Map<UUID, Double> calculateLecturerConfidenceScores() {
var supervisors = project.supervisors();
if (score > 0.0 && supervisors != null) {
for (var supervisor : supervisors) {
addConfidenceScore(results, supervisor, score, 2.0);
addConfidenceScore(this.lecturerConfidenceScores, supervisor, score, 2.0);
}
}
}

return results;
}

private Map<UUID, Double> calculateOrganizationConfidenceScores() {
var results = new HashMap<UUID, Double>();
private void calculateOrganizationConfidenceScores() {
this.organizationConfidenceScores = new HashMap<>();
for (var org : this.organizationRecommendations) {
final var score =
ORGANISATION_PROFILE_MATCH_BOOST_FACTOR * overlapCoefficientCalculator.calculate(seedTags,
org.tags());
addConfidenceScore(results, org.id(), score, 1);
addConfidenceScore(this.organizationConfidenceScores, org.id(), score, 1);
}

var projectScores = this.getProjectConfidenceScores();
if (projectScores == null || projectScores.isEmpty()) {
return results;
return;
}

for (var entry : projectScores.entrySet()) {
Expand All @@ -121,21 +114,19 @@ private Map<UUID, Double> calculateOrganizationConfidenceScores() {

var partner = project.partner();
if (score > 0.0 && partner != null) {
addConfidenceScore(results, partner, score, 2.0);
addConfidenceScore(this.organizationConfidenceScores, partner, score, 2.0);
}
}
return results;
}

private Map<UUID, Double> calculateProjectConfidenceScores() {
var results = new HashMap<UUID, Double>();
private void calculateProjectConfidenceScores() {
this.projectConfidenceScores = new HashMap<>();
for (var project : this.projectRecommendations) {
final var score =
PROJECT_MATCH_BOOST_FACTOR * overlapCoefficientCalculator.calculate(seedTags,
project.tags());
addConfidenceScore(results, project.id(), score, 1);
addConfidenceScore(this.projectConfidenceScores, project.id(), score, 1);
}
return results;
}

private <T> void addConfidenceScore(final Map<T, Double> map, final T obj, final double score,
Expand All @@ -147,47 +138,4 @@ private <T> void addConfidenceScore(final Map<T, Double> map, final T obj, final
return (value + score) / divisor;
});
}

public static class ConfidenceScoreCalculatorBuilder {

public List<LecturerRecommendation> lecturerRecommendations = List.of();
public List<OrganizationRecommendation> organizationRecommendations = List.of();
public List<ProjectRecommendation> projectRecommendations = List.of();
public List<UUID> seedTags = List.of();

protected ConfidenceScoreCalculatorBuilder() {
}

public ConfidenceScoreCalculator build() {
return new ConfidenceScoreCalculator(
projectRecommendations, lecturerRecommendations, organizationRecommendations, seedTags
);
}

public ConfidenceScoreCalculatorBuilder withSeedLecturers(
List<LecturerRecommendation> lecturers) {
Objects.requireNonNull(lecturers);
this.lecturerRecommendations = lecturers;
return this;
}

public ConfidenceScoreCalculatorBuilder withSeedOrganizations(
List<OrganizationRecommendation> organizations) {
Objects.requireNonNull(organizations);
this.organizationRecommendations = organizations;
return this;
}

public ConfidenceScoreCalculatorBuilder withSeedProjects(List<ProjectRecommendation> projects) {
Objects.requireNonNull(projects);
this.projectRecommendations = projects;
return this;
}

public ConfidenceScoreCalculatorBuilder withSeedTags(List<UUID> tags) {
Objects.requireNonNull(tags);
this.seedTags = tags;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.innovationhub.prox.modules.recommendation.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

import java.util.HashSet;
import java.util.List;
Expand All @@ -9,29 +9,29 @@
import org.junit.jupiter.api.Test;

class ConfidenceScoreCalculatorTest {

List<UUID> seedTags = List.of(UUID.randomUUID(), UUID.randomUUID());
List<UUID> mixedTags = List.of(seedTags.get(0), UUID.randomUUID());
List<UUID> randomTags = List.of(UUID.randomUUID(), UUID.randomUUID());
List<OrganizationRecommendation> givenOrganizations = List.of(
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(seedTags)),
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(mixedTags)),
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(randomTags))
);
UUID supervisorId = UUID.randomUUID();

List<ProjectRecommendation> givenProjects = List.of(
new ProjectRecommendation(UUID.randomUUID(), Set.of(supervisorId), UUID.randomUUID(),
new HashSet<>(seedTags)),
new ProjectRecommendation(UUID.randomUUID(), Set.of(UUID.randomUUID()), UUID.randomUUID(),
new HashSet<>(mixedTags)),
new ProjectRecommendation(UUID.randomUUID(), Set.of(UUID.randomUUID()), UUID.randomUUID(),
new HashSet<>(randomTags))
new ProjectRecommendation(UUID.randomUUID(), Set.of(supervisorId), UUID.randomUUID(), new HashSet<>(seedTags)),
new ProjectRecommendation(UUID.randomUUID(), Set.of(UUID.randomUUID()), UUID.randomUUID(), new HashSet<>(mixedTags)),
new ProjectRecommendation(UUID.randomUUID(), Set.of(UUID.randomUUID()), UUID.randomUUID(), new HashSet<>(randomTags))
);

List<LecturerRecommendation> givenLecturers = List.of(
new LecturerRecommendation(supervisorId, new HashSet<>(seedTags)),
new LecturerRecommendation(UUID.randomUUID(), new HashSet<>(mixedTags)),
new LecturerRecommendation(UUID.randomUUID(), new HashSet<>(randomTags))
);

List<OrganizationRecommendation> givenOrganizations = List.of(
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(seedTags)),
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(mixedTags)),
new OrganizationRecommendation(UUID.randomUUID(), new HashSet<>(randomTags))
);

ConfidenceScoreCalculator confidenceScoreCalculator = new ConfidenceScoreCalculator(
givenProjects,
givenLecturers,
Expand Down

0 comments on commit a8ca03e

Please sign in to comment.