Split `Rule::update` in several functions

Also optimization with a static Regex
This commit is contained in:
Lucien Cartier-Tilet 2020-04-05 15:50:42 +02:00
parent 071c8b0728
commit 042fd066f0
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 49 additions and 17 deletions

View File

@ -15,4 +15,5 @@ serde = {version = "1.0", features = ["derive"]}
serde_yaml = "0.7"
serde_json = "1.0"
# Regex support
regex = "1.3"
regex = "1.3"
lazy_static = "1.4.0"

View File

@ -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;

View File

@ -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<String, String>) -> 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<String, String>,
categories: &HashMap<String, String>,
) -> std::result::Result<Rule, String> {
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)
}