duckling 0.4.0

A Rust port of Facebook's Duckling library for parsing natural language into structured data
Documentation
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
use std::fmt;
use std::rc::Rc;

use crate::dimensions::amount_of_money::AmountOfMoneyData;
use crate::dimensions::credit_card_number::CreditCardNumberData;
use crate::dimensions::distance::DistanceData;
use crate::dimensions::duration::DurationData;
use crate::dimensions::email::EmailData;
use crate::dimensions::numeral::NumeralData;
use crate::dimensions::ordinal::OrdinalData;
use crate::dimensions::phone_number::PhoneNumberData;
use crate::dimensions::quantity::QuantityData;
use crate::dimensions::temperature::TemperatureData;
use crate::dimensions::time::TimeData;
use crate::dimensions::time_grain::Grain;
use crate::dimensions::url::UrlData;
use crate::dimensions::volume::VolumeData;
use chrono::{DateTime, FixedOffset, NaiveDateTime};

/// The kind of dimension to extract from text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DimensionKind {
    /// Numbers: "forty-two", "3.5", "100K"
    Numeral,
    /// Ordinals: "first", "3rd", "22nd"
    Ordinal,
    /// Temperatures: "80 degrees fahrenheit", "3°C"
    Temperature,
    /// Distances: "5 miles", "3 kilometers"
    Distance,
    /// Volumes: "2 gallons", "500ml"
    Volume,
    /// Quantities with product: "5 pounds of sugar"
    Quantity,
    /// Money: "$42.50", "3 euros"
    AmountOfMoney,
    /// Email addresses: "user@example.com"
    Email,
    /// Phone numbers: "(555) 123-4567"
    PhoneNumber,
    /// URLs: `"https://example.com"`
    Url,
    /// Credit card numbers
    CreditCardNumber,
    /// Time grains: "day", "week", "month"
    TimeGrain,
    /// Durations: "3 days", "2 hours"
    Duration,
    /// Times and dates: "tomorrow at 3pm", "in 2 hours"
    Time,
}

impl fmt::Display for DimensionKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            DimensionKind::Numeral => "number",
            DimensionKind::Ordinal => "ordinal",
            DimensionKind::Temperature => "temperature",
            DimensionKind::Distance => "distance",
            DimensionKind::Volume => "volume",
            DimensionKind::Quantity => "quantity",
            DimensionKind::AmountOfMoney => "amount-of-money",
            DimensionKind::Email => "email",
            DimensionKind::PhoneNumber => "phone-number",
            DimensionKind::Url => "url",
            DimensionKind::CreditCardNumber => "credit-card-number",
            DimensionKind::TimeGrain => "time-grain",
            DimensionKind::Duration => "duration",
            DimensionKind::Time => "time",
        };
        write!(f, "{}", s)
    }
}

/// A measurement with a numeric value and unit. Used by Temperature, Distance,
/// Volume, Quantity, and AmountOfMoney dimensions.
///
/// ```
/// use duckling::{parse_en, DimensionKind, DimensionValue, MeasurementValue};
///
/// let results = parse_en("$42.50", &[DimensionKind::AmountOfMoney]);
/// assert_eq!(results[0].value, DimensionValue::AmountOfMoney(MeasurementValue::Value {
///     value: 42.5, unit: "USD".into(),
/// }));
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum MeasurementValue {
    /// An exact measurement.
    Value {
        /// The numeric value.
        value: f64,
        /// The unit (e.g. "fahrenheit", "mile", "USD").
        unit: String,
    },
    /// A range of measurements (e.g. "between 3 and 5 dollars").
    Interval {
        /// The lower bound, if any.
        from: Option<MeasurementPoint>,
        /// The upper bound, if any.
        to: Option<MeasurementPoint>,
    },
}

/// A single endpoint in a [`MeasurementValue::Interval`].
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct MeasurementPoint {
    /// The numeric value.
    pub value: f64,
    /// The unit.
    pub unit: String,
}

/// A resolved time point — either an absolute fixed-offset instant or a naive wall-clock time.
///
/// ```
/// use duckling::{parse, Locale, Lang, Context, Options, DimensionKind,
///                DimensionValue, TimeValue, TimePoint, Grain};
/// use chrono::{FixedOffset, NaiveDate, TimeZone};
///
/// let locale = Locale::new(Lang::EN, None);
/// let context = Context::new(
///     FixedOffset::west_opt(2 * 3600).unwrap()
///         .with_ymd_and_hms(2013, 2, 12, 4, 30, 0).unwrap(),
///     locale,
/// );
/// let options = Options::default();
///
/// // Wall-clock times are Naive (no timezone baked in)
/// let results = parse("tomorrow at 3pm", &locale, &[DimensionKind::Time], &context, &options);
/// if let DimensionValue::Time(TimeValue::Single { value: TimePoint::Naive { value, grain }, .. }) = &results[0].value {
///     assert_eq!(*value, NaiveDate::from_ymd_opt(2013, 2, 13).unwrap().and_hms_opt(15, 0, 0).unwrap());
///     assert_eq!(*grain, Grain::Hour);
/// } else { panic!("expected Naive time point"); }
///
/// // Relative offsets from now are Instant (pinned to an absolute offset-aware moment)
/// let results = parse("in one hour", &locale, &[DimensionKind::Time], &context, &options);
/// if let DimensionValue::Time(TimeValue::Single { value: TimePoint::Instant { value, grain }, .. }) = &results[0].value {
///     let expected = FixedOffset::west_opt(2 * 3600).unwrap()
///         .with_ymd_and_hms(2013, 2, 12, 5, 30, 0).unwrap();
///     assert_eq!(*value, expected);
///     assert_eq!(*grain, Grain::Minute);
/// } else { panic!("expected Instant time point"); }
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum TimePoint {
    /// An absolute fixed-offset moment (e.g. "now", "in 2 hours", "5pm EST").
    Instant {
        /// The offset-aware datetime.
        value: DateTime<FixedOffset>,
        /// The precision grain.
        grain: Grain,
    },
    /// A wall-clock/calendar time with no timezone assumption (e.g. "5pm", "tomorrow", "March 15th").
    Naive {
        /// The naive datetime.
        value: NaiveDateTime,
        /// The precision grain.
        grain: Grain,
    },
}

impl TimePoint {
    /// Returns the precision grain of this time point.
    pub fn grain(&self) -> Grain {
        match self {
            TimePoint::Instant { grain, .. } | TimePoint::Naive { grain, .. } => *grain,
        }
    }
}

/// A pair of interval endpoints, used in the `values` array for intervals.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct IntervalEndpoints {
    /// The start of the interval, if bounded.
    pub from: Option<TimePoint>,
    /// The end of the interval, if bounded.
    pub to: Option<TimePoint>,
}

/// A resolved time value — either a single point or an interval.
///
/// Includes a `values` array of up to 3 next occurrences, matching Haskell's
/// `TimeValue SingleTimeValue [SingleTimeValue]`.
///
/// ```
/// use duckling::{parse, Locale, Lang, Context, Options, DimensionKind,
///                DimensionValue, TimeValue, TimePoint, Grain};
/// use chrono::{FixedOffset, NaiveDate, TimeZone};
///
/// let locale = Locale::new(Lang::EN, None);
/// let context = Context::new(
///     FixedOffset::west_opt(2 * 3600).unwrap()
///         .with_ymd_and_hms(2013, 2, 12, 4, 30, 0).unwrap(),
///     locale,
/// );
/// let options = Options::default();
///
/// // Single time point
/// let results = parse("tomorrow", &locale, &[DimensionKind::Time], &context, &options);
/// if let DimensionValue::Time(TimeValue::Single { value: TimePoint::Naive { value, grain }, .. }) = &results[0].value {
///     assert_eq!(*value, NaiveDate::from_ymd_opt(2013, 2, 13).unwrap().and_hms_opt(0, 0, 0).unwrap());
///     assert_eq!(*grain, Grain::Day);
/// } else { panic!("expected Naive time point"); }
///
/// // Time interval
/// let results = parse("from 3pm to 5pm", &locale, &[DimensionKind::Time], &context, &options);
/// if let DimensionValue::Time(TimeValue::Interval { from: Some(f), to: Some(t), .. }) = &results[0].value {
///     assert!(matches!(f, TimePoint::Naive { value, grain: Grain::Hour }
///         if *value == NaiveDate::from_ymd_opt(2013, 2, 12).unwrap().and_hms_opt(15, 0, 0).unwrap()));
///     assert!(matches!(t, TimePoint::Naive { value, grain: Grain::Hour }
///         if *value == NaiveDate::from_ymd_opt(2013, 2, 12).unwrap().and_hms_opt(18, 0, 0).unwrap()));
/// } else { panic!("expected Interval time value"); }
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum TimeValue {
    /// A single time point with additional future occurrences.
    /// Matches Haskell's `TimeValue (SimpleValue v) [v1, v2, v3] holiday`.
    Single {
        /// The primary resolved time point.
        value: TimePoint,
        /// Up to 3 next occurrences (including the primary as first element).
        values: Vec<TimePoint>,
        /// Matches Haskell's `holiday :: Maybe Text` in TimeValue.
        /// Serialized as `"holidayBeta"` when present.
        #[serde(skip_serializing_if = "Option::is_none", rename = "holidayBeta")]
        holiday: Option<String>,
    },
    /// A time interval with additional future occurrences.
    Interval {
        /// The start of the interval, if bounded.
        from: Option<TimePoint>,
        /// The end of the interval, if bounded.
        to: Option<TimePoint>,
        /// Up to 3 next interval occurrences.
        values: Vec<IntervalEndpoints>,
        /// Matches Haskell's `holiday :: Maybe Text` in TimeValue.
        /// Serialized as `"holidayBeta"` when present.
        #[serde(skip_serializing_if = "Option::is_none", rename = "holidayBeta")]
        holiday: Option<String>,
    },
}

/// The resolved value of a parsed entity.
///
/// ```
/// use duckling::{parse_en, DimensionKind, DimensionValue, Grain};
///
/// assert_eq!(parse_en("thirty three", &[DimensionKind::Numeral])[0].value,
///     DimensionValue::Numeral(33.0));
///
/// assert_eq!(parse_en("the 3rd", &[DimensionKind::Ordinal])[0].value,
///     DimensionValue::Ordinal(3));
///
/// assert_eq!(parse_en("3 days", &[DimensionKind::Duration])[0].value,
///     DimensionValue::Duration { value: 3, grain: Grain::Day, normalized_seconds: 259200 });
///
/// assert_eq!(parse_en("user@example.com", &[DimensionKind::Email])[0].value,
///     DimensionValue::Email("user@example.com".into()));
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum DimensionValue {
    /// A numeric value.
    Numeral(f64),
    /// An ordinal value.
    Ordinal(i64),
    /// A temperature measurement.
    Temperature(MeasurementValue),
    /// A distance measurement.
    Distance(MeasurementValue),
    /// A volume measurement.
    Volume(MeasurementValue),
    /// A quantity with an optional product name.
    Quantity {
        /// The measurement value.
        measurement: MeasurementValue,
        /// The product (e.g. "sugar" in "5 pounds of sugar").
        product: Option<String>,
    },
    /// An amount of money.
    AmountOfMoney(MeasurementValue),
    /// An email address.
    Email(String),
    /// A phone number.
    PhoneNumber(String),
    /// A URL.
    Url {
        /// The full URL.
        value: String,
        /// The domain.
        domain: String,
    },
    /// A credit card number.
    CreditCardNumber {
        /// The card number.
        value: String,
        /// The card issuer (e.g. "visa", "mastercard").
        issuer: String,
    },
    /// A time grain.
    TimeGrain(Grain),
    /// A duration.
    Duration {
        /// The count (e.g. 3 in "3 days").
        value: i64,
        /// The grain (e.g. Day in "3 days").
        grain: Grain,
        /// The duration normalized to seconds.
        normalized_seconds: i64,
    },
    /// A time or date.
    Time(TimeValue),
}

impl DimensionValue {
    /// Returns the [`DimensionKind`] for this value.
    pub fn dim_kind(&self) -> DimensionKind {
        match self {
            DimensionValue::Numeral(_) => DimensionKind::Numeral,
            DimensionValue::Ordinal(_) => DimensionKind::Ordinal,
            DimensionValue::Temperature(_) => DimensionKind::Temperature,
            DimensionValue::Distance(_) => DimensionKind::Distance,
            DimensionValue::Volume(_) => DimensionKind::Volume,
            DimensionValue::Quantity { .. } => DimensionKind::Quantity,
            DimensionValue::AmountOfMoney(_) => DimensionKind::AmountOfMoney,
            DimensionValue::Email(_) => DimensionKind::Email,
            DimensionValue::PhoneNumber(_) => DimensionKind::PhoneNumber,
            DimensionValue::Url { .. } => DimensionKind::Url,
            DimensionValue::CreditCardNumber { .. } => DimensionKind::CreditCardNumber,
            DimensionValue::TimeGrain(_) => DimensionKind::TimeGrain,
            DimensionValue::Duration { .. } => DimensionKind::Duration,
            DimensionValue::Time(_) => DimensionKind::Time,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum TokenData {
    Numeral(NumeralData),
    Ordinal(OrdinalData),
    Temperature(TemperatureData),
    Distance(DistanceData),
    Volume(VolumeData),
    Quantity(QuantityData),
    AmountOfMoney(AmountOfMoneyData),
    Email(EmailData),
    PhoneNumber(PhoneNumberData),
    Url(UrlData),
    CreditCardNumber(CreditCardNumberData),
    TimeGrain(Grain),
    Duration(DurationData),
    Time(TimeData),
    RegexMatch(RegexMatchData),
}

impl TokenData {
    pub(crate) fn dimension_kind(&self) -> Option<DimensionKind> {
        match self {
            TokenData::Numeral(_) => Some(DimensionKind::Numeral),
            TokenData::Ordinal(_) => Some(DimensionKind::Ordinal),
            TokenData::Temperature(_) => Some(DimensionKind::Temperature),
            TokenData::Distance(_) => Some(DimensionKind::Distance),
            TokenData::Volume(_) => Some(DimensionKind::Volume),
            TokenData::Quantity(_) => Some(DimensionKind::Quantity),
            TokenData::AmountOfMoney(_) => Some(DimensionKind::AmountOfMoney),
            TokenData::Email(_) => Some(DimensionKind::Email),
            TokenData::PhoneNumber(_) => Some(DimensionKind::PhoneNumber),
            TokenData::Url(_) => Some(DimensionKind::Url),
            TokenData::CreditCardNumber(_) => Some(DimensionKind::CreditCardNumber),
            TokenData::TimeGrain(_) => Some(DimensionKind::TimeGrain),
            TokenData::Duration(_) => Some(DimensionKind::Duration),
            TokenData::Time(_) => Some(DimensionKind::Time),
            TokenData::RegexMatch(_) => None,
        }
    }

    pub(crate) fn is_latent(&self) -> bool {
        match self {
            TokenData::AmountOfMoney(data) => data.latent,
            TokenData::Time(data) => data.latent,
            _ => false,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct RegexMatchData {
    pub(crate) groups: Vec<Option<String>>,
}

impl RegexMatchData {
    pub(crate) fn group(&self, idx: usize) -> Option<&str> {
        self.groups.get(idx).and_then(|g| g.as_deref())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct Range {
    pub(crate) start: usize,
    pub(crate) end: usize,
}

impl Range {
    pub(crate) fn new(start: usize, end: usize) -> Self {
        Range { start, end }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Node {
    pub(crate) range: Range,
    pub(crate) token_data: TokenData,
    pub(crate) children: Vec<Rc<Node>>,
    pub(crate) rule_name: Option<String>,
}

impl Node {
    pub(crate) fn new(range: Range, token_data: TokenData) -> Self {
        Node {
            range,
            token_data,
            children: Vec::new(),
            rule_name: None,
        }
    }
}

pub(crate) type Predicate = Box<dyn Fn(&TokenData) -> bool + Send + Sync>;
pub(crate) type Production = Box<dyn Fn(&[&Node]) -> Option<TokenData> + Send + Sync>;

pub(crate) enum PatternItem {
    Regex(regex::Regex),
    Dimension(DimensionKind),
    Predicate(Predicate),
}

impl fmt::Debug for PatternItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PatternItem::Regex(r) => write!(f, "Regex({})", r.as_str()),
            PatternItem::Dimension(d) => write!(f, "Dimension({:?})", d),
            PatternItem::Predicate(_) => write!(f, "Predicate(...)"),
        }
    }
}

pub(crate) struct Rule {
    pub(crate) name: String,
    pub(crate) pattern: Vec<PatternItem>,
    pub(crate) production: Production,
}

impl fmt::Debug for Rule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Rule")
            .field("name", &self.name)
            .field("pattern", &self.pattern)
            .finish()
    }
}

/// Internal resolved token pairing a parse Node with its resolved Entity.
/// Matches Haskell's `Resolved { range, node, rval, isLatent }`.
#[derive(Debug, Clone)]
pub(crate) struct ResolvedToken {
    pub(crate) node: Node,
    pub(crate) entity: Entity,
}

/// A parsed entity extracted from text, with its position, matched text, and resolved value.
///
/// ```
/// use duckling::{parse_en, Entity, DimensionKind, DimensionValue};
///
/// assert_eq!(parse_en("I need 42 widgets", &[DimensionKind::Numeral]), vec![Entity {
///     body: "42".into(), start: 7, end: 9, latent: Some(false),
///     value: DimensionValue::Numeral(42.0),
/// }]);
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Entity {
    /// The matched text.
    pub body: String,
    /// Byte offset of the match start.
    pub start: usize,
    /// Byte offset of the match end.
    pub end: usize,
    /// The resolved structured value.
    pub value: DimensionValue,
    /// Whether this is a latent (ambiguous) match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latent: Option<bool>,
}