49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package generation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
// These DTOs deliberately cannot carry article bodies, URLs, dates or credentials.
|
|
type ScoringCompany struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
type ScoringTitle struct {
|
|
Index int `json:"index"`
|
|
Title string `json:"title"`
|
|
}
|
|
type TitleScoreRequest struct {
|
|
Watchlist []ScoringCompany `json:"watchlist"`
|
|
Titles []ScoringTitle `json:"titles"`
|
|
}
|
|
type TitleScorer interface {
|
|
Score(context.Context, TitleScoreRequest) ([]float64, error)
|
|
}
|
|
|
|
var ErrRelevanceResponseInvalid = errors.New("relevance_response_invalid")
|
|
var ErrRelevanceScoringFailed = errors.New("relevance_scoring_failed")
|
|
|
|
func validOrderDate(d *time.Time) bool {
|
|
return d != nil && !d.IsZero() && d.Year() >= 1 && d.Year() <= 9999
|
|
}
|
|
func sortByRelevance(c []Candidate) {
|
|
sort.SliceStable(c, func(i, j int) bool {
|
|
if c[i].RelevanceScore != c[j].RelevanceScore {
|
|
return c[i].RelevanceScore > c[j].RelevanceScore
|
|
}
|
|
a, b := orderArticle(c[i]).PublishedAt, orderArticle(c[j]).PublishedAt
|
|
av, bv := validOrderDate(a), validOrderDate(b)
|
|
if av != bv {
|
|
return av
|
|
}
|
|
if !av {
|
|
return false
|
|
}
|
|
return a.After(*b)
|
|
})
|
|
}
|