This repository has been archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
pub fn convert_to_mltt( | ||
text_to_convert: &str, | ||
map: &HashMap<String, String>, | ||
) -> Result<String, Box<dyn Error>> { | ||
let mut text_to_convert = text_to_convert.to_owned(); | ||
let mut keys = map.keys().map(String::from).collect::<Vec<_>>(); | ||
keys.sort_by(|a, b| b.len().cmp(&a.len())); | ||
let to_string_vec = |x: Vec<&str>| x.into_iter().map(String::from).collect::<Vec<_>>(); | ||
let _right_combinators = to_string_vec(vec!["ാ", "ി", "ീ", "ു", "ൂ", "ൃ", "ൄ", "ൌ", "്"]); | ||
let left_combinators = to_string_vec(vec!["െ", "േ", "ൈ"]); | ||
let combinators = to_string_vec(vec!["ൊ", "ോ"]); | ||
|
||
for key in combinators { | ||
if let Some(value) = map.get(&key) { | ||
while let Some(index) = text_to_convert.find(&key) { | ||
let middle_char = text_to_convert[index - 3..].chars().next().unwrap(); | ||
let mid_val = map.get(&format!("{}", middle_char)).unwrap(); | ||
let split_val = value.split("").collect::<Vec<_>>(); | ||
let new_key = format!("{}{}", middle_char, key); | ||
let new_val = format!("{}{}{}", split_val[1], mid_val, split_val[2]); | ||
text_to_convert = text_to_convert.replace(&new_key, &new_val); | ||
} | ||
} | ||
} | ||
|
||
for key in left_combinators { | ||
if let Some(value) = map.get(&key) { | ||
while let Some(index) = text_to_convert.find(&key) { | ||
let right_char = text_to_convert[index - 3..].chars().next().unwrap(); | ||
let right_val = map.get(&format!("{}", right_char)).unwrap(); | ||
let new_key = format!("{}{}", right_char, key); | ||
let new_val = format!("{}{}", value, right_val); | ||
text_to_convert = text_to_convert.replace(&new_key, &new_val); | ||
} | ||
} else { | ||
eprintln!("{:#?} not found in the map!", key); | ||
} | ||
} | ||
|
||
for key in keys { | ||
if let Some(value) = map.get(&key) { | ||
text_to_convert = text_to_convert.replace(&key, value); | ||
} else { | ||
eprintln!("{:#?} not found in the map!", key); | ||
} | ||
} | ||
Ok(text_to_convert) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::map_parser::{create_unicode_to_mltt_map, parse_content}; | ||
|
||
fn generate_map(content: &str) -> Result<HashMap<String, String>, Box<dyn Error>> { | ||
let content = parse_content(&content)?; | ||
create_unicode_to_mltt_map(&content) | ||
} | ||
|
||
#[test] | ||
fn simple_convert() -> Result<(), Box<dyn Error>> { | ||
assert_eq!( | ||
convert_to_mltt("abc", &generate_map("a=a\nb=c\nc=d")?)?, | ||
"abb" | ||
); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,3 @@ fn main() -> Result<(), Box<dyn Error>> { | |
|
||
Ok(()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
pub fn parse_content(content: &str) -> Result<Vec<Vec<String>>, Box<dyn Error>> { | ||
let content = content | ||
.split("\n") | ||
.filter(|x| x.len() >= 1 && x.chars().nth(0) != Some('#') && x.contains("=")) | ||
.map(|x| { | ||
x.split("=") | ||
.map(|s| s.trim()) | ||
.map(String::from) | ||
.collect::<Vec<_>>() | ||
}) | ||
.filter(|x| x[1].len() >= 1 && x[0].len() >= 1) | ||
.collect::<Vec<_>>(); | ||
Ok(content) | ||
} | ||
|
||
pub fn create_unicode_to_mltt_map( | ||
content: &Vec<Vec<String>>, | ||
) -> Result<HashMap<String, String>, Box<dyn Error>> { | ||
let map = content | ||
.into_iter() | ||
.map(|s| (s[1].to_owned(), s[0].to_owned())) | ||
.collect::<HashMap<_, _>>(); | ||
Ok(map) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn simple_parse_content() -> Result<(), Box<dyn Error>> { | ||
assert_eq!(parse_content("a=a")?, vec![vec!["a", "a"]]); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn complex_parse_content() -> Result<(), Box<dyn Error>> { | ||
assert_eq!( | ||
parse_content( | ||
r#"# This is a comment string should not be parsed. | ||
a=a | ||
# b = b | ||
b = c | ||
c = d | ||
a | ||
b | ||
c | ||
."# | ||
)?, | ||
vec![vec!["a", "a"], vec!["b", "c"], vec!["c", "d"],] | ||
); | ||
Ok(()) | ||
} | ||
|
||
fn get_parsed_content(dummy: Vec<Vec<&str>>) -> Vec<Vec<String>> { | ||
dummy | ||
.into_iter() | ||
.map(|x| x.into_iter().map(String::from).collect::<Vec<_>>()) | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
#[test] | ||
fn simple_create_unicode_to_mltt_map() -> Result<(), Box<dyn Error>> { | ||
let content = get_parsed_content(vec![vec!["a", "a"], vec!["b", "c"], vec!["c", "d"]]); | ||
let mut expected_map: HashMap<String, String> = HashMap::new(); | ||
expected_map.insert(String::from("a"), String::from("a")); | ||
expected_map.insert(String::from("c"), String::from("b")); | ||
expected_map.insert(String::from("d"), String::from("c")); | ||
assert_eq!(create_unicode_to_mltt_map(&content)?, expected_map); | ||
Ok(()) | ||
} | ||
} |