public PersonalizationResult personalizeList()

in services/src/main/java/org/apache/unomi/services/sorts/ScorePersonalizationStrategy.java [39:106]


    public PersonalizationResult personalizeList(Profile profile, Session session, PersonalizationService.PersonalizationRequest personalizationRequest) {
        List<String> sortedContent = new ArrayList<>();
        final Map<String,Integer> t = new HashMap<>();

        Integer threshold = (Integer) personalizationRequest.getStrategyOptions().get("threshold");
        if (threshold == null) {
            threshold = 1;
        }

        for (PersonalizationService.PersonalizedContent personalizedContent : personalizationRequest.getContents()) {
            int score = 0;

            String interestList = (String) (personalizedContent.getProperties() != null ? personalizedContent.getProperties().get("interests") : null);
            if (interestList != null) {
                List<Map<String, Object>> profileInterests = (List<Map<String, Object>>) profile.getProperties().get("interests");
                if (profileInterests != null) {
                    for (String interest : interestList.split(" ")) {
                        for (Map<String, Object> profileInterest : profileInterests) {
                            if (profileInterest.get("key").equals(interest)){
                                score += ((Number) profileInterest.get("value")).intValue();
                                break;
                            }
                        }
                    }
                }
            }

            String scoringPlanList = (String) (personalizedContent.getProperties() != null ? personalizedContent.getProperties().get("scoringPlans") : null);
            if (scoringPlanList != null) {
                Map<String,Integer> scoreValues = profile.getScores();
                for (String scoringPlan : scoringPlanList.split(" ")) {
                    if (scoreValues.get(scoringPlan) != null) {
                        score += scoreValues.get(scoringPlan);
                    } else {
                        score++;
                    }
                }
            }

            if (personalizedContent.getFilters() != null) {
                for (PersonalizationService.Filter filter : personalizedContent.getFilters()) {
                    Condition condition = filter.getCondition();
                    if (condition != null && condition.getConditionTypeId() != null) {
                        if (profileService.matchCondition(condition, profile, session)) {
                            if (filter.getProperties() != null && filter.getProperties().get("score") != null) {
                                score += (int) filter.getProperties().get("score");
                            } else {
                                score += 1;
                            }
                        }
                    }
                }
            }
            if (score >= threshold) {
                t.put(personalizedContent.getId(), score);
                sortedContent.add(personalizedContent.getId());
            }
        }

        sortedContent.sort((o1, o2) -> t.get(o2) - t.get(o1));

        String fallback = (String) personalizationRequest.getStrategyOptions().get("fallback");
        if (fallback != null && !sortedContent.contains(fallback)) {
            sortedContent.add(fallback);
        }

        return new PersonalizationResult(sortedContent);
    }