File: quick_answers_model.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 10,972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/components/quick_answers/quick_answers_model.h"

#include <cmath>
#include <compare>

#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/quick_answers/utils/quick_answers_utils.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

int GetFormulaMessageId(bool is_multiply, bool is_approximate) {
  if (is_multiply) {
    if (is_approximate) {
      return IDS_RICH_ANSWERS_VIEW_UNIT_CONVERSION_APPROXIMATE_MULTIPLICATION_FORMULA_TEXT;
    }

    return IDS_RICH_ANSWERS_VIEW_UNIT_CONVERSION_EXACT_MULTIPLICATION_FORMULA_TEXT;
  }

  if (is_approximate) {
    return IDS_RICH_ANSWERS_VIEW_UNIT_CONVERSION_APPROXIMATE_DIVISION_FORMULA_TEXT;
  }

  return IDS_RICH_ANSWERS_VIEW_UNIT_CONVERSION_EXACT_DIVISION_FORMULA_TEXT;
}

}  // namespace

namespace quick_answers {

std::optional<quick_answers::Intent> ToIntent(IntentType intent_type) {
  switch (intent_type) {
    case IntentType::kDictionary:
      return quick_answers::Intent::kDefinition;
    case IntentType::kTranslation:
      return quick_answers::Intent::kTranslation;
    case IntentType::kUnit:
      return quick_answers::Intent::kUnitConversion;
    case IntentType::kUnknown:
      return std::nullopt;
  }

  NOTREACHED() << "Invalid intent type enum value provided";
}

PhoneticsInfo::PhoneticsInfo() = default;
PhoneticsInfo::PhoneticsInfo(const PhoneticsInfo&) = default;
PhoneticsInfo::~PhoneticsInfo() = default;

bool PhoneticsInfo::PhoneticsInfoAvailable() const {
  return AudioUrlAvailable() || TtsAudioAvailable();
}

bool PhoneticsInfo::AudioUrlAvailable() const {
  return !phonetics_audio.is_empty();
}

bool PhoneticsInfo::TtsAudioAvailable() const {
  return !query_text.empty() && !locale.empty();
}

QuickAnswer::QuickAnswer() = default;
QuickAnswer::~QuickAnswer() = default;

IntentInfo::IntentInfo() = default;
IntentInfo::IntentInfo(const IntentInfo& other) = default;
IntentInfo::IntentInfo(const std::string& intent_text,
                       IntentType intent_type,
                       std::string_view device_language,
                       std::string_view source_language) {
  this->intent_text = intent_text;
  this->intent_type = intent_type;
  this->device_language = std::string(device_language);
  this->source_language = std::string(source_language);
}
IntentInfo::~IntentInfo() = default;

PreprocessedOutput::PreprocessedOutput() = default;
PreprocessedOutput::PreprocessedOutput(const PreprocessedOutput& other) =
    default;
PreprocessedOutput::~PreprocessedOutput() = default;

QuickAnswersRequest::QuickAnswersRequest() = default;
QuickAnswersRequest::QuickAnswersRequest(const QuickAnswersRequest& other) =
    default;
QuickAnswersRequest::~QuickAnswersRequest() = default;

Sense::Sense() = default;
Sense::Sense(const Sense& other) = default;
Sense& Sense::Sense::operator=(const Sense& other) = default;
Sense::~Sense() = default;

DefinitionResult::DefinitionResult() = default;
DefinitionResult::DefinitionResult(const DefinitionResult& other) = default;
DefinitionResult& DefinitionResult::DefinitionResult::operator=(
    const DefinitionResult& other) = default;
DefinitionResult::~DefinitionResult() = default;

TranslationResult::TranslationResult() = default;
TranslationResult::TranslationResult(const TranslationResult& other) = default;
TranslationResult& TranslationResult::TranslationResult::operator=(
    const TranslationResult& other) = default;
TranslationResult::~TranslationResult() = default;

ConversionRule::ConversionRule(const std::string& category,
                               const std::string& unit_name,
                               double term_a,
                               double term_b,
                               double term_c)
    : category_(category),
      unit_name_(unit_name),
      term_a_(term_a),
      term_b_(term_b),
      term_c_(term_c) {}
ConversionRule::ConversionRule(const ConversionRule& other) = default;
ConversionRule& ConversionRule::ConversionRule::operator=(
    const ConversionRule& other) = default;
ConversionRule::~ConversionRule() = default;
std::optional<ConversionRule> ConversionRule::Create(
    const std::string& category,
    const std::string& unit_name,
    const std::optional<double>& term_a,
    const std::optional<double>& term_b,
    const std::optional<double>& term_c) {
  if (category.empty() || unit_name.empty()) {
    return std::nullopt;
  }

  // If neither |term_a| nor |term_c| is valid, there is no valid conversion
  // rule.
  if ((!term_a || term_a.value() == kInvalidRateTermValue) &&
      (!term_c || term_c.value() == kInvalidRateTermValue)) {
    return std::nullopt;
  }

  // Neither |term_a| nor |term_c| should be negative. Return nullopt for this
  // unexpected case.
  if ((term_a && term_a.value() < 0) || (term_c && term_c.value() < 0)) {
    return std::nullopt;
  }

  double term_a_value = term_a.value_or(kInvalidRateTermValue);
  double term_b_value = term_b.value_or(kInvalidRateTermValue);
  double term_c_value = term_c.value_or(kInvalidRateTermValue);
  return ConversionRule(category, unit_name, term_a_value, term_b_value,
                        term_c_value);
}
double ConversionRule::ConvertAmountToSi(double unit_amount) const {
  return (term_a_ != kInvalidRateTermValue) ? (term_a_ * unit_amount + term_b_)
                                            : (term_c_ / unit_amount);
}
double ConversionRule::ConvertAmountFromSi(double si_amount) const {
  return (term_a_ != kInvalidRateTermValue) ? ((si_amount - term_b_) / term_a_)
                                            : (term_c_ / si_amount);
}
bool ConversionRule::IsSingleVariableLinearConversion() const {
  return (term_a_ != kInvalidRateTermValue) &&
         (term_b_ == kInvalidRateTermValue) &&
         (term_c_ == kInvalidRateTermValue);
}

UnitConversion::UnitConversion(const ConversionRule& source_rule,
                               const ConversionRule& dest_rule)
    : source_rule_(source_rule), dest_rule_(dest_rule) {}
UnitConversion::UnitConversion(const UnitConversion& other) = default;
UnitConversion& UnitConversion::UnitConversion::operator=(
    const UnitConversion& other) = default;
UnitConversion::~UnitConversion() = default;
std::optional<UnitConversion> UnitConversion::Create(
    const ConversionRule& source_rule,
    const ConversionRule& dest_rule) {
  if (source_rule.category() != dest_rule.category()) {
    return std::nullopt;
  }

  return UnitConversion(source_rule, dest_rule);
}
double UnitConversion::ConvertSourceAmountToDestAmount(
    double source_amount) const {
  return dest_rule_.ConvertAmountFromSi(
      source_rule_.ConvertAmountToSi(source_amount));
}
std::optional<std::string> UnitConversion::GetConversionFormulaText() const {
  // We only return formula description texts for conversions between two units
  // whose `ConversionRule` only involves |term_a_| values i.e. formula form is:
  // a * source = target.
  if (!source_rule_.IsSingleVariableLinearConversion() ||
      !dest_rule_.IsSingleVariableLinearConversion()) {
    return std::nullopt;
  }

  // Don't return a formula description text if the conversion rate is
  // exactly 1.
  if (source_rule_.term_a() == dest_rule_.term_a()) {
    return std::nullopt;
  }

  // Get the greater ratio (i.e. >= 1) of the two linear |term_a_| values.
  // The conversion formula will be in the form: source * (a1/a2) = target
  // The actual ratio will determine whether the conversion operator used for
  // the formula description is multiplication (when the source unit |term_a|
  // value is the numerator) or division (when the target unit |term_a| value
  // is the numerator).
  double conversion_term_a =
      MaybeGetRatio(source_rule_.term_a(), dest_rule_.term_a());

  if (conversion_term_a == kInvalidRateTermValue) {
    return std::nullopt;
  }

  // Check if the conversion term is a decimal number. If it is, an
  // approximation qualifier (i.e. "for an approximate result, ...") will be
  // appended at the beginning of the formula text.
  double int_part = 0.0;
  bool is_approximate_formula = std::modf(conversion_term_a, &int_part) != 0.0;

  bool is_multiply_formula = source_rule_.term_a() > dest_rule_.term_a();
  int formula_message_id =
      GetFormulaMessageId(is_multiply_formula, is_approximate_formula);

  return l10n_util::GetStringFUTF8(
      formula_message_id,
      base::UTF8ToUTF16(base::ToLowerASCII(source_rule_.category())),
      base::UTF8ToUTF16(BuildRoundedUnitAmountDisplayText(conversion_term_a)));
}

// static
double UnitConversion::MaybeGetRatio(double value1, double value2) {
  if (value1 == quick_answers::kInvalidRateTermValue ||
      value2 == quick_answers::kInvalidRateTermValue) {
    return quick_answers::kInvalidRateTermValue;
  }

  return std::max(value1, value2) / std::min(value1, value2);
}

std::weak_ordering operator<=>(const UnitConversion& a,
                               const UnitConversion& b) {
  double a_linear_term_ratio = UnitConversion::MaybeGetRatio(
      a.source_rule_.term_a(), a.dest_rule_.term_a());
  CHECK(std::isfinite(a_linear_term_ratio));
  if (a_linear_term_ratio == kInvalidRateTermValue) {
    return std::weak_ordering::greater;
  }

  double b_linear_term_ratio = UnitConversion::MaybeGetRatio(
      b.source_rule_.term_a(), b.dest_rule_.term_a());
  CHECK(std::isfinite(b_linear_term_ratio));
  if (b_linear_term_ratio == kInvalidRateTermValue ||
      a_linear_term_ratio < b_linear_term_ratio) {
    return std::weak_ordering::less;
  }

  // Cannot use `<=>` on the doubles directly, since that returns a partial
  // ordering, not a weak ordering.
  return a_linear_term_ratio == b_linear_term_ratio
             ? std::weak_ordering::equivalent
             : std::weak_ordering::greater;
}

UnitConversionResult::UnitConversionResult() = default;
UnitConversionResult::UnitConversionResult(const UnitConversionResult& other) =
    default;
UnitConversionResult& UnitConversionResult::UnitConversionResult::operator=(
    const UnitConversionResult& other) = default;
UnitConversionResult::~UnitConversionResult() = default;

StructuredResult::StructuredResult() = default;
StructuredResult::~StructuredResult() = default;
ResultType StructuredResult::GetResultType() const {
  if (translation_result) {
    return ResultType::kTranslationResult;
  }
  if (definition_result) {
    return ResultType::kDefinitionResult;
  }
  if (unit_conversion_result) {
    return ResultType::kUnitConversionResult;
  }
  return ResultType::kNoResult;
}

QuickAnswersSession::QuickAnswersSession() = default;
QuickAnswersSession::~QuickAnswersSession() = default;

}  // namespace quick_answers