feat(generation): select articles in three passes
This commit is contained in:
@ -78,6 +78,8 @@ type UseCase interface {
|
||||
Generate(context.Context, string) (Result, error)
|
||||
}
|
||||
type Dependencies struct {
|
||||
Decider MentionDecider
|
||||
Names MentionNameResolver
|
||||
Repository Repository
|
||||
SummaryResolver SummaryResolver
|
||||
LayaRanker LayaRanker
|
||||
@ -92,6 +94,9 @@ func NewUseCase(d Dependencies) UseCase {
|
||||
if d.LayaRanker != nil {
|
||||
d.Ranker = d.LayaRanker
|
||||
}
|
||||
if d.Decider == nil {
|
||||
d.Decider, _ = d.Ranker.(MentionDecider)
|
||||
}
|
||||
if d.Clock == nil {
|
||||
d.Clock = time.Now
|
||||
}
|
||||
@ -149,7 +154,7 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
if resolved.Summarizer == nil {
|
||||
return finish("failed", "summary_model_unavailable", "")
|
||||
}
|
||||
if u.Ranker == nil {
|
||||
if u.Decider == nil {
|
||||
return finish("failed", "laya_unavailable", "")
|
||||
}
|
||||
symbols := []string{}
|
||||
@ -183,6 +188,10 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
stat.FailureCode = "source_unavailable"
|
||||
partial = true
|
||||
} else {
|
||||
articles = recentArticles(articles)
|
||||
if len(articles) > stat.ArticleLimit {
|
||||
articles = articles[:stat.ArticleLimit]
|
||||
}
|
||||
for _, article := range articles {
|
||||
stat.Candidates++
|
||||
out.TotalCandidates++
|
||||
@ -206,35 +215,18 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
return finish("no_results", "", "")
|
||||
}
|
||||
ranked, err := u.Ranker.Rank(ctx, input.RankingMode, input.Assets, candidates)
|
||||
selected, layaAccepted, err := u.selectThreePass(ctx, input, candidates)
|
||||
if err != nil {
|
||||
return finish("failed", "ranking_failed", "")
|
||||
return finish("failed", err.Error(), "")
|
||||
}
|
||||
originals := map[string]Candidate{}
|
||||
for _, c := range candidates {
|
||||
originals[c.ID] = c
|
||||
for source, n := range layaAccepted {
|
||||
out.SourceStats[stats[source]].LayaSelected = n
|
||||
out.LayaSelected += n
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
selected := []Candidate{}
|
||||
for _, c := range ranked {
|
||||
original, ok := originals[c.ID]
|
||||
if !ok || seen[c.ID] {
|
||||
return finish("failed", "ranking_invalid", "")
|
||||
}
|
||||
seen[c.ID] = true
|
||||
i := stats[original.SourceID]
|
||||
out.SourceStats[i].LayaSelected++
|
||||
out.LayaSelected++
|
||||
}
|
||||
for _, c := range ranked {
|
||||
c = originals[c.ID]
|
||||
i := stats[c.SourceID]
|
||||
if out.SourceStats[i].Selected >= out.SourceStats[i].ArticleLimit || len(selected) >= input.GlobalLimit {
|
||||
continue
|
||||
}
|
||||
selected = append(selected, c)
|
||||
out.SourceStats[i].Selected++
|
||||
for _, c := range selected {
|
||||
out.SourceStats[stats[c.SourceID]].Selected++
|
||||
}
|
||||
|
||||
out.Selected = len(selected)
|
||||
if len(selected) == 0 {
|
||||
return finish("no_results", "", "")
|
||||
|
||||
Reference in New Issue
Block a user