the-algorithm/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/candidate_sources/base/TwoHopExpansionCandidateSource.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

47 lines
1.6 KiB
Scala

package com.twitter.follow_recommendations.common.candidate_sources.base
import com.twitter.product_mixer.core.functional_component.candidate_source.CandidateSource
import com.twitter.stitch.Stitch
/**
* base trait for two-hop expansion based algorithms, e.g. online_stp, phonebook_prediction,
* recent following sims, recent engagement sims, ...
*
* @tparam Target target type
* @tparam FirstDegree type of first degree nodes
* @tparam SecondaryDegree type of secondary degree nodes
* @tparam Candidate output candidate types
*/
trait TwoHopExpansionCandidateSource[-Target, FirstDegree, SecondaryDegree, +Candidate]
extends CandidateSource[Target, Candidate] {
/**
* fetch first degree nodes given request
*/
def firstDegreeNodes(req: Target): Stitch[Seq[FirstDegree]]
/**
* fetch secondary degree nodes given request and first degree nodes
*/
def secondaryDegreeNodes(req: Target, node: FirstDegree): Stitch[Seq[SecondaryDegree]]
/**
* aggregate and score the candidates to generate final results
*/
def aggregateAndScore(
req: Target,
firstDegreeToSecondDegreeNodesMap: Map[FirstDegree, Seq[SecondaryDegree]]
): Stitch[Seq[Candidate]]
/**
* Generate a list of candidates for the target
*/
def apply(target: Target): Stitch[Seq[Candidate]] = {
for {
firstDegreeNodes <- firstDegreeNodes(target)
secondaryDegreeNodes <- Stitch.traverse(firstDegreeNodes)(secondaryDegreeNodes(target, _))
aggregated <- aggregateAndScore(target, firstDegreeNodes.zip(secondaryDegreeNodes).toMap)
} yield aggregated
}
}