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"
|
serde_json = "1.0"
|
||||||
# Regex support
|
# Regex support
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
@ -12,6 +12,9 @@
|
|||||||
//! [original software](https://github.com/ceronyon/LangEvolve) which applies
|
//! [original software](https://github.com/ceronyon/LangEvolve) which applies
|
||||||
//! user-defined sound changes to words and texts based on regex expressions.
|
//! user-defined sound changes to words and texts based on regex expressions.
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate simplelog;
|
extern crate simplelog;
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate serde_yaml;
|
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
|
// TODO break categories in different rules
|
||||||
pub fn update(
|
pub fn update(
|
||||||
&self,
|
&self,
|
||||||
categories: &std::collections::HashMap<String, String>,
|
categories: &HashMap<String, String>,
|
||||||
) -> std::result::Result<Rule, String> {
|
) -> std::result::Result<Rule, String> {
|
||||||
let mut rule = self.clone();
|
let mut rule = self.clone();
|
||||||
let re = Regex::new("%\\D");
|
let (from_match, to_match) = self.detect_categories();
|
||||||
let from_match = re.is_match(&self.from.as_str());
|
// If there are only simple rewrites to make in the from String
|
||||||
let to_match = re.is_match(&self.to.as_str());
|
if from_match > 0 && to_match == 0 {
|
||||||
if from_match && !to_match {
|
rule = self.simple_rewrite(&categories);
|
||||||
for (category, content) in categories {
|
|
||||||
rule.from = Regex::new(
|
|
||||||
rule.from
|
|
||||||
.to_string()
|
|
||||||
.replace(
|
|
||||||
format!("%{}", category).as_str(),
|
|
||||||
format!("[{}]", content).as_str(),
|
|
||||||
)
|
|
||||||
.as_str(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are equivalences between from and to
|
||||||
|
if from_match > 0 && to_match <= from_match && to_match > 0 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Ok(rule)
|
Ok(rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user