Skip to content

Commit

Permalink
feat: Divi: optimize sample data parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
shonya3 committed Aug 26, 2023
1 parent 02e6411 commit 847e55b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 170 deletions.
66 changes: 2 additions & 64 deletions divi/src/card_record.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::{
consts::{CARDS, CONDENSE_FACTOR, LEGACY_CARDS},
IsCard,
};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct FixedCardName {
pub old: String,
pub fixed: String,
}

impl FixedCardName {
pub fn new(old: &str, fixed: &str) -> FixedCardName {
FixedCardName {
old: String::from(old),
fixed: String::from(fixed),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct DivinationCardRecord {
pub name: String,
Expand Down Expand Up @@ -54,55 +37,10 @@ impl DivinationCardRecord {
self
}

pub fn set_weight(&mut self, weight_sample: f32) -> &mut Self {
self.weight = Some((weight_sample * self.amount as f32).powf(1.0 / CONDENSE_FACTOR));
pub fn set_weight(&mut self, weight_multiplier: f32) -> &mut Self {
self.weight = Some((weight_multiplier * self.amount as f32).powf(1.0 / CONDENSE_FACTOR));
self
}

fn most_similar_card(name: &str) -> (String, f64) {
let mut similarity_map = HashMap::<String, f64>::new();
for card in CARDS {
let similarity = strsim::normalized_damerau_levenshtein(&name, card);
similarity_map.insert(card.to_string(), similarity);
}

let most_similar = similarity_map
.iter()
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
.unwrap();

(most_similar.0.to_owned(), most_similar.1.to_owned())
}

pub fn fix_name(&mut self) -> Option<FixedCardName> {
match self.is_card() {
true => None,
false => self.fix_name_unchecked(),
}
}

pub fn fix_name_unchecked(&mut self) -> Option<FixedCardName> {
let (similar, score) = Self::most_similar_card(&self.name);
match score >= 0.75 {
true => {
let fixed_name = FixedCardName::new(&self.name, &similar);
self.name = similar;
Some(fixed_name)
}
false => {
let the_name = format!("The {}", &self.name);
let (similar, score) = Self::most_similar_card(&the_name);
match score >= 0.75 {
true => {
let fixed_name = FixedCardName::new(&self.name, &similar);
self.name = similar;
Some(fixed_name)
}
false => None,
}
}
}
}
}

impl IsCard for DivinationCardRecord {
Expand Down
1 change: 1 addition & 0 deletions divi/src/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;

/// Holds an array of cards with length equal to number of all divination cards(For example, 440 in 2.23 patch)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Cards(#[serde(with = "BigArray")] pub [DivinationCardRecord; CARDS_N]);

Expand Down
2 changes: 0 additions & 2 deletions divi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// #![allow(unused)]

use consts::{CARDS, LEGACY_CARDS};
pub mod card_record;
pub mod cards;
Expand Down
1 change: 1 addition & 0 deletions divi/src/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct DivinationCardPrice {
pub sparkline: Sparkline,
}

/// Holds an array of card prices with length equal to number of all divination cards(For example, 440 in 2.23 patch)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(transparent)]
pub struct Prices(#[serde(with = "BigArray")] pub [DivinationCardPrice; CARDS_N]);
Expand Down
Loading

0 comments on commit 847e55b

Please sign in to comment.