Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make Matcher package self-contained #58

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 0 additions & 58 deletions SubRenamer/Helper/MixedStringComparer.cs

This file was deleted.

64 changes: 62 additions & 2 deletions SubRenamer/Matcher/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using SubRenamer.Helper;

namespace SubRenamer.Matcher;

Expand All @@ -17,7 +16,7 @@ public static string ExtractMatchKeyRegex(string pattern, string filename)
}
catch (Exception e)
{
Logger.Out.WriteLine(e.Message);
Logger.Out.WriteLine("[ExtractMatchKeyRegex] Exception: {0}", e.Message);
}

return "";
Expand Down Expand Up @@ -82,4 +81,65 @@ public static List<MatchItem> SortItemsByKeys(IReadOnlyList<MatchItem> items)
result.Sort((a, b) => new MixedStringComparer().Compare(a.Key, b.Key));
return result;
}

/// <summary>
/// Compares two strings using a mixed comparison algorithm.
/// </summary>
/// <remarks>
/// The algorithm compares strings by their numeric and non-numeric parts.
/// </remarks>
public class MixedStringComparer : IComparer<string>
{
public int Compare(string? x, string? y)
{
if (x is null || y is null) return 0;
return NaturalCompare(x, y);
}

private int NaturalCompare(string str1, string str2)
{
int length1 = str1.Length;
int length2 = str2.Length;
int index1 = 0;
int index2 = 0;

while (index1 < length1 && index2 < length2)
{
if (char.IsDigit(str1[index1]) && char.IsDigit(str2[index2]))
{
int num1 = 0;
int num2 = 0;

while (index1 < length1 && char.IsDigit(str1[index1]))
{
num1 = num1 * 10 + (str1[index1] - '0');
index1++;
}

while (index2 < length2 && char.IsDigit(str2[index2]))
{
num2 = num2 * 10 + (str2[index2] - '0');
index2++;
}

if (num1 != num2)
{
return num1.CompareTo(num2);
}
}
else
{
if (str1[index1] != str2[index2])
{
return str1[index1].CompareTo(str2[index2]);
}

index1++;
index2++;
}
}

return length1.CompareTo(length2);
}
}
}
2 changes: 1 addition & 1 deletion SubRenamer/Matcher/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public static class Logger
{
public static TextWriter Out { get; private set; } = Console.Out;
public static void SetWriter(TextWriter writer) => Out = writer;
}
}
29 changes: 13 additions & 16 deletions SubRenamer/Matcher/Matcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SubRenamer.Common;

namespace SubRenamer.Matcher;

Expand All @@ -10,6 +9,9 @@ public record MatchItem(string Key, string Video, string Subtitle);
public static class Matcher
{
public static List<MatchItem> Execute(IReadOnlyList<MatchItem> inputItems)
=> Execute(inputItems, new MatcherOptions());

public static List<MatchItem> Execute(IReadOnlyList<MatchItem> inputItems, MatcherOptions options)
{
// Create new collection
List<MatchItem> result = [];
Expand All @@ -30,12 +32,8 @@ public static List<MatchItem> Execute(IReadOnlyList<MatchItem> inputItems)
});

// Get file keys
var m = Config.Get().MatchMode;
var videoRegex = (m != MatchMode.Diff) ? (m == MatchMode.Manual ? Config.Get().ManualVideoRegex : Config.Get().VideoRegex) : null;
var subtitleRegex = (m != MatchMode.Diff) ? (m == MatchMode.Manual ? Config.Get().ManualSubtitleRegex : Config.Get().SubtitleRegex) : null;

var video2Keys = CalculateFileKeys(videoFiles, videoRegex);
var subtitle2Keys = CalculateFileKeys(subtitleFiles, subtitleRegex);
var video2Keys = CalculateFileKeys(videoFiles, customRegex: options.VideoRegex);
var subtitle2Keys = CalculateFileKeys(subtitleFiles, customRegex: options.SubtitleRegex);

// Apply keys
List<MatchItem> keyedItems = [];
Expand Down Expand Up @@ -63,22 +61,21 @@ public static List<MatchItem> Execute(IReadOnlyList<MatchItem> inputItems)
return result;
}

private static Dictionary<string, string> CalculateFileKeys(IReadOnlyList<string> files, string? regexPattern)
private static Dictionary<string, string> CalculateFileKeys(IReadOnlyList<string> files, string? customRegex)
{
var result = new Dictionary<string, string>();

if (regexPattern is null)
if (customRegex is null)
{
// 1. Auto Diff Algorithm

// Diff filenames
var names = files
// Method 1. Auto Diff Algorithm
var filenames = files
.Select(Path.GetFileNameWithoutExtension)
.Where(x => !string.IsNullOrEmpty(x))
.Distinct()
.ToList();

var diff = Diff.GetDiffResult(names!);
// Diff filenames
var diff = Diff.GetDiffResult(filenames!);
Logger.Out.WriteLine("[Diff.GetDiffResult]\n\n {0}\n", (diff != null ? diff : "null"));

// Extract Match keys
Expand All @@ -89,10 +86,10 @@ private static Dictionary<string, string> CalculateFileKeys(IReadOnlyList<string
}
else
{
// 2. Regex Algorithm
// Method 2. Custom Regex
foreach (var f in files)
{
result[f] = Helper.PatchKey(Helper.ExtractMatchKeyRegex(regexPattern, Path.GetFileName(f)));
result[f] = Helper.PatchKey(Helper.ExtractMatchKeyRegex(customRegex, Path.GetFileName(f)));
}
}

Expand Down
16 changes: 16 additions & 0 deletions SubRenamer/Matcher/MatcherOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace SubRenamer.Matcher;

public class MatcherOptions
{
/// <summary>
/// Custom regex for extracting keys from video files.
/// (If not set, the diff algorithm will be used)
/// </summary>
public string? VideoRegex { get; init; }

/// <summary>
/// Custom regex for extracting keys from subtitle files
/// (If not set, the diff algorithm will be used)
/// </summary>
public string? SubtitleRegex { get; init; }
}
10 changes: 9 additions & 1 deletion SubRenamer/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
using Microsoft.Extensions.DependencyInjection;
using SubRenamer.Common;
using SubRenamer.Helper;
using SubRenamer.Matcher;
using SubRenamer.Model;
using SubRenamer.Services;
using MatchItem = SubRenamer.Model.MatchItem;

namespace SubRenamer.ViewModels;

Expand Down Expand Up @@ -144,7 +146,13 @@ private void PerformMatch()
{
ShowRenameTasks = false;
var inputItems = MatcherDataConverter.ConvertMatchItems(MatchList);
var resultRaw = Matcher.Matcher.Execute(inputItems);
var m = Config.Get().MatchMode;
var resultRaw = Matcher.Matcher.Execute(inputItems, new MatcherOptions()
{
// Convert Config to MatcherOptions
VideoRegex = (m != MatchMode.Diff) ? (m == MatchMode.Manual ? Config.Get().ManualVideoRegex : Config.Get().VideoRegex) : null,
SubtitleRegex = (m != MatchMode.Diff) ? (m == MatchMode.Manual ? Config.Get().ManualSubtitle : Config.Get().SubtitleRegex) : null,
});
var result = MatcherDataConverter.ConvertMatchItems(resultRaw);
result.ForEach(UpdateMatchItemStatus);
MatchList = new ObservableCollection<MatchItem>(result);
Expand Down