File: quick_answers_model.h

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 (449 lines) | stat: -rw-r--r-- 14,858 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_
#define CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_

#include <compare>
#include <string>
#include <vector>

#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/quick_answers/public/cpp/constants.h"
#include "chromeos/components/quick_answers/utils/unit_conversion_constants.h"
#include "ui/color/color_id.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

namespace quick_answers {

// Interaction with the consent-view (used for logging).
enum class NoticeInteractionType {
  // When user clicks on the "grant-consent" button.
  kAccept = 0,
  // When user clicks on the "manage-settings" button.
  kManageSettings = 1,
  // When user otherwise dismisses or ignores the consent-view.
  kDismiss = 2
};

// The status of loading quick answers.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// Note: Enums labels are at |QuickAnswersLoadStatus|.
enum class LoadStatus {
  kSuccess = 0,
  kNetworkError = 1,
  kNoResult = 2,
  kMaxValue = kNoResult,
};

// The type of the result. Valid values are map to the search result types.
// Please see go/1ns-doc for more detail.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// Note: Enums labels are at |QuickAnswersResultType|.
enum class ResultType {
  kNoResult = 0,
  kDefinitionResult = 5493,
  kTranslationResult = 6613,
  kUnitConversionResult = 13668,
};

// The predicted intent of the request.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// Note: Enums labels are at |QuickAnswersIntentType|.
enum class IntentType {
  kUnknown = 0,
  kUnit = 1,
  kDictionary = 2,
  kTranslation = 3,
  kMaxValue = kTranslation
};

std::optional<quick_answers::Intent> ToIntent(IntentType intent_type);

enum class QuickAnswerUiElementType {
  kUnknown = 0,
  kText = 1,
  kImage = 2,
};

// Enumeration of Quick Answers exit points. These values are persisted to logs.
// Entries should never be renumbered and numeric values should never be reused.
// Append to this enum is allowed only if the possible exit point grows.
enum class QuickAnswersExitPoint {
  // The exit point is unspecified. Might be used by tests, obsolete code or as
  // placeholders.
  kUnspecified = 0,
  kContextMenuDismiss = 1,
  kContextMenuClick = 2,
  kQuickAnswersClick = 3,
  kSettingsButtonClick = 4,
  kReportQueryButtonClick = 5,
  kMaxValue = kReportQueryButtonClick,
};

struct QuickAnswerUiElement {
  explicit QuickAnswerUiElement(QuickAnswerUiElementType type) : type(type) {}
  QuickAnswerUiElement(const QuickAnswerUiElement&) = default;
  QuickAnswerUiElement& operator=(const QuickAnswerUiElement&) = default;
  QuickAnswerUiElement(QuickAnswerUiElement&&) = default;
  virtual ~QuickAnswerUiElement() = default;

  QuickAnswerUiElementType type = QuickAnswerUiElementType::kUnknown;
};

// Class to describe an answer text.
struct QuickAnswerText : public QuickAnswerUiElement {
  explicit QuickAnswerText(const std::string& text,
                           ui::ColorId color_id = ui::kColorLabelForeground)
      : QuickAnswerUiElement(QuickAnswerUiElementType::kText),
        text(base::UTF8ToUTF16(text)),
        color_id(color_id) {}

  std::u16string text;

  // Attributes for text style.
  ui::ColorId color_id;
};

struct QuickAnswerResultText : public QuickAnswerText {
 public:
  QuickAnswerResultText(
      const std::string& text,
      ui::ColorId color_id = ui::kColorLabelForegroundSecondary)
      : QuickAnswerText(text, color_id) {}
};

struct QuickAnswerImage : public QuickAnswerUiElement {
  explicit QuickAnswerImage(const gfx::Image& image)
      : QuickAnswerUiElement(QuickAnswerUiElementType::kImage), image(image) {}

  gfx::Image image;
};

// Class to describe quick answers phonetics info.
struct PhoneticsInfo {
 public:
  PhoneticsInfo();
  PhoneticsInfo(const PhoneticsInfo&);
  ~PhoneticsInfo();

  // Pronunciation of a word, i.e. in phonetic symbols.
  std::string text;

  // Phonetics audio URL for playing pronunciation of dictionary results.
  // For other type of results the URL will be empty.
  GURL phonetics_audio;

  // Query text and locale which will be used for tts if enabled and
  // there is no phonetics audio available.
  std::string query_text;
  std::string locale;

  bool PhoneticsInfoAvailable() const;
  bool AudioUrlAvailable() const;
  bool TtsAudioAvailable() const;
};

// Structure to describe a quick answer.
struct QuickAnswer {
  QuickAnswer();
  ~QuickAnswer();

  ResultType result_type = ResultType::kNoResult;
  std::vector<std::unique_ptr<QuickAnswerUiElement>> title;
  std::vector<std::unique_ptr<QuickAnswerUiElement>> first_answer_row;
  std::unique_ptr<QuickAnswerImage> image;

  PhoneticsInfo phonetics_info;
};

// Information of the device that used by the user to send the request.
struct DeviceProperties {
  // Whether the request is send by an internal user.
  bool is_internal = false;
};

struct IntentInfo {
  IntentInfo();
  IntentInfo(const IntentInfo& other);
  IntentInfo(const std::string& intent_text,
             IntentType intent_type,
             std::string_view device_language = std::string_view(),
             std::string_view source_language = std::string_view());
  ~IntentInfo();

  // The text extracted from the selected_text associated with the intent.
  std::string intent_text;

  // Predicted intent.
  IntentType intent_type = IntentType::kUnknown;

  // Device language code.
  std::string device_language;

  // Source language for definition or translation query, should only be used
  // for definition or translation intents.
  std::string source_language;
};

// Extract information generated from |QuickAnswersRequest|.
struct PreprocessedOutput {
  PreprocessedOutput();
  PreprocessedOutput(const PreprocessedOutput& other);
  ~PreprocessedOutput();

  IntentInfo intent_info;

  // Rewritten query based on |intent_type| and |intent_text|.
  std::string query;
};

// Structure of quick answers request context, including device properties and
// surrounding text.
struct Context {
  // Device specific properties.
  DeviceProperties device_properties;

  std::string surrounding_text;
};

// Structure to describe an quick answer request including selected content and
// context.
struct QuickAnswersRequest {
  QuickAnswersRequest();
  QuickAnswersRequest(const QuickAnswersRequest& other);
  ~QuickAnswersRequest();

  // The selected text.
  std::string selected_text;

  // Output of processed result.
  PreprocessedOutput preprocessed_output;

  // Context information.
  Context context;

  // TODO(b/169346016): Add context and other targeted objects (e.g: images,
  // links, etc).
};

// `Sense` must be copyable as a member of `DefinitionResult`.
struct Sense {
 public:
  Sense();
  Sense(const Sense& other);
  Sense& operator=(const Sense& other);
  ~Sense();

  std::string definition;
  // Not every word sense will have a sample sentence or synonyms.
  std::optional<std::string> sample_sentence;
  std::optional<std::vector<std::string>> synonyms_list;
};

// `DefinitionResult` holds result for definition intent.
// `DefinitionResult` must be copyable.
struct DefinitionResult {
 public:
  DefinitionResult();
  DefinitionResult(const DefinitionResult& other);
  DefinitionResult& operator=(const DefinitionResult& other);
  ~DefinitionResult();

  std::string word;
  std::string word_class;
  PhoneticsInfo phonetics_info;
  Sense sense;
  // Not every word will have subsenses.
  std::optional<std::vector<Sense>> subsenses_list;
};

// `TranslationResult` holds result for translation intent.
// `TranslationResult` must be copyable.
struct TranslationResult {
 public:
  TranslationResult();
  TranslationResult(const TranslationResult& other);
  TranslationResult& operator=(const TranslationResult& other);
  ~TranslationResult();

  std::string text_to_translate;
  std::string translated_text;
  std::string source_locale;
  std::string target_locale;
};

// A unit conversion rule between a unit and the standard SI unit
// of the same category.
// `ConversionRule` must be copyable as a member of `UnitConversion`.
class ConversionRule {
 public:
  ConversionRule(const ConversionRule& other);
  ConversionRule& operator=(const ConversionRule& other);
  ~ConversionRule();

  // Build a `ConversionRule` – returns nullopt if invalid conversion variables.
  static std::optional<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);

  double ConvertAmountToSi(double unit_amount) const;
  double ConvertAmountFromSi(double si_amount) const;
  bool IsSingleVariableLinearConversion() const;

  double term_a() const { return term_a_; }
  const std::string& category() const { return category_; }
  const std::string& unit_name() const { return unit_name_; }

  friend bool operator==(const ConversionRule&,
                         const ConversionRule&) = default;

 private:
  ConversionRule(const std::string& category,
                 const std::string& unit_name,
                 double term_a,
                 double term_b,
                 double term_c);

  std::string category_;
  std::string unit_name_;
  // Conversion formulas are in one of the two formula formats:
  // 1. 'target = a * source + b'
  // 2. 'target = c / source'
  // where a corresponds with |term_a_|, b corresponds with |term_b_|, and c
  // corresponds with |term_c_|.
  // If |term_a_| is a valid value then |term_c_| must be an invalid value, and
  // vice versa.
  //
  // If |term_a_| is not |kInvalidRateTermValue|, then the conversion formula is
  // 'target = a * source + b'
  double term_a_;
  double term_b_;
  // If |term_c_| is not |kInvalidRateTermValue|, then the conversion formula is
  // 'target = c / source'
  double term_c_;
};

// A unit conversion between two units in the same category.
// `UnitConversion` must be copyable as a member of `UnitConversionResult`.
class UnitConversion {
 public:
  UnitConversion(const UnitConversion& other);
  UnitConversion& operator=(const UnitConversion& other);
  ~UnitConversion();

  // Build a `UnitConversion` – returns nullopt if different unit categories.
  static std::optional<UnitConversion> Create(const ConversionRule& source_rule,
                                              const ConversionRule& dest_rule);

  // Used for sorting alternative unit conversions. This must be at least a weak
  // ordering.
  //
  // We have no direct way of comparing unit conversions with different
  // formulas. The best approximation is to limit comparisons to linear
  // formulas where we only consider the |term_a_| values. A
  // smaller a1/a2 ratio between the |source_rule_| and |dest_rule_| of a
  // `UnitConversion` is understood as a "smaller" unit conversion.
  //
  // Unit conversions involving non-linear formulas will be considered greater
  // by default for our purposes.
  friend std::weak_ordering operator<=>(const UnitConversion& a,
                                        const UnitConversion& b);

  friend bool operator==(const UnitConversion&,
                         const UnitConversion&) = default;

  // Given a |source_amount| in the source unit, returns the equivalent amount
  // in the destination unit.
  double ConvertSourceAmountToDestAmount(double source_amount) const;

  // Function to build the formula description text shown on the Quick Answers
  // card. Return nullopt if no simple formula can be derived.
  std::optional<std::string> GetConversionFormulaText() const;

  const std::string& category() const { return source_rule_.category(); }
  const ConversionRule& source_rule() const { return source_rule_; }
  const ConversionRule& dest_rule() const { return dest_rule_; }

 private:
  UnitConversion(const ConversionRule& source_rule,
                 const ConversionRule& dest_rule);

  static double MaybeGetRatio(double value1, double value2);

  ConversionRule source_rule_;
  ConversionRule dest_rule_;
};

// `UnitConversionResult` holds result for unit conversion intent.
// `UnitConversionResult` must be copyable.
struct UnitConversionResult {
 public:
  UnitConversionResult();
  UnitConversionResult(const UnitConversionResult& other);
  UnitConversionResult& operator=(const UnitConversionResult& other);
  ~UnitConversionResult();

  std::string source_text;
  std::string result_text;
  std::string category;
  double source_amount = 0;
  // Not every unit conversion result will have valid unit conversions.
  std::optional<UnitConversion> source_to_dest_unit_conversion;
  std::vector<UnitConversion> alternative_unit_conversions_list;
};

// `StructuredResult` is NOT copyable as it's not trivial to make a class with
// unique_ptr to copyable.
class StructuredResult {
 public:
  StructuredResult();
  ~StructuredResult();
  StructuredResult(const StructuredResult&) = delete;
  StructuredResult& operator=(const StructuredResult) = delete;

  ResultType GetResultType() const;

  // Result type specific structs must be copyable as they can be copied to
  // views.
  std::unique_ptr<TranslationResult> translation_result;
  std::unique_ptr<DefinitionResult> definition_result;
  std::unique_ptr<UnitConversionResult> unit_conversion_result;
};

// `QuickAnswersSession` holds states related to a single Quick Answer session.
//
// This class currently holds results in `QuickAnswer` and `StructuredResult`.
// `QuickAnswer` field is used by `QuickAnswersView`. Rich Answers will read
// `StructuredResult`. Note that `QuickAnswer` is populated by using information
// in `StructuredResult`, i.e. `StructuredResult` is a super-set of
// `QuickAnswer`.
//
// Longer term plan is to migrate other states to this class, e.g. intent.
//
// `QuickAnswersSession` is NOT copyable as it's not trivial to make a class
// with unique_ptr to copyable.
class QuickAnswersSession {
 public:
  QuickAnswersSession();
  ~QuickAnswersSession();
  QuickAnswersSession(const QuickAnswersSession&) = delete;
  QuickAnswersSession& operator=(const QuickAnswersSession) = delete;

  // TODO(b/278929409): Once we migrate all result types to `StructuredResult`,
  // populate `QuickAnswer` outside of ResultParsers.
  std::unique_ptr<QuickAnswer> quick_answer;
  std::unique_ptr<StructuredResult> structured_result;
};

}  // namespace quick_answers

#endif  // CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_