mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-06-02 01:08:47 +02:00
ef4c5eb65e
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.
64 lines
2.3 KiB
Scala
64 lines
2.3 KiB
Scala
package com.twitter.product_mixer.component_library.filter
|
|
|
|
import com.twitter.product_mixer.core.feature.Feature
|
|
import com.twitter.product_mixer.core.functional_component.filter.Filter
|
|
import com.twitter.product_mixer.core.functional_component.filter.FilterResult
|
|
import com.twitter.product_mixer.core.model.common.CandidateWithFeatures
|
|
import com.twitter.product_mixer.core.model.common.UniversalNoun
|
|
import com.twitter.product_mixer.core.model.common.identifier.FilterIdentifier
|
|
import com.twitter.product_mixer.core.pipeline.PipelineQuery
|
|
import com.twitter.stitch.Stitch
|
|
|
|
object FeatureFilter {
|
|
|
|
/**
|
|
* Builds a Filter using the Feature name as the FilterIdentifier
|
|
*
|
|
* @see [[FeatureFilter.fromFeature(identifier, feature)]]
|
|
*/
|
|
def fromFeature[Candidate <: UniversalNoun[Any]](
|
|
feature: Feature[Candidate, Boolean]
|
|
): Filter[PipelineQuery, Candidate] =
|
|
FeatureFilter.fromFeature(FilterIdentifier(feature.toString), feature)
|
|
|
|
/**
|
|
* Builds a Filter that keeps candidates when the provided Boolean Feature is present and True.
|
|
* If the Feature is missing or False, the candidate is removed.
|
|
*
|
|
* {{{
|
|
* Filter.fromFeature(
|
|
* FilterIdentifier("SomeFilter"),
|
|
* feature = SomeFeature
|
|
* )
|
|
* }}}
|
|
*
|
|
* @param identifier A FilterIdentifier for the new filter
|
|
* @param feature A feature of [Candidate, Boolean] type used to determine whether Candidates will be kept
|
|
* when this feature is present and true otherwise they will be removed.
|
|
*/
|
|
def fromFeature[Candidate <: UniversalNoun[Any]](
|
|
identifier: FilterIdentifier,
|
|
feature: Feature[Candidate, Boolean]
|
|
): Filter[PipelineQuery, Candidate] = {
|
|
val i = identifier
|
|
|
|
new Filter[PipelineQuery, Candidate] {
|
|
override val identifier: FilterIdentifier = i
|
|
|
|
override def apply(
|
|
query: PipelineQuery,
|
|
candidates: Seq[CandidateWithFeatures[Candidate]]
|
|
): Stitch[FilterResult[Candidate]] = {
|
|
val (keptCandidates, removedCandidates) = candidates.partition { filterCandidate =>
|
|
filterCandidate.features.getOrElse(feature, false)
|
|
}
|
|
|
|
Stitch.value(
|
|
FilterResult(
|
|
kept = keptCandidates.map(_.candidate),
|
|
removed = removedCandidates.map(_.candidate)))
|
|
}
|
|
}
|
|
}
|
|
}
|