shodh_memory/query_parsing/
parser_trait.rs1use chrono::{DateTime, NaiveDate, Utc};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ParsedQuery {
12 pub original: String,
14
15 pub entities: Vec<Entity>,
17
18 pub events: Vec<Event>,
20
21 pub modifiers: Vec<String>,
23
24 pub temporal: TemporalInfo,
26
27 pub is_attribute_query: bool,
29
30 pub attribute: Option<AttributeQuery>,
32
33 pub compounds: Vec<String>,
35
36 pub ic_weights: HashMap<String, f32>,
38
39 pub confidence: f32,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct Entity {
46 pub text: String,
48 pub stem: String,
50 pub entity_type: EntityType,
52 pub ic_weight: f32,
54 pub negated: bool,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
60pub enum EntityType {
61 Person,
62 Place,
63 Thing,
64 Event,
65 Time,
66 Unknown,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct Event {
72 pub text: String,
74 pub stem: String,
76 pub ic_weight: f32,
78}
79
80#[derive(Debug, Clone, Default, Serialize, Deserialize)]
82pub struct TemporalInfo {
83 pub has_temporal_intent: bool,
85
86 pub intent: TemporalIntent,
88
89 pub relative_refs: Vec<RelativeTimeRef>,
91
92 pub resolved_dates: Vec<NaiveDate>,
94
95 pub absolute_dates: Vec<NaiveDate>,
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
101pub enum TemporalIntent {
102 WhenQuestion,
104 SpecificTime,
106 Ordering,
108 Duration,
110 #[default]
112 None,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct RelativeTimeRef {
118 pub text: String,
120 pub resolved: Option<NaiveDate>,
122 pub direction: TimeDirection,
124 pub unit: TimeUnit,
126 pub offset: i32,
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
132pub enum TimeDirection {
133 Past,
134 Future,
135 Current,
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
140pub enum TimeUnit {
141 Day,
142 Week,
143 Month,
144 Year,
145 Unknown,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct AttributeQuery {
151 pub entity: String,
153 pub attribute: String,
155 pub synonyms: Vec<String>,
157}
158
159pub trait QueryParser: Send + Sync {
161 fn parse(&self, query: &str, context_date: Option<DateTime<Utc>>) -> ParsedQuery;
170
171 fn name(&self) -> &'static str;
173
174 fn is_available(&self) -> bool {
176 true
177 }
178}
179
180impl ParsedQuery {
181 pub fn empty(original: &str) -> Self {
183 Self {
184 original: original.to_string(),
185 entities: Vec::new(),
186 events: Vec::new(),
187 modifiers: Vec::new(),
188 temporal: TemporalInfo::default(),
189 is_attribute_query: false,
190 attribute: None,
191 compounds: Vec::new(),
192 ic_weights: HashMap::new(),
193 confidence: 0.0,
194 }
195 }
196
197 pub fn entity_texts(&self) -> Vec<&str> {
199 self.entities.iter().map(|e| e.text.as_str()).collect()
200 }
201
202 pub fn event_stems(&self) -> Vec<&str> {
204 self.events.iter().map(|e| e.stem.as_str()).collect()
205 }
206
207 pub fn is_temporal_query(&self) -> bool {
209 self.temporal.has_temporal_intent
210 }
211}