Skip to content

Commit

Permalink
fix: swap trailing determiners at the end of titles
Browse files Browse the repository at this point in the history
fixes Radarr#9815.\n\n Some files have their , A or , The at the end of the file to prevent alphabetical sorting clustering these titles together. This stops working when searching for files inside radarr as a cleaned title will have 'prestigethe' instead of 'theprestige'.
  • Loading branch information
tempcvrohch committed Mar 5, 2024
1 parent a0dd26c commit 7db56d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/NzbDrone.Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NzbDrone.Common.Extensions
public static class StringExtensions
{
private static readonly Regex CamelCaseRegex = new Regex("(?<!^)[A-Z]", RegexOptions.Compiled);
private static readonly Regex TrailingDeterminerRegex = new Regex("(, The)$|(, A)$", RegexOptions.Compiled);

public static string NullSafe(this string target)
{
Expand Down Expand Up @@ -228,5 +229,18 @@ public static string Reverse(this string text)

return new string(array);
}

// Some files have their determiner (The, A) at the end of the title to help sorting, flip them back to prevent searching errors.
public static string SwapTrailingDeterminers(this string input)
{
var match = TrailingDeterminerRegex.Match(input);
if (match.Success)
{
input = TrailingDeterminerRegex.Replace(input, string.Empty);
input = match.Value.Replace(", ", string.Empty) + " " + input;
}

return input;
}
}
}
12 changes: 12 additions & 0 deletions src/NzbDrone.Core.Test/ParserTests/NormalizeTitleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,17 @@ public void should_not_clean_trailing_a()
{
"Tokyo Ghoul A".CleanMovieTitle().Should().Be("tokyoghoula");
}

[TestCase("Prestige, The", "theprestige")]
[TestCase("Prestige, A", "aprestige")]
[TestCase("Crow, The", "thecrow")]
[TestCase("Crow, A", "acrow")]
[TestCase("Beautiful Romance, The", "thebeautifulromance")]
[TestCase("Beautiful Romance, A", "abeautifulromance")]
public void should_swap_determiner(string parsedSeriesName, string seriesName)
{
var result = parsedSeriesName.CleanMovieTitle();
result.Should().Be(seriesName);
}
}
}
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public static string CleanMovieTitle(this string title)
return title;
}

return ReplaceGermanUmlauts(NormalizeRegex.Replace(title, string.Empty).ToLower()).RemoveAccent();
return ReplaceGermanUmlauts(NormalizeRegex.Replace(title.SwapTrailingDeterminers(), string.Empty).ToLower()).RemoveAccent();
}

public static string NormalizeEpisodeTitle(this string title)
Expand Down

0 comments on commit 7db56d7

Please sign in to comment.