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

Change romaji generator return type from array to the dictionaty. #2078

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) andy840119 <[email protected]>. 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;
Expand All @@ -9,7 +10,7 @@

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.Romajis;

public abstract class BaseRomajiGeneratorTest<TRomajiGenerator, TConfig> : BaseLyricGeneratorTest<TRomajiGenerator, RomajiGenerateResult[], TConfig>
public abstract class BaseRomajiGeneratorTest<TRomajiGenerator, TConfig> : BaseLyricGeneratorTest<TRomajiGenerator, IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, TConfig>
where TRomajiGenerator : RomajiGenerator<TConfig> where TConfig : RomajiGeneratorConfig, new()
{
protected void CheckGenerateResult(Lyric lyric, string[] expectedRubies, TConfig config)
Expand All @@ -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<TimeTag, RomajiGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomajiGenerateResult> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,25 @@ public class RomajiGenerateResultHelper
/// <param name="timeTag">Origin time-tag</param>
/// <param name="str">Generate result string format</param>
/// <returns><see cref="RomajiGenerateResult"/>Romaji generate result.</returns>
public static RomajiGenerateResult ParseRomajiGenerateResult(TimeTag timeTag, string str)
public static KeyValuePair<TimeTag, RomajiGenerateResult> 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, RomajiGenerateResult>(timeTag, result);
}

public static RomajiGenerateResult[] ParseRomajiGenerateResults(IList<TimeTag> timeTags, IList<string> strings)
public static IReadOnlyDictionary<TimeTag, RomajiGenerateResult> ParseRomajiGenerateResults(IList<TimeTag> timeTags, IList<string> 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<RomajiGenerateResult> parseRomajiGenerateResults(IList<TimeTag> timeTags, IList<string> strings)
static IEnumerable<KeyValuePair<TimeTag, RomajiGenerateResult>> parseRomajiGenerateResults(IList<TimeTag> timeTags, IList<string> strings)
{
for (int i = 0; i < timeTags.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) andy840119 <[email protected]>. 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;
Expand All @@ -12,7 +13,7 @@

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.Romajis;

public class RomajiTagGeneratorSelectorTest : BaseLyricGeneratorSelectorTest<RomajiGeneratorSelector, RomajiGenerateResult[]>
public class RomajiTagGeneratorSelectorTest : BaseLyricGeneratorSelectorTest<RomajiGeneratorSelector, IReadOnlyDictionary<TimeTag, RomajiGenerateResult>>
{
[TestCase(17, "花火大会", true)]
[TestCase(17, "我是中文", true)] // only change the language code to decide should be able to generate or not.
Expand Down Expand Up @@ -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<TimeTag, RomajiGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomajiGenerateResult> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public JaRomajiGenerator(JaRomajiGeneratorConfig config)
});
}

protected override RomajiGenerateResult[] GenerateFromItem(Lyric item)
protected override IReadOnlyDictionary<TimeTag, RomajiGenerateResult> GenerateFromItem(Lyric item)
{
// Tokenize the text
string text = item.Text;
Expand All @@ -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<RomajiGeneratorParameter> getProcessingRomajies(string text, TokenStream tokenStream, JaRomajiGeneratorConfig config)
Expand Down Expand Up @@ -85,22 +85,22 @@ private static IEnumerable<RomajiGeneratorParameter> getProcessingRomajies(strin
tokenStream.Dispose();
}

internal static IEnumerable<RomajiGenerateResult> Convert(IList<TimeTag> timeTags, IList<RomajiGeneratorParameter> romajis)
internal static IReadOnlyDictionary<TimeTag, RomajiGenerateResult> Convert(IList<TimeTag> timeTags, IList<RomajiGeneratorParameter> 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<TimeTag, List<RomajiGeneratorParameter>> createGroup(IList<TimeTag> timeTags, IList<RomajiGeneratorParameter> romajis)
static IReadOnlyDictionary<TimeTag, List<RomajiGeneratorParameter>> createGroup(IList<TimeTag> timeTags, IList<RomajiGeneratorParameter> romajis)
{
var dictionary = timeTags.ToDictionary(x => x, v => new List<RomajiGeneratorParameter>());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Copyright (c) andy840119 <[email protected]>. 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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) andy840119 <[email protected]>. 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;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies;

public abstract class RomajiGenerator<TConfig> : LyricPropertyGenerator<RomajiGenerateResult[], TConfig>
public abstract class RomajiGenerator<TConfig> : LyricPropertyGenerator<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, TConfig>
where TConfig : RomajiGeneratorConfig, new()
{
protected RomajiGenerator(TConfig config)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) andy840119 <[email protected]>. 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<RomajiGenerateResult[], RomajiGeneratorConfig>
public class RomajiGeneratorSelector : LyricGeneratorSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>
{
public RomajiGeneratorSelector(KaraokeRulesetEditGeneratorConfigManager generatorConfigManager)
: base(generatorConfigManager)
Expand Down