Split Rule::update
in several functions
Also optimization with a static Regex
This commit is contained in:
parent
071c8b0728
commit
042fd066f0
@ -16,3 +16,4 @@ serde_yaml = "0.7"
|
||||
serde_json = "1.0"
|
||||
# Regex support
|
||||
regex = "1.3"
|
||||
lazy_static = "1.4.0"
|
||||
|
@ -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;
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate serde_yaml;
|
||||
@ -31,16 +33,23 @@ impl Rule {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO break categories in different rules
|
||||
pub fn update(
|
||||
&self,
|
||||
categories: &std::collections::HashMap<String, String>,
|
||||
) -> std::result::Result<Rule, String> {
|
||||
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();
|
||||
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
|
||||
@ -49,10 +58,29 @@ impl Rule {
|
||||
format!("%{}", category).as_str(),
|
||||
format!("[{}]", content).as_str(),
|
||||
)
|
||||
.as_str(),
|
||||
.as_str()
|
||||
);
|
||||
}
|
||||
rule
|
||||
}
|
||||
|
||||
// TODO break categories in different rules
|
||||
pub fn update(
|
||||
&self,
|
||||
categories: &HashMap<String, String>,
|
||||
) -> std::result::Result<Rule, String> {
|
||||
let mut rule = self.clone();
|
||||
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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user