Skip to content

Commit

Permalink
improve opening name search
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 5, 2024
1 parent dfb5c82 commit 0a7337b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
55 changes: 26 additions & 29 deletions src-tauri/src/opening.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use log::info;
use serde::{Deserialize, Serialize, ser::SerializeStruct};
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use shakmaty::{fen::Fen, san::San, Chess, EnPassantMode, Position, Setup};

use lazy_static::lazy_static;
use strsim::jaro_winkler;
use strsim::{jaro_winkler, sorensen_dice};

use crate::error::Error;

Expand Down Expand Up @@ -68,33 +68,30 @@ pub fn get_opening_from_setup(setup: Setup) -> Result<String, Error> {

#[tauri::command]
pub async fn search_opening_name(query: String) -> Result<Vec<Opening>, Error> {
let mut best_matches: Vec<(Opening, f64)> = Vec::new();

for opening in OPENINGS.iter() {
if best_matches.iter().any(|(m, _)| m.name == opening.name) {
continue;
}

let score = jaro_winkler(&query, &opening.name);

if best_matches.len() < 15 {
best_matches.push((opening.clone(), score));
best_matches.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
} else if let Some(min_score) = best_matches.last().map(|(_, s)| *s) {
if score > min_score {
best_matches.pop();
best_matches.push((opening.clone(), score));
best_matches.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
}
}
}

if !best_matches.is_empty() {
let best_matches_names = best_matches.iter().map(|(o, _)| o.clone()).collect();
Ok(best_matches_names)
} else {
Err(Error::NoMatchFound)
}
let lower_query = query.to_lowercase();
let scores = OPENINGS
.iter()
.map(|opening| {
let lower_name = opening.name.to_lowercase();
let sorenson_score = sorensen_dice(&lower_query, &lower_name);
let jaro_score = jaro_winkler(&lower_query, &lower_name);
let score = sorenson_score.max(jaro_score);
(opening.clone(), score)
})
.collect::<Vec<_>>();
let mut best_matches = scores
.into_iter()
.filter(|(_, score)| *score > 0.8)
.collect::<Vec<_>>();

best_matches.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());

let best_matches_names = best_matches
.iter()
.map(|(o, _)| o.clone())
.take(15)
.collect();
Ok(best_matches_names)
}

lazy_static! {
Expand Down
2 changes: 1 addition & 1 deletion src/components/panels/info/FenSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function FenSearch({ currentFen }: { currentFen: string }) {
const exactOptionMatch = data?.some((item) => item.fen === search);

const options = data?.map((item) => (
<Combobox.Option value={item.fen} key={item.name}>
<Combobox.Option value={item.fen} key={item.fen}>
<Group wrap="nowrap">
<div>
<Text size="sm">{item.name}</Text>
Expand Down

0 comments on commit 0a7337b

Please sign in to comment.