com.twitter.graphjet.algorithms.counting.moment.TopSecondDegreeByCountMomentRecsGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.twitter.graphjet.algorithms.counting.moment.TopSecondDegreeByCountMomentRecsGenerator.java

Source

/**
 * Copyright 2017 Twitter. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twitter.graphjet.algorithms.counting.moment;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

import com.google.common.collect.Lists;

import com.twitter.graphjet.algorithms.NodeInfo;
import com.twitter.graphjet.algorithms.RecommendationInfo;
import com.twitter.graphjet.algorithms.RecommendationRequest;
import com.twitter.graphjet.algorithms.counting.GeneratorHelper;

import it.unimi.dsi.fastutil.longs.LongList;

public final class TopSecondDegreeByCountMomentRecsGenerator {

    private TopSecondDegreeByCountMomentRecsGenerator() {
    }

    /**
     * Generate a list of recommendations based on given list of candidate nodes and the original request.
     * @param request         original request message, contains filtering criteria
     * @param candidateNodes  list of candidate nodes
     * @return                list of {@link MomentRecommendationInfo}
     */
    public static List<RecommendationInfo> generateMomentRecs(TopSecondDegreeByCountRequestForMoment request,
            List<NodeInfo> candidateNodes) {

        int maxNumResults = Math.min(request.getMaxNumResults(), RecommendationRequest.MAX_RECOMMENDATION_RESULTS);

        PriorityQueue<NodeInfo> validNodes = GeneratorHelper.getValidNodes(candidateNodes,
                request.getMinUserPerSocialProof(), maxNumResults);

        return getRecommendationsFromNodes(request, validNodes);
    }

    private static List<RecommendationInfo> getRecommendationsFromNodes(
            TopSecondDegreeByCountRequestForMoment request, PriorityQueue<NodeInfo> topNodes) {
        List<RecommendationInfo> outputResults = Lists.newArrayListWithCapacity(topNodes.size());
        int maxNumSocialProofs = request.getMaxNumSocialProofs();

        while (!topNodes.isEmpty()) {
            NodeInfo nodeInfo = topNodes.poll();

            Map<Byte, LongList> topSocialProofs = GeneratorHelper.pickTopSocialProofs(nodeInfo.getSocialProofs(),
                    maxNumSocialProofs);

            MomentRecommendationInfo momentRecs = new MomentRecommendationInfo(nodeInfo.getValue(),
                    nodeInfo.getWeight(), topSocialProofs);
            outputResults.add(momentRecs);
        }
        Collections.reverse(outputResults);
        return outputResults;
    }
}