page_content_annotations_common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_OPTIMIZATION_GUIDE_CORE_PAGE_CONTENT_ANNOTATIONS_COMMON_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_PAGE_CONTENT_ANNOTATIONS_COMMON_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/values.h"
  10. #include "components/optimization_guide/core/entity_metadata.h"
  11. #include "components/optimization_guide/core/page_content_annotation_type.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace optimization_guide {
  14. // A weighted ID value.
  15. class WeightedIdentifier {
  16. public:
  17. WeightedIdentifier(int32_t value, double weight);
  18. WeightedIdentifier(const WeightedIdentifier&);
  19. ~WeightedIdentifier();
  20. int32_t value() const { return value_; }
  21. double weight() const { return weight_; }
  22. std::string ToString() const;
  23. base::Value AsValue() const;
  24. bool operator==(const WeightedIdentifier& other) const;
  25. friend std::ostream& operator<<(std::ostream& stream,
  26. const WeightedIdentifier& ws);
  27. private:
  28. int32_t value_;
  29. // In the range of [0.0, 1.0].
  30. double weight_ = 0;
  31. };
  32. // The result of an execution, and all associated data.
  33. class BatchAnnotationResult {
  34. public:
  35. // Creates a result for a page topics annotation.
  36. static BatchAnnotationResult CreatePageTopicsResult(
  37. const std::string& input,
  38. absl::optional<std::vector<WeightedIdentifier>> topics);
  39. // Creates a result for a page entities annotation.
  40. static BatchAnnotationResult CreatePageEntitiesResult(
  41. const std::string& input,
  42. absl::optional<std::vector<ScoredEntityMetadata>> entities);
  43. // Creates a result for a content visibility annotation.
  44. static BatchAnnotationResult CreateContentVisibilityResult(
  45. const std::string& input,
  46. absl::optional<double> visibility_score);
  47. // Creates a result where the AnnotationType and output are not set.
  48. static BatchAnnotationResult CreateEmptyAnnotationsResult(
  49. const std::string& input);
  50. BatchAnnotationResult(const BatchAnnotationResult&);
  51. ~BatchAnnotationResult();
  52. // Returns true if the output corresponding to |type| is not nullopt;
  53. bool HasOutputForType() const;
  54. const std::string& input() const { return input_; }
  55. AnnotationType type() const { return type_; }
  56. const absl::optional<std::vector<WeightedIdentifier>>& topics() const {
  57. return topics_;
  58. }
  59. const absl::optional<std::vector<ScoredEntityMetadata>>& entities() const {
  60. return entities_;
  61. }
  62. absl::optional<double> visibility_score() const { return visibility_score_; }
  63. std::string ToString() const;
  64. std::string ToJSON() const;
  65. base::Value AsValue() const;
  66. bool operator==(const BatchAnnotationResult& other) const;
  67. friend std::ostream& operator<<(std::ostream& stream,
  68. const BatchAnnotationResult& result);
  69. private:
  70. BatchAnnotationResult();
  71. std::string input_;
  72. AnnotationType type_ = AnnotationType::kUnknown;
  73. // Output for page topics annotations, set only if the |type_| matches and the
  74. // execution was successful.
  75. absl::optional<std::vector<WeightedIdentifier>> topics_;
  76. // Output for page entities annotations, set only if the |type_| matches and
  77. // the execution was successful.
  78. absl::optional<std::vector<ScoredEntityMetadata>> entities_;
  79. // Output for visisbility score annotations, set only if the |type_| matches
  80. // and the execution was successful.
  81. absl::optional<double> visibility_score_;
  82. };
  83. using BatchAnnotationCallback =
  84. base::OnceCallback<void(const std::vector<BatchAnnotationResult>&)>;
  85. // Creates a vector of |BatchAnnotationResult| from the given |inputs| where
  86. // each result's status is set to |status|. Useful for creating an Annotation
  87. // response with a single error.
  88. std::vector<BatchAnnotationResult> CreateEmptyBatchAnnotationResults(
  89. const std::vector<std::string>& inputs);
  90. } // namespace optimization_guide
  91. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_PAGE_CONTENT_ANNOTATIONS_COMMON_H_