-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: avoid fuzzy searching garbage
- Loading branch information
Showing
4 changed files
with
183 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub mod fuzzy; | ||
pub mod search; | ||
|
||
use chrono::{DateTime, TimeZone}; | ||
use chrono_tz::Europe::Rome; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
use anyhow::{anyhow, Result}; | ||
use aws_sdk_dynamodb::{types::AttributeValue, Client as DynamoDbClient}; | ||
use std::collections::HashMap; | ||
|
||
use super::{stations, Stazione, UNKNOWN_VALUE}; | ||
|
||
fn fuzzy_search(search: &str) -> Option<String> { | ||
let stations = stations(); | ||
let closest_match = stations | ||
.iter() | ||
.map(|s: &String| { | ||
( | ||
s, | ||
edit_distance::edit_distance( | ||
&search.to_lowercase(), | ||
&s.replace(" ", "").to_lowercase(), | ||
), | ||
) | ||
}) | ||
.filter(|(_, score)| *score < 4) | ||
.min_by_key(|(_, score)| *score) | ||
.map(|(station, _)| station.clone()); // Map to String and clone the station name | ||
|
||
closest_match | ||
} | ||
|
||
pub async fn get_station( | ||
client: &DynamoDbClient, | ||
station_name: String, | ||
table_name: &str, | ||
) -> Result<Option<Stazione>> { | ||
if let Some(closest_match) = fuzzy_search(&station_name) { | ||
let result = client | ||
.get_item() | ||
.table_name(table_name) | ||
.key("nomestaz", AttributeValue::S(closest_match.clone())) | ||
.send() | ||
.await?; | ||
|
||
match result.item { | ||
Some(item) => { | ||
let idstazione = parse_string_field(&item, "idstazione")?; | ||
let timestamp = parse_number_field::<i64>(&item, "timestamp")?; | ||
let lon = parse_string_field(&item, "lon")?; | ||
let lat = parse_string_field(&item, "lat")?; | ||
let ordinamento = parse_number_field::<i32>(&item, "ordinamento")?; | ||
let nomestaz = parse_string_field(&item, "nomestaz")?; | ||
let soglia1 = parse_number_field::<f64>(&item, "soglia1")?; | ||
let soglia2 = parse_number_field::<f64>(&item, "soglia2")?; | ||
let soglia3 = parse_number_field::<f64>(&item, "soglia3")?; | ||
let value = parse_optional_number_field(&item, "value")?.unwrap_or(UNKNOWN_VALUE); | ||
|
||
Ok(Some(Stazione { | ||
timestamp, | ||
idstazione, | ||
ordinamento, | ||
nomestaz, | ||
lon, | ||
lat, | ||
soglia1, | ||
soglia2, | ||
soglia3, | ||
value, | ||
})) | ||
} | ||
None => Err(anyhow!("Station '{}' not found", closest_match)), | ||
} | ||
} else { | ||
Err(anyhow!("'{}' did not match any know station", station_name)) | ||
} | ||
} | ||
|
||
fn parse_string_field(item: &HashMap<String, AttributeValue>, field: &str) -> Result<String> { | ||
match item.get(field) { | ||
Some(AttributeValue::S(s)) => Ok(s.clone()), | ||
Some(AttributeValue::Ss(ss)) => Ok(ss.join(",")), // If the field is a string set | ||
_ => Err(anyhow!("Missing or invalid '{}' field", field)), | ||
} | ||
} | ||
|
||
fn parse_number_field<T: std::str::FromStr>( | ||
item: &HashMap<String, AttributeValue>, | ||
field: &str, | ||
) -> Result<T> | ||
where | ||
<T as std::str::FromStr>::Err: std::fmt::Display, | ||
{ | ||
match item.get(field) { | ||
Some(AttributeValue::N(n)) => n.parse::<T>().map_err(|e| { | ||
anyhow!( | ||
"Failed to parse '{}' field with value '{}' as number: {}", | ||
field, | ||
n, | ||
e | ||
) | ||
}), | ||
Some(AttributeValue::S(s)) => s.parse::<T>().map_err(|e| { | ||
anyhow!( | ||
"Failed to parse '{}' field with value '{}' as number: {}", | ||
field, | ||
s, | ||
e | ||
) | ||
}), | ||
_ => Err(anyhow!("Missing or invalid '{}' field", field)), | ||
} | ||
} | ||
|
||
fn parse_optional_number_field<T: std::str::FromStr>( | ||
item: &HashMap<String, AttributeValue>, | ||
field: &str, | ||
) -> Result<Option<T>> | ||
where | ||
<T as std::str::FromStr>::Err: std::fmt::Display, | ||
{ | ||
match item.get(field) { | ||
Some(AttributeValue::N(n)) => { | ||
if let Ok(value) = n.parse::<T>() { | ||
Ok(Some(value)) | ||
} else { | ||
Err(anyhow!( | ||
"Failed to parse '{}' field with value '{}' as number", | ||
field, | ||
n | ||
)) | ||
} | ||
} | ||
Some(AttributeValue::S(s)) => { | ||
if let Ok(value) = s.parse::<T>() { | ||
Ok(Some(value)) | ||
} else { | ||
Err(anyhow!( | ||
"Failed to parse '{}' field with value '{}' as number", | ||
field, | ||
s | ||
)) | ||
} | ||
} | ||
_ => Err(anyhow!("Invalid type for '{}' field", field)), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn fuzzy_search_cesena_yields_cesena_station() { | ||
let message = "cesena".to_string(); | ||
let expected = Some("Cesena".to_string()); | ||
|
||
assert_eq!(fuzzy_search(&message), expected); | ||
} | ||
|
||
#[test] | ||
fn fuzzy_search_scarlo_yields_scarlo_station() { | ||
let message = "scarlo".to_string(); | ||
let expected = Some("S. Carlo".to_string()); | ||
|
||
assert_eq!(fuzzy_search(&message), expected); | ||
} | ||
|
||
#[test] | ||
fn fuzzy_search_nonexisting_yields_nonexisting_station() { | ||
let message = "thisdoesnotexists".to_string(); | ||
let expected = None; | ||
|
||
assert_eq!(fuzzy_search(&message), expected); | ||
} | ||
|
||
#[test] | ||
fn fuzzy_search_ecsena_yields_cesena_station() { | ||
let message = "ecsena".to_string(); | ||
let expected = Some("Cesena".to_string()); | ||
|
||
assert_eq!(fuzzy_search(&message), expected); | ||
} | ||
} |