123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "components/optimization_guide/core/page_content_annotations_common.h"
- #include <algorithm>
- #include <ostream>
- #include "base/check_op.h"
- #include "base/json/json_writer.h"
- #include "base/strings/strcat.h"
- #include "base/strings/string_number_conversions.h"
- #include "base/strings/string_util.h"
- #include "base/strings/stringprintf.h"
- namespace optimization_guide {
- WeightedIdentifier::WeightedIdentifier(int32_t value, double weight)
- : value_(value), weight_(weight) {
- DCHECK_GE(weight_, 0.0);
- DCHECK_LE(weight_, 1.0);
- }
- WeightedIdentifier::WeightedIdentifier(const WeightedIdentifier&) = default;
- WeightedIdentifier::~WeightedIdentifier() = default;
- bool WeightedIdentifier::operator==(const WeightedIdentifier& other) const {
- constexpr double kWeightTolerance = 1e-6;
- return this->value_ == other.value_ &&
- std::abs(this->weight_ - other.weight_) <= kWeightTolerance;
- }
- std::string WeightedIdentifier::ToString() const {
- return base::StringPrintf("WeightedIdentifier{%d,%f}", value(), weight());
- }
- base::Value WeightedIdentifier::AsValue() const {
- base::Value::Dict wi;
- wi.Set("value", value());
- wi.Set("weight", weight());
- return base::Value(std::move(wi));
- }
- std::ostream& operator<<(std::ostream& stream, const WeightedIdentifier& ws) {
- stream << ws.ToString();
- return stream;
- }
- BatchAnnotationResult::BatchAnnotationResult() = default;
- BatchAnnotationResult::BatchAnnotationResult(const BatchAnnotationResult&) =
- default;
- BatchAnnotationResult::~BatchAnnotationResult() = default;
- bool BatchAnnotationResult::HasOutputForType() const {
- switch (type()) {
- case AnnotationType::kUnknown:
- return false;
- case AnnotationType::kPageTopics:
- return !!topics();
- case AnnotationType::kContentVisibility:
- return !!visibility_score();
- case AnnotationType::kPageEntities:
- return !!entities();
- }
- }
- base::Value BatchAnnotationResult::AsValue() const {
- base::Value::Dict result;
- result.Set("input", input());
- result.Set("type", AnnotationTypeToString(type()));
- if (topics()) {
- base::Value::List list;
- for (const auto& wi : *topics()) {
- list.Append(wi.AsValue());
- }
- result.Set("topics", std::move(list));
- }
- if (entities()) {
- base::Value::List list;
- for (const auto& md : *entities()) {
- list.Append(md.AsValue());
- }
- result.Set("entities", std::move(list));
- }
- if (visibility_score()) {
- result.Set("visibility_score", *visibility_score());
- }
- return base::Value(std::move(result));
- }
- std::string BatchAnnotationResult::ToJSON() const {
- std::string json;
- if (base::JSONWriter::Write(AsValue(), &json)) {
- return json;
- }
- return std::string();
- }
- std::string BatchAnnotationResult::ToString() const {
- std::string output = "nullopt";
- if (topics_) {
- std::vector<std::string> all_weighted_ids;
- for (const WeightedIdentifier& wi : *topics_) {
- all_weighted_ids.push_back(wi.ToString());
- }
- output = "{" + base::JoinString(all_weighted_ids, ",") + "}";
- } else if (entities_) {
- std::vector<std::string> all_entities;
- for (const ScoredEntityMetadata& md : *entities_) {
- all_entities.push_back(md.ToString());
- }
- output = "{" + base::JoinString(all_entities, ",") + "}";
- } else if (visibility_score_) {
- output = base::NumberToString(*visibility_score_);
- }
- return base::StringPrintf(
- "BatchAnnotationResult{"
- "\"%s\", "
- "type: %s, "
- "output: %s}",
- input_.c_str(), AnnotationTypeToString(type_).c_str(), output.c_str());
- }
- std::ostream& operator<<(std::ostream& stream,
- const BatchAnnotationResult& result) {
- stream << result.ToString();
- return stream;
- }
- // static
- BatchAnnotationResult BatchAnnotationResult::CreatePageTopicsResult(
- const std::string& input,
- absl::optional<std::vector<WeightedIdentifier>> topics) {
- BatchAnnotationResult result;
- result.input_ = input;
- result.topics_ = topics;
- result.type_ = AnnotationType::kPageTopics;
- // Always sort the result (if present) by the given score.
- if (result.topics_) {
- std::sort(result.topics_->begin(), result.topics_->end(),
- [](const WeightedIdentifier& a, const WeightedIdentifier& b) {
- return a.weight() < b.weight();
- });
- }
- return result;
- }
- // static
- BatchAnnotationResult BatchAnnotationResult::CreatePageEntitiesResult(
- const std::string& input,
- absl::optional<std::vector<ScoredEntityMetadata>> entities) {
- BatchAnnotationResult result;
- result.input_ = input;
- result.entities_ = entities;
- result.type_ = AnnotationType::kPageEntities;
- // Always sort the result (if present) by the given score.
- if (result.entities_) {
- std::sort(result.entities_->begin(), result.entities_->end(),
- [](const ScoredEntityMetadata& a, const ScoredEntityMetadata& b) {
- return a.score < b.score;
- });
- }
- return result;
- }
- // static
- BatchAnnotationResult BatchAnnotationResult::CreateContentVisibilityResult(
- const std::string& input,
- absl::optional<double> visibility_score) {
- BatchAnnotationResult result;
- result.input_ = input;
- result.visibility_score_ = visibility_score;
- result.type_ = AnnotationType::kContentVisibility;
- return result;
- }
- // static
- BatchAnnotationResult BatchAnnotationResult::CreateEmptyAnnotationsResult(
- const std::string& input) {
- BatchAnnotationResult result;
- result.input_ = input;
- return result;
- }
- bool BatchAnnotationResult::operator==(
- const BatchAnnotationResult& other) const {
- return this->input_ == other.input_ && this->type_ == other.type_ &&
- this->topics_ == other.topics_ && this->entities_ == other.entities_ &&
- this->visibility_score_ == other.visibility_score_;
- }
- std::vector<BatchAnnotationResult> CreateEmptyBatchAnnotationResults(
- const std::vector<std::string>& inputs) {
- std::vector<BatchAnnotationResult> results;
- results.reserve(inputs.size());
- for (const std::string& input : inputs) {
- results.emplace_back(
- BatchAnnotationResult::CreateEmptyAnnotationsResult(input));
- }
- return results;
- }
- } // namespace optimization_guide
|