From 5296e1c7080f5b118726579b69bb098c86faf669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Sat, 4 Dec 2021 14:49:43 +0100 Subject: [PATCH] Better comparison of music tags (#485) --- Changelog.md | 1 + czkawka_core/src/same_music.rs | 77 +++++++++++++++++++++++------ czkawka_gui/src/create_tree_view.rs | 22 ++++----- czkawka_gui/ui/main_window.glade | 4 +- 4 files changed, 76 insertions(+), 28 deletions(-) diff --git a/Changelog.md b/Changelog.md index f295fd9..a581796 100644 --- a/Changelog.md +++ b/Changelog.md @@ -12,6 +12,7 @@ - Improve custom selecting of records(allows to use Rust red regex) - [#489](https://github.com/qarmin/czkawka/pull/478) - Remove support for finding zeroed files - [#461](https://github.com/qarmin/czkawka/pull/461) - Remove HashMB mode - [#476](https://github.com/qarmin/czkawka/pull/476) +- Approximate comparison of music - [#483](https://github.com/qarmin/czkawka/pull/483) ## Version 3.3.1 - 22.11.2021r - Fix crash when moving buttons [#457](https://github.com/qarmin/czkawka/pull/457) diff --git a/czkawka_core/src/same_music.rs b/czkawka_core/src/same_music.rs index 569fbc4..5e966cd 100644 --- a/czkawka_core/src/same_music.rs +++ b/czkawka_core/src/same_music.rs @@ -477,12 +477,10 @@ impl SameMusic { let mut hash_map: BTreeMap> = Default::default(); for file_entry in vec_file_entry { let mut title = file_entry.title.to_lowercase().trim().to_string(); + if self.approximate_comparison { + get_approximate_conversion(&mut title); + } if !title.is_empty() { - if self.approximate_comparison { - if let Some(index) = title.find('(') { - title = title[0..index].trim().to_string(); - } - } hash_map.entry(title.clone()).or_insert_with(Vec::new); hash_map.get_mut(title.as_str()).unwrap().push(file_entry); } @@ -510,9 +508,7 @@ impl SameMusic { for file_entry in vec_file_entry { let mut artist = file_entry.artist.to_lowercase().trim().to_string(); if self.approximate_comparison { - if let Some(index) = artist.find('(') { - artist = artist[0..index].trim().to_string(); - } + get_approximate_conversion(&mut artist); } if !artist.is_empty() { hash_map.entry(artist.clone()).or_insert_with(Vec::new); @@ -542,9 +538,7 @@ impl SameMusic { for file_entry in vec_file_entry { let mut album_title = file_entry.album_title.to_lowercase().trim().to_string(); if self.approximate_comparison { - if let Some(index) = album_title.find('(') { - album_title = album_title[0..index].trim().to_string(); - } + get_approximate_conversion(&mut album_title); } if !album_title.is_empty() { hash_map.entry(album_title.clone()).or_insert_with(Vec::new); @@ -574,9 +568,7 @@ impl SameMusic { for file_entry in vec_file_entry { let mut album_artist = file_entry.album_artist.to_lowercase().trim().to_string(); if self.approximate_comparison { - if let Some(index) = album_artist.find('(') { - album_artist = album_artist[0..index].trim().to_string(); - } + get_approximate_conversion(&mut album_artist); } if !album_artist.is_empty() { hash_map.entry(album_artist.clone()).or_insert_with(Vec::new); @@ -752,12 +744,12 @@ impl PrintResults for SameMusic { for vec_file_entry in self.duplicated_music_entries.iter() { for file_entry in vec_file_entry { println!( - "T: {} - A: {} - AT: {} - AA: {} - Y: {} - P: {}", + "T: {} - A: {} - Y: {} - AT: {} - AA: {} - P: {}", file_entry.title, file_entry.artist, + file_entry.year, file_entry.album_title, file_entry.album_artist, - file_entry.year, file_entry.path.display() ); } @@ -767,3 +759,56 @@ impl PrintResults for SameMusic { Common::print_time(start_time, SystemTime::now(), "print_entries".to_string()); } } + +fn get_approximate_conversion(what: &mut String) { + let mut new_what = String::with_capacity(what.len()); + let mut tab_number = 0; + let mut space_before = true; + for character in what.chars().into_iter() { + match character { + '(' => { + tab_number += 1; + } + ')' => { + if tab_number == 0 { + // Nothing to do, not even save it to output + } else { + tab_number -= 1; + } + } + ' ' => { + if !space_before { + new_what.push(' '); + space_before = true; + } + } + ch => { + if tab_number == 0 { + space_before = false; + new_what.push(ch); + } + } + } + } + + if new_what.ends_with(' ') { + new_what.pop(); + } + *what = new_what; +} + +#[cfg(test)] +mod tests { + use crate::same_music::get_approximate_conversion; + + #[test] + fn test_strings() { + let mut what = "roman ( ziemniak ) ".to_string(); + get_approximate_conversion(&mut what); + assert_eq!(what, "roman"); + + let mut what = " HH) ".to_string(); + get_approximate_conversion(&mut what); + assert_eq!(what, "HH"); + } +} diff --git a/czkawka_gui/src/create_tree_view.rs b/czkawka_gui/src/create_tree_view.rs index 79ccf48..af2426e 100644 --- a/czkawka_gui/src/create_tree_view.rs +++ b/czkawka_gui/src/create_tree_view.rs @@ -517,6 +517,17 @@ pub fn create_tree_view_same_music(tree_view: &mut gtk::TreeView) { column.add_attribute(&renderer, "foreground", ColumnsSameMusic::TextColor as i32); tree_view.append_column(&column); + let renderer = gtk::CellRendererText::new(); + let column: gtk::TreeViewColumn = TreeViewColumn::new(); + column.pack_start(&renderer, true); + column.set_title("Year"); + column.set_resizable(true); + column.set_min_width(50); + column.add_attribute(&renderer, "text", ColumnsSameMusic::Year as i32); + column.add_attribute(&renderer, "background", ColumnsSameMusic::Color as i32); + column.add_attribute(&renderer, "foreground", ColumnsSameMusic::TextColor as i32); + tree_view.append_column(&column); + let renderer = gtk::CellRendererText::new(); let column: gtk::TreeViewColumn = TreeViewColumn::new(); column.pack_start(&renderer, true); @@ -539,17 +550,6 @@ pub fn create_tree_view_same_music(tree_view: &mut gtk::TreeView) { column.add_attribute(&renderer, "foreground", ColumnsSameMusic::TextColor as i32); tree_view.append_column(&column); - let renderer = gtk::CellRendererText::new(); - let column: gtk::TreeViewColumn = TreeViewColumn::new(); - column.pack_start(&renderer, true); - column.set_title("Year"); - column.set_resizable(true); - column.set_min_width(50); - column.add_attribute(&renderer, "text", ColumnsSameMusic::Year as i32); - column.add_attribute(&renderer, "background", ColumnsSameMusic::Color as i32); - column.add_attribute(&renderer, "foreground", ColumnsSameMusic::TextColor as i32); - tree_view.append_column(&column); - let renderer = gtk::CellRendererText::new(); let column: gtk::TreeViewColumn = TreeViewColumn::new(); column.pack_start(&renderer, true); diff --git a/czkawka_gui/ui/main_window.glade b/czkawka_gui/ui/main_window.glade index 0f57e34..0ff79f6 100644 --- a/czkawka_gui/ui/main_window.glade +++ b/czkawka_gui/ui/main_window.glade @@ -1855,7 +1855,6 @@ Author: Rafał Mikrut False 5 5 - 2 8 @@ -1940,6 +1939,9 @@ Author: Rafał Mikrut True False + 5 + 5 + 2 Approximate Comparison