some renaming
This commit is contained in:
parent
e84484111c
commit
4ec6e32357
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,120 @@
|
|||
use std::fs;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct DictionaryEntry {
|
||||
hieroglyph: String,
|
||||
reading: String,
|
||||
translation: String,
|
||||
}
|
||||
|
||||
impl std::clone::Clone for DictionaryEntry {
|
||||
fn clone(&self) -> Self {
|
||||
Self { hieroglyph: self.hieroglyph.clone(), reading: self.reading.clone(), translation: self.translation.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DictionaryEntry {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}:{}:{}",
|
||||
self.hieroglyph, self.reading, self.translation
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sym_pos_or_panic(path: &String, line: &str, sym: &str, line_number: usize) -> usize {
|
||||
match line.find(sym) {
|
||||
Some(c) => c,
|
||||
None => panic!(
|
||||
"Failed to parse {} on line {}. Cannot find '{}'.",
|
||||
path, line_number, sym
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_and_parse(path: &String) -> Vec<DictionaryEntry> {
|
||||
let mut result: Vec<DictionaryEntry> = Vec::new();
|
||||
let content = fs::read_to_string(path).unwrap();
|
||||
|
||||
let content: Vec<&str> = content.split('\n').collect();
|
||||
for (line_number, line) in content.iter().enumerate() {
|
||||
if line == &"" {
|
||||
continue;
|
||||
}
|
||||
let hieroglyph_start = get_sym_pos_or_panic(path, line, "(", line_number) + 1;
|
||||
let hieroglyph_end = get_sym_pos_or_panic(path, line, ")", line_number);
|
||||
|
||||
let reading_start = get_sym_pos_or_panic(path, line, "[", line_number) + 1;
|
||||
let reading_end = get_sym_pos_or_panic(path, line, "]", line_number);
|
||||
|
||||
let translation_start = get_sym_pos_or_panic(path, line, "{", line_number) + 1;
|
||||
let translation_end = get_sym_pos_or_panic(path, line, "}", line_number);
|
||||
|
||||
let hieroglyph = line[hieroglyph_start..hieroglyph_end].to_string();
|
||||
let reading = if reading_start == reading_end {
|
||||
String::from("")
|
||||
} else {
|
||||
line[reading_start..reading_end].to_string()
|
||||
};
|
||||
let translation = line[translation_start..translation_end].to_string();
|
||||
|
||||
let entry = DictionaryEntry {
|
||||
hieroglyph,
|
||||
reading,
|
||||
translation,
|
||||
};
|
||||
|
||||
result.push(entry);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn deduplicate (entries: &Vec<DictionaryEntry>) -> Vec<DictionaryEntry>{
|
||||
|
||||
let mut deduplicated:Vec<DictionaryEntry> = Vec::new();
|
||||
|
||||
for entry in entries.iter() {
|
||||
let mut is_dup = false;
|
||||
|
||||
for e in deduplicated.iter() {
|
||||
if e.hieroglyph == entry.hieroglyph {
|
||||
is_dup = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if is_dup {
|
||||
let pos = deduplicated.iter().position(|x| x.hieroglyph == entry.hieroglyph).unwrap();
|
||||
deduplicated[pos].translation += &(String::from(" ; ") + &entry.translation);
|
||||
} else {
|
||||
deduplicated.push(entry.clone());
|
||||
}
|
||||
}
|
||||
|
||||
deduplicated
|
||||
}
|
||||
|
||||
pub fn import(path: &String) -> Vec<DictionaryEntry>{
|
||||
let dictionary = read_and_parse(path);
|
||||
let dictionary = deduplicate(&dictionary);
|
||||
dictionary
|
||||
}
|
||||
|
||||
// pub fn write_to_file(path: &String, dict: &Vec<Entry>) {
|
||||
// let mut output = OpenOptions::new()
|
||||
// .write(true)
|
||||
// .append(true)
|
||||
// .open("./reparsed-test.txt")
|
||||
// .unwrap();
|
||||
|
||||
// for entry in dict {
|
||||
// if let Err(e) = writeln!(
|
||||
// output,
|
||||
// "({}) [{}] {{{}}}",
|
||||
// entry.hieroglyph, entry.reading, entry.translation
|
||||
// ) {
|
||||
// eprintln!("Couldn't write to file: {}", e);
|
||||
// }
|
||||
// }
|
||||
// }
|
Loading…
Reference in New Issue