ranking_cluster_finalizer.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_HISTORY_CLUSTERS_CORE_RANKING_CLUSTER_FINALIZER_H_
  5. #define COMPONENTS_HISTORY_CLUSTERS_CORE_RANKING_CLUSTER_FINALIZER_H_
  6. #include "components/history_clusters/core/cluster_finalizer.h"
  7. #include "components/history_clusters/core/config.h"
  8. #include "components/history_clusters/core/on_device_clustering_features.h"
  9. namespace history_clusters {
  10. // An object that contains the elements that make up a visit's score
  11. // based on a Finch-controllable weighting.
  12. class VisitScores {
  13. public:
  14. VisitScores() = default;
  15. ~VisitScores() = default;
  16. // Calculate the total visit score based on the individual scores and a set of
  17. // weightings between each. Note, this score is not between [0, 1] as
  18. // normalization will consider all visits within a cluster.
  19. float GetTotalScore() const {
  20. return visit_duration_score_ * GetConfig().visit_duration_ranking_weight +
  21. foreground_duration_score_ *
  22. GetConfig().foreground_duration_ranking_weight +
  23. bookmark_score_ * GetConfig().bookmark_ranking_weight +
  24. srp_score_ * GetConfig().search_results_page_ranking_weight +
  25. page_title_score_ * GetConfig().has_page_title_ranking_weight;
  26. }
  27. void set_visit_duration_score(float score) { visit_duration_score_ = score; }
  28. void set_foreground_duration_score(float score) {
  29. foreground_duration_score_ = score;
  30. }
  31. void set_bookmarked() { bookmark_score_ = 1.0; }
  32. void set_is_srp() { srp_score_ = 1.0; }
  33. void set_has_page_title() { page_title_score_ = 1.0; }
  34. private:
  35. // The score for the duration associated with a visit.
  36. float visit_duration_score_ = 0.0;
  37. // The score for the foreground duration.
  38. float foreground_duration_score_ = 0.0;
  39. // The score for whether the visit was bookmarked.
  40. float bookmark_score_ = 0.0;
  41. // The score for whether the visit was on a search results page.
  42. float srp_score_ = 0.0;
  43. // The score for whether the visit had a page title.
  44. float page_title_score_ = 0.0;
  45. };
  46. // A cluster finalizer that scores visits based on visit duration.
  47. class RankingClusterFinalizer : public ClusterFinalizer {
  48. public:
  49. RankingClusterFinalizer();
  50. ~RankingClusterFinalizer() override;
  51. // ClusterFinalizer:
  52. void FinalizeCluster(history::Cluster& cluster) override;
  53. private:
  54. // Calculates the scores for the visits within |cluster| based on
  55. // their total visit duration and updates |url_visit_scores|.
  56. void CalculateVisitDurationScores(
  57. history::Cluster& cluster,
  58. base::flat_map<history::VisitID, VisitScores>& url_visit_scores);
  59. // Calculates the scores for the visits within |cluster| based on
  60. // their binary attributes and updates |url_visit_scores|.
  61. void CalculateVisitAttributeScoring(
  62. history::Cluster& cluster,
  63. base::flat_map<history::VisitID, VisitScores>& url_visit_scores);
  64. // Computes the final scores for each visit based on the current
  65. // individual scores for each visit in |url_visit_scores|.
  66. void ComputeFinalVisitScores(
  67. history::Cluster& cluster,
  68. base::flat_map<history::VisitID, VisitScores>& url_visit_scores);
  69. };
  70. } // namespace history_clusters
  71. #endif // COMPONENTS_HISTORY_CLUSTERS_CORE_RANKING_CLUSTER_FINALIZER_H_