File: quick_answers_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • 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 (137 lines) | stat: -rw-r--r-- 5,303 bytes parent folder | download | duplicates (5)
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
// 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/utils/quick_answers_utils.h"

#include <string>

#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/quick_answers/utils/unit_conversion_constants.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "ui/base/l10n/l10n_util.h"

namespace quick_answers {

namespace {

constexpr char kUnitConversionQueryRewriteTemplate[] = "Convert:%s";
constexpr char kDictionaryQueryRewriteTemplate[] = "%s %s";
constexpr char kTranslationQueryRewriteTemplate[] = "Translate:%s";

constexpr char kDictionaryQueryKeyword[] = "Define";
constexpr char kDictionaryQueryKeywordES[] = "Definir";
constexpr char kDictionaryQueryKeywordIT[] = "Definire";
constexpr char kDictionaryQueryKeywordFR[] = "Définir";
constexpr char kDictionaryQueryKeywordPT[] = "Definir";
constexpr char kDictionaryQueryKeywordDE[] = "Definieren";

}  // namespace

// Get the best dictionary query keyword based on the language.
const char* GetDictionaryQueryKeyword(const std::string& language) {
  if (language == "es")
    return kDictionaryQueryKeywordES;
  if (language == "it")
    return kDictionaryQueryKeywordIT;
  if (language == "fr")
    return kDictionaryQueryKeywordFR;
  if (language == "pt")
    return kDictionaryQueryKeywordPT;
  if (language == "de")
    return kDictionaryQueryKeywordDE;

  return kDictionaryQueryKeyword;
}

const PreprocessedOutput PreprocessRequest(const IntentInfo& intent_info) {
  PreprocessedOutput processed_output;
  processed_output.intent_info = intent_info;
  processed_output.query = intent_info.intent_text;

  switch (intent_info.intent_type) {
    case IntentType::kUnit:
      processed_output.query = base::StringPrintf(
          kUnitConversionQueryRewriteTemplate, intent_info.intent_text.c_str());
      break;
    case IntentType::kDictionary:
      processed_output.query = base::StringPrintf(
          kDictionaryQueryRewriteTemplate,
          GetDictionaryQueryKeyword(intent_info.source_language),
          intent_info.intent_text.c_str());
      break;
    case IntentType::kTranslation:
      processed_output.query = base::StringPrintf(
          kTranslationQueryRewriteTemplate, intent_info.intent_text.c_str());
      break;
    case IntentType::kUnknown:
      // TODO(llin): Update to NOTREACHED after integrating with TCLib.
      break;
  }
  return processed_output;
}

std::string BuildDefinitionTitleText(const std::string& query_term,
                                     const std::string& phonetics) {
  return l10n_util::GetStringFUTF8(IDS_QUICK_ANSWERS_DEFINITION_TITLE_TEXT,
                                   base::UTF8ToUTF16(query_term),
                                   base::UTF8ToUTF16(phonetics));
}

std::string BuildKpEntityTitleText(const std::string& average_score,
                                   const std::string& aggregated_count) {
  return l10n_util::GetStringFUTF8(IDS_QUICK_ANSWERS_RATING_REVIEW_TITLE_TEXT,
                                   base::UTF8ToUTF16(average_score),
                                   base::UTF8ToUTF16(aggregated_count));
}

std::string BuildTranslationTitleText(const IntentInfo& intent_info) {
  auto locale_name = l10n_util::GetDisplayNameForLocale(
      intent_info.source_language, intent_info.device_language, true);
  return l10n_util::GetStringFUTF8(IDS_QUICK_ANSWERS_TRANSLATION_TITLE_TEXT,
                                   base::UTF8ToUTF16(intent_info.intent_text),
                                   locale_name);
}

std::string BuildUnitConversionResultText(const std::string& result_value,
                                          const std::string& name) {
  return l10n_util::GetStringFUTF8(
      IDS_QUICK_ANSWERS_UNIT_CONVERSION_RESULT_TEXT,
      base::UTF8ToUTF16(result_value), base::UTF8ToUTF16(name));
}

std::string UnescapeStringForHTML(const std::string& string) {
  return base::UTF16ToUTF8(base::UnescapeForHTML(base::UTF8ToUTF16(string)));
}

std::optional<double> GetRatio(const std::optional<double>& value1,
                               const std::optional<double>& value2) {
  if (!value1.has_value() || !value2.has_value()) {
    return std::nullopt;
  }

  if (value1.value() == 0 || value2.value() == 0) {
    return std::nullopt;
  }

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

std::string BuildRoundedUnitAmountDisplayText(double unit_amount) {
  // We use |kResultValueTemplate| (i.e. '%.6g') in the `base::StringPrintf`
  // call below. Precision with `g` is the number of significant digits, not the
  // number of decimal places.
  //
  // We would like to show a conversion term rounded to a maximum of three
  // decimal places. This rounding calculation here will drop the values of the
  // fourth decimal place and later, turning them into trailing zeros that
  // `base::StringPrintf` will remove when formatting.
  double rounded_unit_amount = std::round(unit_amount * 1000) / 1000.0;

  return base::StringPrintf(kResultValueTemplate, rounded_unit_amount);
}

}  // namespace quick_answers