From fd022094a5753fb53c9c020be6ede31ef44dda41 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 23 Jul 2023 17:11:40 +0800 Subject: [PATCH] Change the return type to dictionary for easier to find the result by time-tag. --- .../Lyrics/Romajis/BaseRomajiGeneratorTest.cs | 10 +++++----- .../Lyrics/Romajis/Ja/JaRomajiGeneratorTest.cs | 2 +- .../Romajis/RomajiGenerateResultHelper.cs | 17 ++++++++--------- .../Romajis/RomajiGeneratorSelectorTest.cs | 10 +++++----- .../Lyrics/Romajies/Ja/JaRomajiGenerator.cs | 14 +++++++------- .../Lyrics/Romajies/RomajiGenerateResult.cs | 4 ---- .../Lyrics/Romajies/RomajiGenerator.cs | 3 ++- .../Lyrics/Romajies/RomajiGeneratorSelector.cs | 4 +++- 8 files changed, 31 insertions(+), 33 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/BaseRomajiGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/BaseRomajiGeneratorTest.cs index cf96bd3a1..59e80aedf 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/BaseRomajiGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/BaseRomajiGeneratorTest.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System.Collections.Generic; using System.Linq; using NUnit.Framework; using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies; @@ -9,7 +10,7 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.Romajis; -public abstract class BaseRomajiGeneratorTest : BaseLyricGeneratorTest +public abstract class BaseRomajiGeneratorTest : BaseLyricGeneratorTest, TConfig> where TRomajiGenerator : RomajiGenerator where TConfig : RomajiGeneratorConfig, new() { protected void CheckGenerateResult(Lyric lyric, string[] expectedRubies, TConfig config) @@ -18,10 +19,9 @@ protected void CheckGenerateResult(Lyric lyric, string[] expectedRubies, TConfig CheckGenerateResult(lyric, expected, config); } - protected override void AssertEqual(RomajiGenerateResult[] expected, RomajiGenerateResult[] actual) + protected override void AssertEqual(IReadOnlyDictionary expected, IReadOnlyDictionary actual) { - TimeTagAssert.ArePropertyEqual(expected.Select(x => x.TimeTag).ToArray(), actual.Select(x => x.TimeTag).ToArray()); - Assert.AreEqual(expected.Select(x => x.InitialRomaji), actual.Select(x => x.InitialRomaji)); - Assert.AreEqual(expected.Select(x => x.RomajiText), actual.Select(x => x.RomajiText)); + TimeTagAssert.ArePropertyEqual(expected.Select(x => x.Key).ToArray(), actual.Select(x => x.Key).ToArray()); + Assert.AreEqual(expected.Select(x => x.Value), actual.Select(x => x.Value)); } } diff --git a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/Ja/JaRomajiGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/Ja/JaRomajiGeneratorTest.cs index 6214c8895..1a28e9879 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/Ja/JaRomajiGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/Ja/JaRomajiGeneratorTest.cs @@ -81,7 +81,7 @@ public void TestConvertToRomajiGenerateResult(string text, string[] timeTagStrin var romajis = parseRomajiGenerateResults(romajiParams); var expected = RomajiGenerateResultHelper.ParseRomajiGenerateResults(timeTags, expectedResults); - var actual = JaRomajiGenerator.Convert(timeTags, romajis).ToArray(); + var actual = JaRomajiGenerator.Convert(timeTags, romajis); AssertEqual(expected, actual); } diff --git a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGenerateResultHelper.cs b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGenerateResultHelper.cs index d02f4bba7..6d2f25f61 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGenerateResultHelper.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGenerateResultHelper.cs @@ -21,26 +21,25 @@ public class RomajiGenerateResultHelper /// Origin time-tag /// Generate result string format /// Romaji generate result. - public static RomajiGenerateResult ParseRomajiGenerateResult(TimeTag timeTag, string str) + public static KeyValuePair ParseRomajiGenerateResult(TimeTag timeTag, string str) { - bool initialRomaji = str.StartsWith("^", StringComparison.Ordinal); - - return new RomajiGenerateResult + var result = new RomajiGenerateResult { - TimeTag = timeTag, - InitialRomaji = initialRomaji, + InitialRomaji = str.StartsWith("^", StringComparison.Ordinal), RomajiText = str.Replace("^", ""), }; + + return new KeyValuePair(timeTag, result); } - public static RomajiGenerateResult[] ParseRomajiGenerateResults(IList timeTags, IList strings) + public static IReadOnlyDictionary ParseRomajiGenerateResults(IList timeTags, IList strings) { if (timeTags.Count != strings.Count) throw new InvalidOperationException(); - return parseRomajiGenerateResults(timeTags, strings).ToArray(); + return parseRomajiGenerateResults(timeTags, strings).ToDictionary(k => k.Key, v => v.Value); - static IEnumerable parseRomajiGenerateResults(IList timeTags, IList strings) + static IEnumerable> parseRomajiGenerateResults(IList timeTags, IList strings) { for (int i = 0; i < timeTags.Count; i++) { diff --git a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGeneratorSelectorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGeneratorSelectorTest.cs index 7b1a981ca..81d14827d 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGeneratorSelectorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Editor/Generator/Lyrics/Romajis/RomajiGeneratorSelectorTest.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System.Collections.Generic; using System.Globalization; using System.Linq; using NUnit.Framework; @@ -12,7 +13,7 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.Romajis; -public class RomajiTagGeneratorSelectorTest : BaseLyricGeneratorSelectorTest +public class RomajiTagGeneratorSelectorTest : BaseLyricGeneratorSelectorTest> { [TestCase(17, "花火大会", true)] [TestCase(17, "我是中文", true)] // only change the language code to decide should be able to generate or not. @@ -54,10 +55,9 @@ public void TestGenerate(int lcid, string text, string[] timeTagStrings, string[ CheckGenerateResult(lyric, expected, selector); } - protected override void AssertEqual(RomajiGenerateResult[] expected, RomajiGenerateResult[] actual) + protected override void AssertEqual(IReadOnlyDictionary expected, IReadOnlyDictionary actual) { - TimeTagAssert.ArePropertyEqual(expected.Select(x => x.TimeTag).ToArray(), actual.Select(x => x.TimeTag).ToArray()); - Assert.AreEqual(expected.Select(x => x.InitialRomaji), actual.Select(x => x.InitialRomaji)); - Assert.AreEqual(expected.Select(x => x.RomajiText), actual.Select(x => x.RomajiText)); + TimeTagAssert.ArePropertyEqual(expected.Select(x => x.Key).ToArray(), actual.Select(x => x.Key).ToArray()); + Assert.AreEqual(expected.Select(x => x.Value), actual.Select(x => x.Value)); } } diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/Ja/JaRomajiGenerator.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/Ja/JaRomajiGenerator.cs index abce21b86..e1558c675 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/Ja/JaRomajiGenerator.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/Ja/JaRomajiGenerator.cs @@ -29,7 +29,7 @@ public JaRomajiGenerator(JaRomajiGeneratorConfig config) }); } - protected override RomajiGenerateResult[] GenerateFromItem(Lyric item) + protected override IReadOnlyDictionary GenerateFromItem(Lyric item) { // Tokenize the text string text = item.Text; @@ -39,7 +39,7 @@ protected override RomajiGenerateResult[] GenerateFromItem(Lyric item) var processingRomajies = getProcessingRomajies(text, tokenStream, Config).ToArray(); // then, trying to mapping them with the time-tags. - return Convert(item.TimeTags, processingRomajies).ToArray(); + return Convert(item.TimeTags, processingRomajies); } private static IEnumerable getProcessingRomajies(string text, TokenStream tokenStream, JaRomajiGeneratorConfig config) @@ -85,22 +85,22 @@ private static IEnumerable getProcessingRomajies(strin tokenStream.Dispose(); } - internal static IEnumerable Convert(IList timeTags, IList romajis) + internal static IReadOnlyDictionary Convert(IList timeTags, IList romajis) { var group = createGroup(timeTags, romajis); - return group.Select((x, i) => + return group.ToDictionary(k => k.Key, (x) => { + bool isFirst = timeTags.IndexOf(x.Key) == 0; // todo: use better to mark the initial romaji. string romajiText = string.Join(" ", x.Value.Select(r => r.RomajiText)); return new RomajiGenerateResult { - TimeTag = x.Key, - InitialRomaji = i == 0, // todo: use better to mark the initial romaji. + InitialRomaji = isFirst, RomajiText = romajiText, }; }); - static IDictionary> createGroup(IList timeTags, IList romajis) + static IReadOnlyDictionary> createGroup(IList timeTags, IList romajis) { var dictionary = timeTags.ToDictionary(x => x, v => new List()); diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerateResult.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerateResult.cs index 8b7435b0d..5e436e639 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerateResult.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerateResult.cs @@ -1,14 +1,10 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Game.Rulesets.Karaoke.Objects; - namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies; public struct RomajiGenerateResult { - public TimeTag TimeTag { get; set; } - public bool InitialRomaji { get; set; } public string? RomajiText { get; set; } diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerator.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerator.cs index 744fc2d51..554feffa8 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerator.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGenerator.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System.Collections.Generic; using System.Linq; using osu.Framework.Graphics.Sprites; using osu.Framework.Localisation; @@ -8,7 +9,7 @@ namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies; -public abstract class RomajiGenerator : LyricPropertyGenerator +public abstract class RomajiGenerator : LyricPropertyGenerator, TConfig> where TConfig : RomajiGeneratorConfig, new() { protected RomajiGenerator(TConfig config) diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGeneratorSelector.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGeneratorSelector.cs index 9215854d3..58c8d2c1b 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGeneratorSelector.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/Lyrics/Romajies/RomajiGeneratorSelector.cs @@ -1,13 +1,15 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System.Collections.Generic; using System.Globalization; using osu.Game.Rulesets.Karaoke.Configuration; using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies.Ja; +using osu.Game.Rulesets.Karaoke.Objects; namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies; -public class RomajiGeneratorSelector : LyricGeneratorSelector +public class RomajiGeneratorSelector : LyricGeneratorSelector, RomajiGeneratorConfig> { public RomajiGeneratorSelector(KaraokeRulesetEditGeneratorConfigManager generatorConfigManager) : base(generatorConfigManager)