From 042fd066f02f35febcb1cba08d8704f47b551b7a Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sun, 5 Apr 2020 15:50:42 +0200 Subject: [PATCH] Split `Rule::update` in several functions Also optimization with a static Regex --- Cargo.toml | 3 +- src/lib.rs | 3 ++ src/settings/rule/mod.rs | 60 +++++++++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1530829..5baa8a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ serde = {version = "1.0", features = ["derive"]} serde_yaml = "0.7" serde_json = "1.0" # Regex support -regex = "1.3" \ No newline at end of file +regex = "1.3" +lazy_static = "1.4.0" diff --git a/src/lib.rs b/src/lib.rs index b92a50c..5b1f9d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ //! [original software](https://github.com/ceronyon/LangEvolve) which applies //! user-defined sound changes to words and texts based on regex expressions. +#[macro_use] +extern crate lazy_static; + use std::fs::File; extern crate log; extern crate simplelog; diff --git a/src/settings/rule/mod.rs b/src/settings/rule/mod.rs index fbbe245..a233fb5 100644 --- a/src/settings/rule/mod.rs +++ b/src/settings/rule/mod.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + extern crate serde; extern crate serde_json; extern crate serde_yaml; @@ -31,28 +33,54 @@ impl Rule { } } + fn detect_categories(&self) -> (u8, u8) { + lazy_static! { + static ref RE : Regex = Regex::new("%([A-Z])"); + } + let captures_from = match RE.captures(self.from.as_str()) { + None => 0 as u8, + Some(c) => c.len() as u8, + }; + let captures_to = match RE.captures(&self.to) { + None => 0 as u8, + Some(c) => c.len() as u8, + }; + (captures_from, captures_to) + } + + fn simple_rewrite(&self, categories: &HashMap) -> Self { + let mut rule = self.clone(); + for (category, content) in categories { + rule.from = Regex::new( + rule.from + .to_string() + .replace( + format!("%{}", category).as_str(), + format!("[{}]", content).as_str(), + ) + .as_str() + ); + } + rule + } + // TODO break categories in different rules pub fn update( &self, - categories: &std::collections::HashMap, + categories: &HashMap, ) -> std::result::Result { let mut rule = self.clone(); - let re = Regex::new("%\\D"); - let from_match = re.is_match(&self.from.as_str()); - let to_match = re.is_match(&self.to.as_str()); - if from_match && !to_match { - for (category, content) in categories { - rule.from = Regex::new( - rule.from - .to_string() - .replace( - format!("%{}", category).as_str(), - format!("[{}]", content).as_str(), - ) - .as_str(), - ); - } + let (from_match, to_match) = self.detect_categories(); + // If there are only simple rewrites to make in the from String + if from_match > 0 && to_match == 0 { + rule = self.simple_rewrite(&categories); } + + // If there are equivalences between from and to + if from_match > 0 && to_match <= from_match && to_match > 0 { + + } + Ok(rule) }