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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
use std::collections::HashSet;
use std::iter::FromIterator;
pub use crate::ingredient::Ingredient;
use fraction::fraction_number;
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{alpha1, char, not_line_ending, space0, space1},
combinator::{opt, verify},
error::{context, VerboseError},
multi::{many1, separated_list1},
number::complete::double,
sequence::{delimited, tuple},
IResult,
};
use tracing::info;
use unit::Measure;
extern crate nom;
#[cfg(feature = "serde-derive")]
#[macro_use]
extern crate serde;
mod fraction;
pub mod ingredient;
pub mod rich_text;
pub mod unit;
pub mod util;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>;
/// use [IngredientParser] to customize
pub fn from_str(input: &str) -> Ingredient {
(IngredientParser::new(false)).from_str(input)
}
#[derive(Clone, PartialEq, Debug, Default)]
pub struct IngredientParser {
pub units: HashSet<String>,
pub adjectives: HashSet<String>,
pub is_rich_text: bool,
}
impl IngredientParser {
pub fn new(is_rich_text: bool) -> Self {
let units: Vec<String> = vec![
// non standard units - these aren't really convertible for the most part.
// default set
"whole", "packet", "sticks", "stick", "cloves", "clove", "bunch", "head", "large",
"pinch", "small", "medium", "package", "recipe", "slice", "standard", "can", "leaf",
"leaves", "strand",
]
.iter()
.map(|&s| s.into())
.collect();
let adjectives: Vec<String> = vec![
"chopped",
"minced",
"diced",
"freshly ground",
"freshly grated",
"finely chopped",
"thinly sliced",
"sliced",
"plain",
"to taste",
]
.iter()
.map(|&s| s.into())
.collect();
IngredientParser {
units: HashSet::from_iter(units.iter().cloned()),
adjectives: HashSet::from_iter(adjectives.iter().cloned()),
is_rich_text,
}
}
/// wrapper for [self.parse_ingredient]
/// ```
/// use ingredient::{from_str};
/// assert_eq!(from_str("one whole egg").to_string(),"1 whole egg");
/// ```
pub fn from_str(self, input: &str) -> Ingredient {
//todo: add back error handling? can't get this to ever fail since parser is pretty flexible
self.parse_ingredient(input).unwrap().1
}
/// Parses one or two amounts, e.g. `12 grams` or `120 grams / 1 cup`. Used by [self.parse_ingredient].
/// ```
/// use ingredient::{IngredientParser,unit::Measure};
/// let ip = IngredientParser::new(false);
/// assert_eq!(
/// ip.parse_amount("120 grams"),
/// vec![Measure::parse_new("grams",120.0)]
/// );
/// assert_eq!(
/// ip.parse_amount("120 grams / 1 cup"),
/// vec![Measure::parse_new("grams",120.0),Measure::parse_new("cup", 1.0)]
/// );
/// assert_eq!(
/// ip.parse_amount("120 grams / 1 cup / 1 whole"),
/// vec![Measure::parse_new("grams",120.0),Measure::parse_new("cup", 1.0),Measure::parse_new("whole", 1.0)]
/// );
/// ```
#[tracing::instrument(name = "parse_amount")]
pub fn parse_amount(&self, input: &str) -> Vec<Measure> {
// todo: also can't get this one to fail either
self.clone().many_amount(input).expect(input).1
}
/// Parse an ingredient line item, such as `120 grams / 1 cup whole wheat flour, sifted lightly`.
///
/// returns an [Ingredient], Can be used as a wrapper to return verbose errors.
///
/// supported formats include:
/// * 1 g name
/// * 1 g / 1g name, modifier
/// * 1 g; 1 g name
/// * ¼ g name
/// * 1/4 g name
/// * 1 ¼ g name
/// * 1 1/4 g name
/// * 1 g (1 g) name
/// * 1 g name (about 1 g; 1 g)
/// * name
/// * 1 name
/// ```
/// use ingredient::{IngredientParser, ingredient::Ingredient, unit::Measure};
/// let ip = IngredientParser::new(false);
/// assert_eq!(
/// ip.parse_ingredient("1¼ cups / 155.5 grams flour"),
/// Ok((
/// "",
/// Ingredient {
/// name: "flour".to_string(),
/// amounts: vec![
/// Measure::parse_new("cups", 1.25),
/// Measure::parse_new("grams", 155.5),
/// ],
/// modifier: None,
/// }
/// ))
/// );
/// ```
#[tracing::instrument(name = "parse_ingredient")]
pub fn parse_ingredient(self, input: &str) -> Res<&str, Ingredient> {
context(
"ing",
tuple((
opt(|a| self.clone().many_amount(a)),
space0, // space between amount(s) and name
opt(tuple((|a| self.clone().adjective(a), space1))), // optional modifier
opt(many1(text)), // name, can be multiple words
opt(|a| self.clone().amt_parens(a)), // can have some more amounts in parens after the name
opt(tag(", ")), // comma seperates the modifier
not_line_ending, // modifier, can be multiple words and even include numbers, since once we've hit the comma everything is fair game.
)),
)(input)
.map(|(next_input, res)| {
let (
amounts,
_maybespace,
adjective,
name_chunks,
amounts2,
_maybecomma,
modifier_chunks,
): (
Option<Vec<Measure>>,
&str,
Option<(String, &str)>,
Option<Vec<&str>>,
Option<Vec<Measure>>,
Option<&str>,
&str,
) = res;
let mut modifiers: String = modifier_chunks.to_owned();
if let Some((adjective, _)) = adjective {
modifiers.push_str(&adjective);
}
let mut name: String = name_chunks
.unwrap_or(vec![])
.join("")
.trim_matches(' ')
.to_string();
// if the ingredient name still has adjective in it, remove that
self.adjectives.iter().for_each(|f| {
if name.contains(f) {
modifiers.push_str(f);
name = name.replace(f, "").trim_matches(' ').to_string();
}
});
let mut amounts = match amounts {
Some(a) => a,
None => vec![],
};
amounts = match amounts2 {
Some(a) => amounts.into_iter().chain(a.into_iter()).collect(),
None => amounts,
};
(
next_input,
Ingredient {
name,
amounts,
modifier: match modifiers.chars().count() {
0 => None,
_ => Some(modifiers.to_string()),
},
},
)
})
}
fn get_value(self, input: &str) -> Res<&str, (f64, Option<f64>)> {
context(
"get_value",
alt((
|a| self.clone().upper_range_only(a),
|a| self.clone().num_or_range(a),
)),
)(input)
}
fn num_or_range(self, input: &str) -> Res<&str, (f64, Option<f64>)> {
context(
"num_or_range",
tuple((
|a| self.clone().num(a),
opt(|a| self.clone().range_up_num(a)),
)),
)(input)
.map(|(next_input, res)| {
let (val, upper_val) = res;
let upper = match upper_val {
Some(u) => Some(u),
None => None,
};
(next_input, (val, upper))
})
}
fn upper_range_only(self, input: &str) -> Res<&str, (f64, Option<f64>)> {
context(
"upper_range_only",
tuple((
opt(space0),
alt((tag("up to"), tag("at most"))),
space0,
|a| self.clone().num(a),
)),
)(input)
.map(|(next_input, res)| (next_input, (0.0, Some(res.3))))
}
fn unit(self, input: &str) -> Res<&str, String> {
context(
"unit",
verify(unitamt, |s: &str| unit::is_valid(self.units.clone(), s)),
)(input)
}
fn adjective(self, input: &str) -> Res<&str, String> {
context(
"adjective",
verify(unitamt, |s: &str| {
self.adjectives.contains(&s.to_lowercase())
}),
)(input)
}
// parses a single amount
fn amount1(self, input: &str) -> Res<&str, Measure> {
let res = context(
"amount1",
tuple(
(
opt(tag("about ")), // todo: add flag for estimates
opt(|a| self.clone().mult_prefix_1(a)),
|a| self.clone().get_value(a), // value
space0,
opt(|a| self.clone().unit(a)), // unit
opt(alt((tag("."), tag(" of")))),
), // 1 gram
),
)(input)
.map(|(next_input, res)| {
let (_prefix, mult, value, _space, unit, _period) = res;
let mut v = value.0;
if mult.is_some() {
v = v * mult.unwrap();
}
return (
next_input,
Measure::from_parts(
unit.unwrap_or("whole".to_string())
.to_string()
.to_lowercase()
.as_ref(),
v,
value.1,
),
);
});
res
}
// parses an amount like `78g to 104g cornmeal`
fn amount_with_units_twice(self, input: &str) -> Res<&str, Option<Measure>> {
let res = context(
"amount_with_units_twice",
tuple((
opt(tag("about ")), // todo: add flag for estimates
|a| self.clone().get_value(a), // value
space0,
opt(|a| self.clone().unit(a)), // unit
|a| self.clone().range_up_num(a),
opt(|a| self.clone().unit(a)),
opt(alt((tag("."), tag(" of")))),
)),
)(input)
.map(|(next_input, res)| {
let (_prefix, value, _space, unit, upper_val, upper_unit, _period) = res;
if upper_unit.is_some() && unit != upper_unit {
info!("unit mismatch: {:?} vs {:?}", unit, upper_unit);
// panic!("unit mismatch: {:?} vs {:?}", unit, upper_unit)
return (next_input, None);
}
// let upper = match value.1 {
// Some(u) => Some(u),
// None => upper_val,
// match upper_val {
// Some(u) => Some(u),
// None => None,
// },
// };
let upper = Some(upper_val);
return (
next_input,
Some(Measure::from_parts(
unit.unwrap_or("whole".to_string())
.to_string()
.to_lowercase()
.as_ref(),
value.0,
upper,
)),
);
});
res
}
// parses 1-n amounts, e.g. `12 grams` or `120 grams / 1 cup`
#[tracing::instrument(name = "many_amount")]
fn many_amount(self, input: &str) -> Res<&str, Vec<Measure>> {
context(
"many_amount",
separated_list1(
alt((tag("; "), tag(" / "), tag(" "), tag(", "), tag("/"))),
alt((
|a| self.clone().plus_amount(a).map(|(a, b)| (a, vec![b])),
|a| {
self.clone().amount_with_units_twice(a).map(|(a, b)| {
(
a,
match b {
Some(a) => vec![a],
None => vec![],
},
)
})
}, // regular amount
|a| self.clone().amt_parens(a), // amoiunt with parens
|a| self.clone().amount1(a).map(|(a, b)| (a, vec![b])), // regular amount
)),
),
)(input)
.map(|(next_input, res)| {
// let (a, b) = res;
(next_input, res.into_iter().flatten().collect())
})
}
fn amt_parens(self, input: &str) -> Res<&str, Vec<Measure>> {
context(
"amt_parens",
delimited(char('('), |a| self.clone().many_amount(a), char(')')),
)(input)
}
/// handles vulgar fraction, or just a number
fn num(self, input: &str) -> Res<&str, f64> {
if self.is_rich_text {
context("num", alt((fraction_number, double)))(input)
} else {
context("num", alt((fraction_number, text_number, double)))(input)
}
}
fn mult_prefix_1(self, input: &str) -> Res<&str, f64> {
context(
"mult_prefix_1",
tuple((|a| self.clone().num(a), space1, tag("x"), space1)),
)(input)
.map(|(next_input, res)| {
let (num, _, _, _) = res;
(next_input, num)
})
}
fn range_up_num(self, input: &str) -> Res<&str, f64> {
context(
"range_up_num",
alt((
tuple((
space0,
alt((tag("-"), tag("–"))), // second dash is an unusual variant
space0,
|a| self.clone().num(a),
)),
tuple((
space1,
alt((tag("to"), tag("through"), tag("or"))),
space1,
|a| self.clone().num(a),
)),
)),
)(input)
.map(|(next_input, (_space1, _, _space2, num))| (next_input, num))
}
fn plus_amount(self, input: &str) -> Res<&str, Measure> {
context(
"plus_num",
tuple((
|a| self.clone().amount1(a),
space1,
tag("plus"),
space1,
|a| self.clone().amount1(a),
)),
)(input)
.map(|(next_input, (a, _space1, _, _, b))| {
let c = a.add(b).unwrap();
return (next_input, c);
})
}
}
fn text(input: &str) -> Res<&str, &str> {
alt((
alpha1,
space1,
tag("-"),
tag("—"),
tag("-"),
tag("'"),
tag("’"),
tag("."),
tag("è"),
tag("î"),
tag("ó"),
tag("é"),
// tag("\""),
))(input)
}
fn unitamt(input: &str) -> Res<&str, String> {
nom::multi::many0(alt((alpha1, tag("°"), tag("\""))))(input)
.map(|(next_input, res)| (next_input, res.join("")))
}
fn text_number(input: &str) -> Res<&str, f64> {
context("text_number", alt((tag("one"), tag("a "))))(input)
.map(|(next_input, _)| (next_input, 1.0))
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use super::*;
#[test]
fn test_amount() {
assert_eq!(
(IngredientParser::new(false)).parse_amount("350 °"),
vec![Measure::parse_new("°", 350.0)]
);
assert_eq!(
(IngredientParser::new(false)).parse_amount("350 °F"),
vec![Measure::parse_new("°f", 350.0)]
);
}
#[test]
fn test_amount_range() {
assert_eq!(
(IngredientParser::new(false)).parse_amount("2¼-2.5 cups"),
vec![Measure::parse_new_with_upper("cups", 2.25, 2.5)]
);
assert_eq!(
Ingredient::try_from("1-2 cups flour"),
Ok(Ingredient {
name: "flour".to_string(),
amounts: vec![Measure::parse_new_with_upper("cups", 1.0, 2.0)],
modifier: None,
})
);
assert_eq!(
format!(
"{}",
(IngredientParser::new(false))
.parse_amount("2 ¼ - 2.5 cups")
.first()
.unwrap()
),
"2.25 - 2.5 cups"
);
assert_eq!(
(IngredientParser::new(false)).parse_amount("2 to 4 days"),
vec![Measure::parse_new_with_upper("days", 2.0, 4.0)]
);
// #30
assert_eq!(
(IngredientParser::new(false)).parse_amount("up to 4 days"),
vec![Measure::parse_new_with_upper("days", 0.0, 4.0)]
);
}
#[test]
fn test_ingredient_parse() {
assert_eq!(
Ingredient::try_from("12 cups flour"),
Ok(Ingredient {
name: "flour".to_string(),
amounts: vec![Measure::parse_new("cups", 12.0)],
modifier: None,
})
);
}
#[test]
fn test_stringy() {
assert_eq!(
format!("res: {}", from_str("12 cups flour")),
"res: 12 cups flour"
);
assert_eq!(from_str("one whole egg").to_string(), "1 whole egg");
assert_eq!(from_str("a tsp flour").to_string(), "1 tsp flour");
}
#[test]
fn test_with_parens() {
assert_eq!(
from_str("1 cup (125.5 grams) AP flour, sifted").to_string(),
"1 cup / 125.5 g AP flour, sifted"
);
}
}