2#include "NumberToText.h"
23 audio_tools::Vector<const char*>&
say(
double value,
24 const char* unit =
nullptr,
27 number.set(value, decimals);
32 audio_tools::Vector<const char*>&
say(int64_t wholeNumber,
33 const char* unit =
nullptr) {
35 number.set(wholeNumber);
40 audio_tools::Vector<const char*>&
say(
const char* wholeNumber,
46 const char* unit = unitIn ? unitIn : default_unit;
48 bool isOne = Str(wholeNumber).toLong() == 1l;
51 int idx = idxCombinedUnit(unit);
54 addAll(ntt.
say(wholeNumber, decimals));
59 addAll(ntt.
say(wholeNumber));
61 processCombinedUnit(idx, isOne);
66 int digits = decimalInfo(unit);
67 const char* decimals_to_say = calc.decAsInt(decimals, digits);
68 addAll(ntt.
say(decimals_to_say));
70 bool decIsOne = Str(decimals).toLong() == 1l;
71 processDecimalUnit(idx, decIsOne);
76 callback(result, reference);
83 virtual audio_tools::Vector<const char*>&
allTexts() {
87 for (
int j = 0; j < max_unit_1; j++) {
94 for (
int i = 1; i < 4; i++) {
95 const char* word = unit2[j][i];
96 if (word !=
nullptr) {
108 const char* default_unit =
nullptr;
109 audio_tools::Vector<const char*> result;
113 const static int max_unit_1 = 6;
114 const char* unit_1[max_unit_1][3] = {
115 {
"lb",
"pound",
"pounds"}, {
"ft",
"foot",
"feet"},
116 {
"in",
"inch",
"inches"}, {
"mi",
"mile",
"miles"},
117 {
"$",
"dollar",
"dollars"}, {
"¢",
"cent",
"cents"}};
121 const char* unit2[
max_unit_2][4] = {{
"mph",
"miles",
"per",
"hour"},
122 {
"C",
"degrees",
"celcius",
nullptr},
123 {
"F",
"degrees",
"fahrenheit",
nullptr},
124 {
"%",
"percent",
nullptr,
nullptr},
125 {
"lt",
"liter",
nullptr,
nullptr},
126 {
"ml",
"milli",
"lt",
nullptr},
127 {
"gr",
"gram",
nullptr,
nullptr},
128 {
"kg",
"kilo",
"gr",
nullptr},
129 {
"mg",
"milli",
"gr",
nullptr},
130 {
"km",
"kilo",
"m",
nullptr},
131 {
"m",
"meter",
nullptr,
nullptr},
132 {
"cm",
"centi",
"m",
nullptr},
133 {
"mm",
"milli",
"m",
nullptr},
134 {
"u.s.",
"us",
nullptr,
nullptr},
135 {
"usd",
"u.s.",
"$",
nullptr}};
139 const static int max_unit_3 = 6;
140 const char* unit3[max_unit_3][2] = {{
"usd",
"¢"}, {
"gr",
"mg"}, {
"kg",
"gr"},
141 {
"m",
"mm"}, {
"km",
"m"}, {
"lt",
"ml"}};
148 const static int decimal_info_len = 1;
149 const DecimalInfo decimal_info[decimal_info_len] = {{
"usd", 2}};
152 void add(
const char* str) { result.push_back(str); }
155 void addAll(audio_tools::Vector<const char*>& words) {
156 for (
auto word : words) {
168 if (process1(unit, isOne))
return true;
170 if (process2(unit, isOne))
return true;
173 LOGE(
"Error %s", unit);
178 bool process2(
const char* unit,
bool isOne) {
183 if (u.equalsIgnoreCase(unit2[j][0])) {
184 for (
int i = 1; i < 4; i++) {
185 const char* word = unit2[j][i];
186 if (word !=
nullptr) {
202 bool process1(
const char* unit,
bool isOne) {
204 for (
int j = 0; j < max_unit_1; j++) {
205 if (u.equalsIgnoreCase(unit_1[j][0])) {
207 add(isOne ? unit_1[j][1] : unit_1[j][2]);
214 bool isKey(
const char* unit) {
216 for (
int j = 0; j < max_unit_1; j++) {
217 if (u.equalsIgnoreCase(unit_1[j][0])) {
223 if (u.equalsIgnoreCase(unit2[j][0])) {
232 int idxCombinedUnit(
const char* unit) {
234 for (
int i = 0; i < max_unit_3; i++) {
235 if (u.equalsIgnoreCase(unit3[i][0])) {
242 bool processCombinedUnit(
int idx,
bool isOne) {
243 return process(unit3[idx][0], isOne);
246 bool processDecimalUnit(
int idx,
bool isOne) {
247 return process(unit3[idx][1], isOne);
250 int decimalInfo(
const char* unit) {
252 for (
int i = 0; i < decimal_info_len; i++) {
253 if (u.equalsIgnoreCase(decimal_info[i].id)) {
254 return decimal_info[i].dec;
Convert numbers to string and provide integer and decimals.
Definition: SimpleTTSBase.h:106
const char * intValue()
provides the full number
Definition: SimpleTTSBase.h:139
const char * decValues()
provides the decimals after the .
Definition: SimpleTTSBase.h:144
Translates a number into englich words.
Definition: NumberToText.h:15
audio_tools::Vector< const char * > & say(double value, int decimals=2)
converts a real number to it's text representation (with the indicated number of digits)
Definition: NumberToText.h:19
An extension of the Number to Text functionality which supports numbers with units of measures.
Definition: NumberUnitToText.h:13
audio_tools::Vector< const char * > & say(int64_t wholeNumber, const char *unit=nullptr)
converts an integer with a unit to it's text representation
Definition: NumberUnitToText.h:32
bool process(const char *unit, bool isOne)
resolve keys until we end up with final words
Definition: NumberUnitToText.h:162
audio_tools::Vector< const char * > & say(double value, const char *unit=nullptr, int decimals=2)
converts a real number with a unit to it's text representation
Definition: NumberUnitToText.h:23
static const int max_unit_2
units that can be constructed by combining words
Definition: NumberUnitToText.h:120
audio_tools::Vector< const char * > & say(const char *wholeNumber, const char *decimals, const char *unitIn)
converts a number provided by string components to it's text representation
Definition: NumberUnitToText.h:40
virtual audio_tools::Vector< const char * > & allTexts()
Provides all texts.
Definition: NumberUnitToText.h:83
Common Functionality for TTS classes.
Definition: SimpleTTSBase.h:15
Units might have different decimals. The decimal system is based on 3. But currencies have usually 2.
Definition: NumberUnitToText.h:144