whichtime_sys/parsers/nl/
casual_time.rs1use crate::components::Component;
8use crate::context::ParsingContext;
9use crate::error::Result;
10use crate::parsers::Parser;
11use crate::results::ParsedResult;
12use crate::types::Meridiem;
13use chrono::{Datelike, Duration};
14use fancy_regex::Regex;
15use std::sync::LazyLock;
16
17static PATTERN: LazyLock<Regex> = LazyLock::new(|| {
18 Regex::new(r"(?i)(?<![a-zA-Z])(middag|middernacht|ochtend|avond|nacht)(?=\W|$)").unwrap()
19});
20
21const TIME_GROUP: usize = 1;
22
23pub struct NLCasualTimeParser;
25
26impl NLCasualTimeParser {
27 pub fn new() -> Self {
28 Self
29 }
30}
31
32impl Parser for NLCasualTimeParser {
33 fn name(&self) -> &'static str {
34 "NLCasualTimeParser"
35 }
36
37 fn should_apply(&self, _context: &ParsingContext) -> bool {
38 true
39 }
40
41 fn parse(&self, context: &ParsingContext) -> Result<Vec<ParsedResult>> {
42 let mut results = Vec::new();
43 let ref_date = context.reference.instant;
44
45 let mut start = 0;
46 while start < context.text.len() {
47 let search_text = &context.text[start..];
48 let captures = match PATTERN.captures(search_text) {
49 Ok(Some(caps)) => caps,
50 Ok(None) => break,
51 Err(_) => break,
52 };
53
54 let full_match = match captures.get(0) {
55 Some(m) => m,
56 None => break,
57 };
58
59 let match_start = start + full_match.start();
60 let match_end = start + full_match.end();
61
62 let time_keyword = captures
63 .get(TIME_GROUP)
64 .map(|m| m.as_str().to_lowercase())
65 .unwrap_or_default();
66
67 let mut components = context.create_components();
68
69 let mut target_date = ref_date;
71
72 match time_keyword.as_str() {
73 "ochtend" => {
74 components.imply(Component::Hour, 6);
75 components.imply(Component::Minute, 0);
76 components.assign(Component::Meridiem, Meridiem::AM as i32);
77 }
78 "middag" => {
79 components.imply(Component::Hour, 12);
81 components.imply(Component::Minute, 0);
82 components.assign(Component::Meridiem, Meridiem::AM as i32);
84 }
85 "avond" => {
86 components.imply(Component::Hour, 20);
87 components.imply(Component::Minute, 0);
88 components.assign(Component::Meridiem, Meridiem::PM as i32);
89 }
90 "nacht" => {
91 components.imply(Component::Hour, 22);
92 components.imply(Component::Minute, 0);
93 components.assign(Component::Meridiem, Meridiem::PM as i32);
94 }
95 "middernacht" => {
96 target_date = ref_date + Duration::days(1);
100
101 components.imply(Component::Hour, 0);
102 components.imply(Component::Minute, 0);
103 components.imply(Component::Second, 0);
104 }
105 _ => {
106 start = match_end;
107 continue;
108 }
109 }
110
111 components.assign(Component::Year, target_date.year());
112 components.assign(Component::Month, target_date.month() as i32);
113 components.assign(Component::Day, target_date.day() as i32);
114
115 results.push(context.create_result(match_start, match_end, components, None));
116
117 start = match_end;
118 }
119
120 Ok(results)
121 }
122}
123
124impl Default for NLCasualTimeParser {
125 fn default() -> Self {
126 Self::new()
127 }
128}