some renaming
This commit is contained in:
		
							
								
								
									
										4822
									
								
								reparsed.txt
									
									
									
									
									
								
							
							
						
						
									
										4822
									
								
								reparsed.txt
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										117
									
								
								src/import.rs
									
									
									
									
									
								
							
							
						
						
									
										117
									
								
								src/import.rs
									
									
									
									
									
								
							@@ -1,117 +0,0 @@
 | 
				
			|||||||
use std::fs;
 | 
					 | 
				
			||||||
use std::fs::OpenOptions;
 | 
					 | 
				
			||||||
use std::io::prelude::*;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(PartialEq)]
 | 
					 | 
				
			||||||
struct Entry {
 | 
					 | 
				
			||||||
    hieroglyph: String,
 | 
					 | 
				
			||||||
    reading: String,
 | 
					 | 
				
			||||||
    translation: String,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl std::clone::Clone for Entry {
 | 
					 | 
				
			||||||
    fn clone(&self) -> Self {
 | 
					 | 
				
			||||||
        Self { hieroglyph: self.hieroglyph.clone(), reading: self.reading.clone(), translation: self.translation.clone() }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl std::fmt::Display for Entry {
 | 
					 | 
				
			||||||
    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<Entry> {
 | 
					 | 
				
			||||||
    let mut result: Vec<Entry> = 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 = Entry {
 | 
					 | 
				
			||||||
            hieroglyph,
 | 
					 | 
				
			||||||
            reading,
 | 
					 | 
				
			||||||
            translation,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        result.push(entry);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    result
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn deduplicate (entries: &Vec<Entry>) -> Vec<Entry>{
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let mut deduplicated:Vec<Entry> = 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) {
 | 
					 | 
				
			||||||
    let mut output = OpenOptions::new()
 | 
					 | 
				
			||||||
        .write(true)
 | 
					 | 
				
			||||||
        .append(true)
 | 
					 | 
				
			||||||
        .open("./reparsed-test.txt")
 | 
					 | 
				
			||||||
        .unwrap();
 | 
					 | 
				
			||||||
    let dictionary = read_and_parse(path);
 | 
					 | 
				
			||||||
    let dictionary = deduplicate(&dictionary);
 | 
					 | 
				
			||||||
    for entry in dictionary {
 | 
					 | 
				
			||||||
        if let Err(e) = writeln!(
 | 
					 | 
				
			||||||
            output,
 | 
					 | 
				
			||||||
            "({}) [{}] {{{}}}",
 | 
					 | 
				
			||||||
            entry.hieroglyph, entry.reading, entry.translation
 | 
					 | 
				
			||||||
        ) {
 | 
					 | 
				
			||||||
            eprintln!("Couldn't write to file: {}", e);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -10,7 +10,7 @@ mod db;
 | 
				
			|||||||
mod game;
 | 
					mod game;
 | 
				
			||||||
mod ui;
 | 
					mod ui;
 | 
				
			||||||
mod widgets;
 | 
					mod widgets;
 | 
				
			||||||
mod import;
 | 
					mod dictionary;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::ui::menu::MenuScene;
 | 
					use crate::ui::menu::MenuScene;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -39,9 +39,9 @@ const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
 | 
				
			|||||||
fn main() -> glib::ExitCode {
 | 
					fn main() -> glib::ExitCode {
 | 
				
			||||||
    gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
 | 
					    gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    db::init();
 | 
					    db::init(); // Create database if not exists, create program's home if not exists, etc...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    import::import(&String::from("./reparsed.txt"));
 | 
					    dictionary::import(&String::from("./reparsed.txt")); // 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let app: Application = Application::builder().application_id(APP_ID).build();
 | 
					    let app: Application = Application::builder().application_id(APP_ID).build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user