Skip to content

Commit 4e4c127

Browse files
Fix float parsing for all languages.
1 parent 5ec6fa0 commit 4e4c127

File tree

16 files changed

+73
-71
lines changed

16 files changed

+73
-71
lines changed

grammar/de/src/rules.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,28 +2328,26 @@ pub fn rules_numbers(b: &mut RuleSetBuilder<Dimension>) -> RustlingResult<()> {
23282328
|text_match| FloatValue::new(text_match.group(1).replace(",", ".").parse()?)
23292329
);
23302330
b.rule_3("number dot number",
2331-
number_check!(|number: &NumberValue| !number.prefixed()),
2331+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
23322332
b.reg(r#"komma"#)?,
2333-
number_check!(|number: &NumberValue| !number.suffixed()),
2333+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
23342334
|a, _, b| {
2335-
let power = b.value().value().to_string().chars().count();
2336-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
2335+
let value: f64 = format!("{}.{}", a.value().value, b.value().value).parse()?;
23372336
Ok(FloatValue {
2338-
value: b.value().value() * coeff + a.value().value(),
2337+
value,
23392338
..FloatValue::default()
23402339
})
2341-
}
2342-
);
2340+
});
23432341
b.rule_4("number dot zero ... number",
2344-
number_check!(|number: &NumberValue| !number.prefixed()),
2342+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
23452343
b.reg(r#"komma"#)?,
23462344
b.reg(r#"(?:(?:null )*(?:null))"#)?,
2347-
number_check!(|number: &NumberValue| !number.suffixed()),
2345+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
23482346
|a, _, zeros, b| {
2349-
let power = zeros.group(0).split_whitespace().count() + b.value().value().to_string().chars().count();
2350-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
2347+
let zeros_string = std::iter::repeat("0").take(zeros.group(0).split_whitespace().count()).collect::<String>();
2348+
let value: f64 = format!("{}.{}{}", a.value().value, zeros_string, b.value().value).parse()?;
23512349
Ok(FloatValue {
2352-
value: b.value().value() * coeff + a.value().value(),
2350+
value,
23532351
..FloatValue::default()
23542352
})
23552353
});

grammar/de/src/training.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ pub fn examples_numbers(v: &mut Vec<::rustling::train::Example<Dimension>>) {
309309
example!(v, check_integer(102), "102", "hundert zwei");
310310
example!(v, check_float(1.1), "1,1", "1 komma 1", "1,10", "01,10");
311311
example!(v, check_float(0.77), "0,77", ",77");
312+
example!(v, check_float(0.3), "0,3", "null komma drei");
313+
example!(v, check_float(0.03), "0,03", "null komma null drei");
312314
example!(v, check_integer(100000), "100.000", "100000", "100K", "100k");
313315
example!(v, check_integer(3000000), "3M", "3000K", "3000000", "3.000.000");
314316
example!(v, check_integer(1200000), "1.200.000", "1200000", "1,2M", "1200K", ",0012G");

grammar/en/src/rules_number.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,29 @@ pub fn rules_numbers(b: &mut RuleSetBuilder<Dimension>) -> RustlingResult<()> {
193193
|integer, _| FloatValue::new(integer.value().value as f64 + 0.25)
194194
);
195195
b.rule_3("number dot number",
196-
number_check!(|number: &NumberValue| !number.prefixed()),
196+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
197197
b.reg(r#"dot|point"#)?,
198-
number_check!(|number: &NumberValue| !number.suffixed()),
198+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
199199
|a, _, b| {
200-
let power = b.value().value().to_string().chars().count();
201-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
200+
let value: f64 = format!("{}.{}", a.value().value, b.value().value).parse()?;
202201
Ok(FloatValue {
203-
value: b.value().value() * coeff + a.value().value(),
202+
value,
204203
..FloatValue::default()
205204
})
206205
});
207206
b.rule_4("number dot zero... number",
208-
number_check!(|number: &NumberValue| !number.prefixed()),
207+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
209208
b.reg(r#"dot|point"#)?,
210209
b.reg(r#"(?:(?:oh |zero )*(?:oh|zero))"#)?,
211-
number_check!(|number: &NumberValue| !number.suffixed()),
210+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
212211
|a, _, zeros, b| {
213-
let power = zeros.group(0).split_whitespace().count() + b.value().value().to_string().chars().count();
214-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
212+
let zeros_string = std::iter::repeat("0").take(zeros.group(0).split_whitespace().count()).collect::<String>();
213+
let value: f64 = format!("{}.{}{}", a.value().value, zeros_string, b.value().value).parse()?;
215214
Ok(FloatValue {
216-
value: b.value().value() * coeff + a.value().value(),
215+
value,
217216
..FloatValue::default()
218217
})
218+
219219
});
220220
b.rule_1_terminal("decimal with thousands separator",
221221
b.reg(r#"(\d+(,\d\d\d)+\.\d+)"#)?,

grammar/en/src/training.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ pub fn examples_numbers(v: &mut Vec<::rustling::train::Example<Dimension>>) {
259259
example!(v, check_integer(17), "17", "seventeen");
260260
example!(v, check_integer(18), "18", "eighteen");
261261
example!(v, check_float(1.1), "1.1", "1.10", "01.10", "one point ten", "one point one");
262+
example!(v, check_float(0.3), "0.3", "0.30", "zero point three");
262263
example!(v, check_float(0.5), "0.5", "0.50", "zero point five");
264+
example!(v, check_float(0.05), "0.05", "zero point zero five");
263265
example!(v, check_float(32.75), "32.75", "thirty-two point seventy-five");
264266
example!(v, check_float(10.08), "10.08", "ten point zero eight");
265267
example!(v,

grammar/es/src/rules_number.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,29 @@ pub fn rules_numbers(b: &mut RuleSetBuilder<Dimension>) -> RustlingResult<()> {
218218
FloatValue::new(value)
219219
});
220220
b.rule_3("number dot number",
221-
number_check!(|number: &NumberValue| !number.prefixed()),
221+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
222222
b.reg(r#"punto|coma"#)?,
223-
number_check!(|number: &NumberValue| !number.suffixed()),
223+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
224224
|a, _, b| {
225-
let power = b.value().value().to_string().chars().count();
226-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
225+
let value: f64 = format!("{}.{}", a.value().value, b.value().value).parse()?;
227226
Ok(FloatValue {
228-
value: b.value().value() * coeff + a.value().value(),
227+
value,
229228
..FloatValue::default()
230229
})
231230
});
232231
b.rule_4("number dot zero ... number",
233-
number_check!(|number: &NumberValue| !number.prefixed()),
232+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
234233
b.reg(r#"punto|coma"#)?,
235234
b.reg(r#"(?:(?:[zc]ero )*(?:[zc]ero))"#)?,
236-
number_check!(|number: &NumberValue| !number.suffixed()),
235+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
237236
|a, _, zeros, b| {
238-
let power = zeros.group(0).split_whitespace().count() + b.value().value().to_string().chars().count();
239-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
237+
let zeros_string = std::iter::repeat("0").take(zeros.group(0).split_whitespace().count()).collect::<String>();
238+
let value: f64 = format!("{}.{}{}", a.value().value, zeros_string, b.value().value).parse()?;
240239
Ok(FloatValue {
241-
value: b.value().value() * coeff + a.value().value(),
240+
value,
242241
..FloatValue::default()
243242
})
244243
});
245-
246244
b.rule_1_terminal("decimal with thousands separator",
247245
b.reg(r#"(\d+(\.\d\d\d)+,\d+)"#)?,
248246
|text_match| {

grammar/es/src/training.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ pub fn examples_numbers(v: &mut Vec<::rustling::train::Example<Dimension>>) {
292292
example!(v, check_integer(-1200000), "- 1.200.000", "-1200000", "menos 1200000", "-1,2M", "-1200K", "-,0012G", "menos un millón doscientos mil");
293293
example!(v, check_float(1.1), "1,1", "1,10", "1,10", "uno punto uno", "uno coma uno", "uno punto diez", "uno coma diez");
294294
example!(v, check_float(0.5), "0,5", "0,50", "cero punto cinco", "cero coma cinco", "cero punto cincuenta", "cero coma cincuenta");
295+
example!(v, check_float(0.3), "0,3", "0,30", "cero punto tres", "cero coma tres");
296+
example!(v, check_float(0.03), "0,03", "cero punto cero tres", "cero coma cero tres");
295297
example!(v, check_float(32.75), "32,75", "treinta y dos punto setenta y cinco", "treinta y dos coma setenta y cinco");
296298
example!(v, check_float(10.08), "10,08", "diez punto cero ocho", "diez coma cero ocho");
297299
// TODO: Check if want/need support for ordinal special character/overscript

grammar/fr/src/rules_number.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ pub fn rules_numbers(b: &mut RuleSetBuilder<Dimension>) -> RustlingResult<()> {
212212
integer_check!(|integer: &IntegerValue| !integer.suffixed),
213213
|a, _, zeros, b| {
214214
let zeros_string = std::iter::repeat("0").take(zeros.group(0).split_whitespace().count()).collect::<String>();
215-
println!("{:?}", format!("{}.{}{}", a.value().value, zeros_string, b.value().value));
216215
let value: f64 = format!("{}.{}{}", a.value().value, zeros_string, b.value().value).parse()?;
217216
Ok(FloatValue {
218217
value,

grammar/fr/src/training.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ pub fn examples_numbers(v: &mut Vec<::rustling::train::Example<Dimension>>) {
340340
"-,0012G");
341341
example!(v, check_float(1.1), "1,1", "1,10", "un virgule dix");
342342
example!(v, check_float(0.5), "0,5", "0,50", "zéro virgule cinq", "zero point cinq");
343+
example!(v, check_float(0.3), "0,3", "0,30", "zéro virgule trois", "zero point trois");
344+
example!(v, check_float(0.03), "0,03", "zéro virgule zéro trois", "zero point zero trois");
343345
example!(v, check_float(32.75), "32,75", "trente-deux virgule soixante-quinze");
344346
example!(v, check_float(10.08), "10,08", "dix virgule zéro huit", "dix point zéro huit");
345347
example!(v, check_ordinal(1), "1er", "1ere", "le 1er");

grammar/it/src/rules_number.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,32 +197,29 @@ pub fn rules_numbers(b: &mut RuleSetBuilder<Dimension>) -> RustlingResult<()> {
197197
FloatValue::new(value)
198198
});
199199
b.rule_3("number dot number",
200-
number_check!(|number: &NumberValue| !number.prefixed()),
200+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
201201
b.reg(r#"punto|virgola"#)?,
202-
number_check!(|number: &NumberValue| !number.suffixed()),
202+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
203203
|a, _, b| {
204-
let power = b.value().value().to_string().chars().count();
205-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
204+
let value: f64 = format!("{}.{}", a.value().value, b.value().value).parse()?;
206205
Ok(FloatValue {
207-
value: b.value().value() * coeff + a.value().value(),
206+
value,
208207
..FloatValue::default()
209208
})
210209
});
211-
212210
b.rule_4("number dot zero ... number",
213-
number_check!(|number: &NumberValue| !number.prefixed()),
211+
integer_check!(|integer: &IntegerValue| !integer.prefixed),
214212
b.reg(r#"punto|virgola"#)?,
215213
b.reg(r#"(?:(?:zero )*(?:zero))"#)?,
216-
number_check!(|number: &NumberValue| !number.suffixed()),
214+
integer_check!(|integer: &IntegerValue| !integer.suffixed),
217215
|a, _, zeros, b| {
218-
let power = zeros.group(0).split_whitespace().count() + b.value().value().to_string().chars().count();
219-
let coeff = 10.0_f64.powf(-1.0 * power as f64);
216+
let zeros_string = std::iter::repeat("0").take(zeros.group(0).split_whitespace().count()).collect::<String>();
217+
let value: f64 = format!("{}.{}{}", a.value().value, zeros_string, b.value().value).parse()?;
220218
Ok(FloatValue {
221-
value: b.value().value() * coeff + a.value().value(),
219+
value,
222220
..FloatValue::default()
223221
})
224222
});
225-
226223
b.rule_1_terminal("decimal with thousands separator",
227224
b.reg(r#"(\d+(\.\d\d\d)+,\d+)"#)?,
228225
|text_match| {

grammar/it/src/training.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ pub fn examples_numbers(v: &mut Vec<::rustling::train::Example<Dimension>>) {
295295
example!(v, check_integer(-1200000), "- 1.200.000", "-1200000", "meno 1200000", "-1,2M", "-1200K", "-,0012G");
296296
example!(v, check_float(1.1), "1,1", "1,10", "01,10", "uno punto uno", "uno virgola uno", "uno punto dieci", "uno virgola dieci");
297297
example!(v, check_float(0.5), "0,5", "0,50", "zero punto cinque", "zero virgola cinque", "zero punto cinquanta", "zero virgola cinquanta");
298+
example!(v, check_float(0.3), "0,3", "0,30", "zero punto tre", "zero virgola tre");
299+
example!(v, check_float(0.03), "0,03", "zero punto zero tre", "zero virgola zero tre");
298300
example!(v, check_float(32.75), "32,75", "trenta due punto settanta cinque", "trenta due virgola settanta cinque");
299301
example!(v, check_float(10.08), "10,08", "dieci punto zero otto");//, "dieci virgola zero otto");
300302
example!(v, check_ordinal(1), "1o", "1a", "il 1o", "la 1a", "1°");

0 commit comments

Comments
 (0)