update dictionary schema

This commit is contained in:
leca 2024-04-13 23:13:25 +03:00
parent b55d458722
commit b25da1b84b
4 changed files with 5087 additions and 5001 deletions

File diff suppressed because it is too large Load Diff

5000
reparsed.txt Normal file

File diff suppressed because it is too large Load Diff

83
src/import.rs Normal file
View File

@ -0,0 +1,83 @@
use std::fs;
use std::fs::OpenOptions;
use std::io::prelude::*;
struct Entry {
hieroglyph: String,
reading: String,
translation: String,
}
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
}
pub fn import(path: &String) {
let mut output = OpenOptions::new()
.write(true)
.append(true)
.open("./reparsed-test.txt")
.unwrap();
for entry in read_and_parse(path) {
if let Err(e) = writeln!(
output,
"({}) [{}] {{{}}}",
entry.hieroglyph, entry.reading, entry.translation
) {
eprintln!("Couldn't write to file: {}", e);
}
}
}

View File

@ -10,6 +10,7 @@ mod db;
mod game; mod game;
mod ui; mod ui;
mod widgets; mod widgets;
mod import;
use crate::ui::menu::MenuScene; use crate::ui::menu::MenuScene;
@ -40,6 +41,8 @@ fn main() -> glib::ExitCode {
db::init(); db::init();
import::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();
app.connect_activate(build_ui); app.connect_activate(build_ui);
@ -188,4 +191,4 @@ fn connect_close_requrest<W: IsA<Window>>(app: &Application, w: &W) {
true true
}), }),
); );
} }