implemented deduplication and deduplicated dictionaty

This commit is contained in:
leca 2024-04-14 08:49:34 +03:00
parent b25da1b84b
commit 53ebe77935
2 changed files with 204 additions and 348 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,12 +2,19 @@ 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!(
@ -65,13 +72,40 @@ fn read_and_parse(path: &String) -> Vec<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();
for entry in read_and_parse(path) {
let dictionary = read_and_parse(path);
let dictionary = deduplicate(&dictionary);
for entry in dictionary {
if let Err(e) = writeln!(
output,
"({}) [{}] {{{}}}",