Skip to content

Commit

Permalink
fix(clafrica): improve predications (#83)
Browse files Browse the repository at this point in the history
Now, an element in a dictionary can have many values.
A translator can return a list of predicates.
  • Loading branch information
pythonbrad authored Sep 15, 2023
1 parent 378bcdd commit 77ff7c4
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
*.swp
*.un
*.swo
*.swn
*.swm
2 changes: 1 addition & 1 deletion clafrica/data/config_sample.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Main clafrica config file

[core]
buffer_size = 12
buffer_size = 64
auto_capitalize = false
auto_commit = false
page_size = 10
Expand Down
1 change: 1 addition & 0 deletions clafrica/data/dictionary.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[translation]
halo = "hello"
hola = { values = ["hello", "hi"], alias = [] }
hi = { value = "hello", alias = ["hey"] }
4 changes: 4 additions & 0 deletions clafrica/data/invalid_data.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# invalid
[data]
a = ["b", "c"]

File renamed without changes.
4 changes: 4 additions & 0 deletions clafrica/data/invalid_translator.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# invalid
[translators]
a = ["b", "c"]

2 changes: 1 addition & 1 deletion clafrica/data/scripts/datetime/date.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn translate(input) {
let date = parse_date(data);

if !date.is_empty() {
return [input, "", `${date[0]} ${global::MONTHS[date[1]]} ${date[2]}`, true];
return [input, "", [`${date[0]} ${global::MONTHS[date[1]]} ${date[2]}`, `${global::MONTHS[date[1]]} ${date[0]} ${date[2]}`], true];
}

return [input, "", "", false];
Expand Down
40 changes: 32 additions & 8 deletions clafrica/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rhai::{Engine, AST};
use serde::Deserialize;
use std::result::Result;
use std::{collections::HashMap, error, fs, path::Path};
use toml::{self};

Expand All @@ -23,8 +24,10 @@ pub struct CoreConfig {
#[serde(untagged)]
enum Data {
Simple(String),
Multi(Vec<String>),
File(DataFile),
Detailed(DetailedData),
MoreDetailed(MoreDetailedData),
}

#[derive(Deserialize, Debug, Clone)]
Expand All @@ -38,6 +41,12 @@ struct DetailedData {
alias: Vec<String>,
}

#[derive(Deserialize, Debug, Clone)]
struct MoreDetailedData {
values: Vec<String>,
alias: Vec<String>,
}

macro_rules! insert_with_auto_capitalize {
( $data: expr, $auto_capitalize: expr, $key: expr, $value: expr ) => {
$data.insert($key.to_owned(), Data::Simple($value.to_owned()));
Expand Down Expand Up @@ -83,6 +92,7 @@ impl Config {
insert_with_auto_capitalize!(data, auto_capitalize, key, value);
});
}
_ => Err(format!("Invalid script file `{filepath:?}`.\nCaused by:\n\t{value:?} not allowed in the data table."))?,
};
Ok(())
},
Expand All @@ -104,7 +114,7 @@ impl Config {
let filepath = config_path.join(v.clone()).to_str().unwrap().to_string();
translators.insert(key.to_owned(), Data::Simple(filepath));
}
_ => (),
_ => Err(format!("Invalid script file `{filepath:?}`.\nCaused by:\n\t{value:?} not allowed in the translator table."))?,
};
Ok(())
},
Expand All @@ -122,14 +132,19 @@ impl Config {
let conf = Config::from_file(&filepath)?;
translation.extend(conf.translation.unwrap_or_default());
}
Data::Simple(_) => {
Data::Simple(_) | Data::Multi(_) => {
translation.insert(key.to_owned(), value.clone());
}
Data::Detailed(DetailedData { value, alias }) => {
alias.iter().chain([key.to_owned()].iter()).for_each(|e| {
translation.insert(e.to_owned(), Data::Simple(value.to_owned()));
});
}
Data::MoreDetailed(MoreDetailedData { values, alias }) => {
alias.iter().chain([key.to_owned()].iter()).for_each(|e| {
translation.insert(e.to_owned(), Data::Multi(values.clone()));
});
}
};
Ok(())
},
Expand Down Expand Up @@ -189,7 +204,7 @@ impl Config {
.collect()
}

pub fn extract_translation(&self) -> HashMap<String, String> {
pub fn extract_translation(&self) -> HashMap<String, Vec<String>> {
let empty = HashMap::new();

self.translation
Expand All @@ -198,11 +213,12 @@ impl Config {
.iter()
.filter_map(|(k, v)| {
let v = match v {
Data::Simple(v) => Some(v),
Data::Simple(v) => Some(vec![v.to_owned()]),
Data::Multi(v) => Some(v.to_owned()),
_ => None,
};

v.map(|v| (k.to_owned(), v.to_owned()))
v.map(|v| (k.to_owned(), v))
})
.collect()
}
Expand All @@ -219,7 +235,7 @@ mod tests {

assert_eq!(
conf.core.as_ref().map(|core| {
assert_eq!(core.buffer_size.unwrap(), 12);
assert_eq!(core.buffer_size.unwrap(), 64);
assert!(!core.auto_capitalize.unwrap());
assert!(!core.auto_commit.unwrap());
assert_eq!(core.page_size.unwrap(), 10);
Expand All @@ -232,7 +248,7 @@ mod tests {
assert_eq!(data.keys().len(), 23);

// parsing error
let conf = Config::from_file(Path::new("./data/invalid.toml"));
let conf = Config::from_file(Path::new("./data/invalid_file.toml"));
assert!(conf.is_err());

// config file not found
Expand All @@ -243,6 +259,14 @@ mod tests {
let conf = Config::from_file(Path::new("./data/blank_sample.toml")).unwrap();
let data = conf.extract_data();
assert_eq!(data.keys().len(), 0);

// invalid data
let conf = Config::from_file(Path::new("./data/invalid_data.toml"));
assert!(conf.is_err());

// invalid translator
let conf = Config::from_file(Path::new("./data/invalid_translator.toml"));
assert!(conf.is_err());
}

#[test]
Expand Down Expand Up @@ -275,7 +299,7 @@ mod tests {

let conf = Config::from_file(Path::new("./data/config_sample.toml")).unwrap();
let translation = conf.extract_translation();
assert_eq!(translation.keys().len(), 3);
assert_eq!(translation.keys().len(), 4);

let conf = Config::from_file(Path::new("./data/blank_sample.toml")).unwrap();
let translation = conf.extract_translation();
Expand Down
14 changes: 8 additions & 6 deletions clafrica/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ pub fn run(

if !committed {
translator.translate(&input).iter().for_each(
|(code, remaining_code, text, translated)| {
if auto_commit && *translated {
processor.commit(code, remaining_code, text);
} else if !text.is_empty() {
frontend.add_predicate(code, remaining_code, text);
}
|(code, remaining_code, texts, translated)| {
texts.iter().for_each(|text| {
if auto_commit && *translated {
processor.commit(code, remaining_code, text);
} else if !text.is_empty() {
frontend.add_predicate(code, remaining_code, text);
}
});
},
);
};
Expand Down
16 changes: 11 additions & 5 deletions clafrica/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use rhai::{Array, Engine, Scope, AST};
use std::collections::HashMap;

pub struct Translator {
dictionary: HashMap<String, String>,
dictionary: HashMap<String, Vec<String>>,
translators: HashMap<String, AST>,
auto_commit: bool,
}

impl Translator {
pub fn new(
dictionary: HashMap<String, String>,
dictionary: HashMap<String, Vec<String>>,
translators: HashMap<String, AST>,
auto_commit: bool,
) -> Self {
Expand All @@ -20,7 +20,7 @@ impl Translator {
}
}

pub fn translate(&self, input: &str) -> Vec<(String, String, String, bool)> {
pub fn translate(&self, input: &str) -> Vec<(String, String, Vec<String>, bool)> {
let mut scope = Scope::new();
let engine = Engine::new();

Expand Down Expand Up @@ -48,10 +48,16 @@ impl Translator {
(data.len() == 4).then(|| {
let code = data[0].clone().into_string().unwrap();
let remaining_code = data[1].clone().into_string().unwrap();
let text = data[2].clone().into_string().unwrap();
let texts = data[2]
.clone()
.into_array()
.unwrap_or(vec![data[2].clone()])
.iter()
.map(|e| e.clone().into_string().unwrap())
.collect();
let translated = data[3].clone().as_bool().unwrap();

(code, remaining_code, text, translated)
(code, remaining_code, texts, translated)
})
}))
.collect()
Expand Down

0 comments on commit 77ff7c4

Please sign in to comment.