feat: introduce querying card with partial match using regex

This commit is contained in:
tretrauit 2024-01-08 12:44:04 +07:00
parent 872e551157
commit 0f9331ba60
2 changed files with 64 additions and 5 deletions

View File

@ -25,7 +25,7 @@ pub fn init() {
.unwrap();
}
pub async fn query_card(name: &str, series: &str) -> Option<Card> {
pub async fn query_card(name: &String, series: &String) -> Option<Card> {
KATANA
.get()
.unwrap()
@ -40,6 +40,51 @@ pub async fn query_card(name: &str, series: &str) -> Option<Card> {
.unwrap()
}
pub async fn query_card_regex(name: &String, series: &String) -> Option<Card> {
let mut name_regex = String::new();
let mut ascii_name = String::new();
for c in name.chars() {
if c.is_ascii_alphanumeric() {
ascii_name.push(c);
} else {
ascii_name.push(' ');
}
}
ascii_name.split_whitespace().for_each(|word| {
name_regex.push_str("(?=.*\\b");
name_regex.push_str(word.to_lowercase().as_str());
name_regex.push_str("\\b)");
});
name_regex.push_str(".+");
let mut series_regex = String::new();
let mut ascii_series = String::new();
for c in series.chars() {
if c.is_ascii_alphanumeric() {
ascii_series.push(c);
} else {
ascii_series.push(' ');
}
}
ascii_series.split_whitespace().for_each(|word| {
series_regex.push_str("(?=.*\\b");
series_regex.push_str(word.to_lowercase().as_str());
series_regex.push_str("\\b)");
});
series_regex.push_str(".+");
KATANA
.get()
.unwrap()
.find_one(
mongodb::bson::doc! {
"name": {"$regex": name_regex, "$options" : "i"},
"series": {"$regex": series_regex, "$options" : "i"}
},
None,
)
.await
.unwrap()
}
pub async fn write_card(mut card: Card) -> Result<(), String> {
let old_card = KATANA
.get()

View File

@ -253,11 +253,18 @@ pub async fn analyze_card_libtesseract(card: image::DynamicImage, count: u32) ->
last_update_ts: 0,
};
// Read the wishlist number
match db::query_card(&card.name.as_str(), &card.series.as_str()).await {
match db::query_card(&card.name, &card.series).await {
Some(c) => {
card = c;
}
None => {}
None => {
match db::query_card_regex(&card.name, &card.series).await {
Some(c) => {
card = c;
}
None => {}
}
}
}
card
}
@ -312,11 +319,18 @@ pub async fn analyze_card_subprocess(card: image::DynamicImage, count: u32) -> C
last_update_ts: 0,
};
// Read the wishlist number
match db::query_card(&card.name.as_str(), &card.series.as_str()).await {
match db::query_card(&card.name, &card.series).await {
Some(c) => {
card = c;
}
None => {}
None => {
match db::query_card_regex(&card.name, &card.series).await {
Some(c) => {
card = c;
}
None => {}
}
}
}
card
}