Skip to content

Commit

Permalink
Rename the romanization generator and adjust the naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Jan 16, 2024
1 parent 38a6ed2 commit d63d5cf
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 165 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;

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

public abstract class BaseRomajiGeneratorTest<TRomajiGenerator, TConfig> : BaseLyricGeneratorTest<TRomajiGenerator, IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, TConfig>
where TRomajiGenerator : RomajiGenerator<TConfig> where TConfig : RomajiGeneratorConfig, new()
public abstract class BaseRomanizationGeneratorTest<TRomanizationGenerator, TConfig> : BaseLyricGeneratorTest<TRomanizationGenerator, IReadOnlyDictionary<TimeTag, RomanizationGenerateResult>, TConfig>
where TRomanizationGenerator : RomanizationGenerator<TConfig> where TConfig : RomanizationGeneratorConfig, new()
{
protected void CheckGenerateResult(Lyric lyric, string[] expectedRubies, TConfig config)
{
var expected = RomajiGenerateResultHelper.ParseRomajiGenerateResults(lyric.TimeTags, expectedRubies);
var expected = RomanizationGenerateResultHelper.ParseRomanizationGenerateResults(lyric.TimeTags, expectedRubies);
CheckGenerateResult(lyric, expected, config);
}

protected override void AssertEqual(IReadOnlyDictionary<TimeTag, RomajiGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomajiGenerateResult> actual)
protected override void AssertEqual(IReadOnlyDictionary<TimeTag, RomanizationGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomanizationGenerateResult> actual)
{
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization.Ja;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

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

public class JaRomajiGeneratorTest : BaseRomajiGeneratorTest<JaRomajiGenerator, JaRomajiGeneratorConfig>
public class JaRomanizationGeneratorTest : BaseRomanizationGeneratorTest<JaRomanizationGenerator, JaRomanizationGeneratorConfig>
{
[TestCase("花火大会", new[] { "[0,start]", "[3,end]" }, true)]
[TestCase("花火大会", new[] { "[0,start]" }, true)]
[TestCase("花火大会", new[] { "[3,end]" }, false)] // not able to generate the has no start time-tag.
[TestCase("花火大会", new string[] { }, false)] // not able to generate the romaji if has no time-tag.
[TestCase("", new string[] { }, false)] // not able to generate the romaji if lyric is empty.
[TestCase("花火大会", new string[] { }, false)] // not able to make the romanization if has no time-tag.
[TestCase("", new string[] { }, false)] // not able to make the romanization if lyric is empty.
[TestCase(" ", new string[] { }, false)]
[TestCase(null, new string[] { }, false)]
public void TestCanGenerate(string text, string[] timeTagStrings, bool canGenerate)
Expand All @@ -40,7 +40,7 @@ public void TestCanGenerate(string text, string[] timeTagStrings, bool canGenera
[TestCase("枯れた世界に輝く",
new[] { "[0,start]", "[1,start]", "[2,start]", "[3,start]", "[4,start]", "[5,start]", "[6,start]", "[6,start]", "[6,start]", "[7,start]", "[7,end]" },
new[] { "^kare", "", "ta", "sekai", "", "ni", "kagayaku", "", "", "", "" })]
public void TestGenerate(string text, string[] timeTagStrings, string[] expectedRomajies)
public void TestGenerate(string text, string[] timeTagStrings, string[] expectedRomanizedSyllables)
{
var config = GeneratorEmptyConfig();

Expand All @@ -51,12 +51,12 @@ public void TestGenerate(string text, string[] timeTagStrings, string[] expected
TimeTags = timeTags,
};

CheckGenerateResult(lyric, expectedRomajies, config);
CheckGenerateResult(lyric, expectedRomanizedSyllables, config);
}

[TestCase("はなび", new[] { "[0,start]" }, new[] { "^HANA BI" })]
[TestCase("花火大会", new[] { "[0,start]", "[2,start]", "[3,end]" }, new[] { "^HANABI", "TAIKAI", "" })]
public void TestGenerateWithUppercase(string text, string[] timeTagStrings, string[] expectedRomajies)
public void TestGenerateWithUppercase(string text, string[] timeTagStrings, string[] expectedRomanizedSyllables)
{
var config = GeneratorEmptyConfig(x => x.Uppercase.Value = true);

Expand All @@ -67,45 +67,42 @@ public void TestGenerateWithUppercase(string text, string[] timeTagStrings, stri
TimeTags = timeTags,
};

CheckGenerateResult(lyric, expectedRomajies, config);
CheckGenerateResult(lyric, expectedRomanizedSyllables, config);
}

[TestCase("花", new[] { "[0,start]", "[0,end]" }, new[] { "[0]:hana" }, new[] { "^hana", "" })]
[TestCase("花火", new[] { "[0,start]", "[1,end]" }, new[] { "[0]:hana", "[1]:bi" }, new[] { "^hana bi", "" })]
[TestCase("花火", new[] { "[0,start]", "[1,start]", "[1,end]" }, new[] { "[0]:hana", "[1]:bi" }, new[] { "^hana", "bi", "" })]
[TestCase("花火", new[] { "[0,start]", "[0,start]", "[1,start]", "[1,end]" }, new[] { "[0]:hana", "[1]:bi" }, new[] { "^hana", "", "bi", "" })]
[TestCase("はなび", new[] { "[0,start]", "[1,start]", "[2,start]", "[2,end]" }, new[] { "[0]:hana", "[2]:bi" }, new[] { "^hana", "", "bi", "" })]
public void TestConvertToRomajiGenerateResult(string text, string[] timeTagStrings, string[] romajiParams, string[] expectedResults)
public void TestConvertToRomanizationGenerateResult(string text, string[] timeTagStrings, string[] romanizationParams, string[] expectedResults)
{
var timeTags = TestCaseTagHelper.ParseTimeTags(timeTagStrings);
var romajis = parseRomajiGenerateResults(romajiParams);
var romanizations = parseRomanizationGenerateResults(romanizationParams);

var expected = RomajiGenerateResultHelper.ParseRomajiGenerateResults(timeTags, expectedResults);
var actual = JaRomajiGenerator.Convert(timeTags, romajis);
var expected = RomanizationGenerateResultHelper.ParseRomanizationGenerateResults(timeTags, expectedResults);
var actual = JaRomanizationGenerator.Convert(timeTags, romanizations);

AssertEqual(expected, actual);
}

/// <summary>
/// Process test case time tag string format into <see cref="TimeTag"/>
/// </summary>
/// <example>
/// <inheritdoc cref="TestCaseTagHelper.ParseRomajiTag"/>
/// </example>
/// <param name="str">Time tag string format</param>
/// <returns><see cref="RomajiGenerateResultHelper"/>Time tag object</returns>
private static JaRomajiGenerator.RomajiGeneratorParameter parseRomajiGenerateResult(string str)
/// <returns><see cref="RomanizationGenerateResultHelper"/>Time tag object</returns>
private static JaRomanizationGenerator.RomanizationGeneratorParameter parseRomanizationGenerateResult(string str)
{
// because format is same as the text-tag testing format, so just use this helper.
var romajiTag = TestCaseTagHelper.ParseRomajiTag(str);
return new JaRomajiGenerator.RomajiGeneratorParameter
return new JaRomanizationGenerator.RomanizationGeneratorParameter
{
StartIndex = romajiTag.StartIndex,
EndIndex = romajiTag.EndIndex,
RomajiText = romajiTag.Text,
RomanizedSyllable = romajiTag.Text,
};
}

private static JaRomajiGenerator.RomajiGeneratorParameter[] parseRomajiGenerateResults(IEnumerable<string> strings)
=> strings.Select(parseRomajiGenerateResult).ToArray();
private static JaRomanizationGenerator.RomanizationGeneratorParameter[] parseRomanizationGenerateResults(IEnumerable<string> strings)
=> strings.Select(parseRomanizationGenerateResult).ToArray();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization;
using osu.Game.Rulesets.Karaoke.Objects;

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

public class RomanizationGenerateResultHelper
{
/// <summary>
/// Convert the string format into the <see cref="RomanizationGenerateResult"/>.
/// </summary>
/// <example>
/// karaoke
/// ^karaoke
/// </example>
/// <param name="timeTag">Origin time-tag</param>
/// <param name="str">Generate result string format</param>
/// <returns><see cref="RomanizationGenerateResult"/>Romanization generate result.</returns>
public static KeyValuePair<TimeTag, RomanizationGenerateResult> ParseRomanizationGenerateResult(TimeTag timeTag, string str)
{
var result = new RomanizationGenerateResult
{
FirstSyllable = str.StartsWith("^", StringComparison.Ordinal),
RomanizedSyllable = str.Replace("^", ""),
};

return new KeyValuePair<TimeTag, RomanizationGenerateResult>(timeTag, result);
}

public static IReadOnlyDictionary<TimeTag, RomanizationGenerateResult> ParseRomanizationGenerateResults(IList<TimeTag> timeTags, IList<string> strings)
{
if (timeTags.Count != strings.Count)
throw new InvalidOperationException();

return parseRomanizationGenerateResults(timeTags, strings).ToDictionary(k => k.Key, v => v.Value);

static IEnumerable<KeyValuePair<TimeTag, RomanizationGenerateResult>> parseRomanizationGenerateResults(IList<TimeTag> timeTags, IList<string> strings)
{
for (int i = 0; i < timeTags.Count; i++)
{
yield return ParseRomanizationGenerateResult(timeTags[i], strings[i]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

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

public class RomajiTagGeneratorSelectorTest : BaseLyricGeneratorSelectorTest<RomajiGeneratorSelector, IReadOnlyDictionary<TimeTag, RomajiGenerateResult>>
public class RomanizationGeneratorSelectorTest : BaseLyricGeneratorSelectorTest<RomanizationGeneratorSelector, IReadOnlyDictionary<TimeTag, RomanizationGenerateResult>>
{
[TestCase(17, "花火大会", true)]
[TestCase(17, "我是中文", true)] // only change the language code to decide should be able to generate or not.
[TestCase(17, "", false)] // will not able to generate the romaji if lyric is empty.
[TestCase(17, "", false)] // will not able to make the romanization if lyric is empty.
[TestCase(17, " ", false)]
[TestCase(17, null, false)]
[TestCase(1028, "はなび", false)] // Should not be able to generate if language is not supported.
Expand All @@ -39,7 +39,7 @@ public void TestCanGenerate(int lcid, string text, bool canGenerate)

[TestCase(17, "はなび", new[] { "[0,start]" }, new[] { "^hana bi" })] // Japanese
[TestCase(1041, "花火大会", new[] { "[0,start]", "[3,end]" }, new[] { "^hanabi taikai", "" })] // Japanese
public void TestGenerate(int lcid, string text, string[] timeTagStrings, string[] expectedRomajies)
public void TestGenerate(int lcid, string text, string[] timeTagStrings, string[] expectedRomanizedSyllables)
{
var selector = CreateSelector();

Expand All @@ -51,11 +51,11 @@ public void TestGenerate(int lcid, string text, string[] timeTagStrings, string[
TimeTags = timeTags,
};

var expected = RomajiGenerateResultHelper.ParseRomajiGenerateResults(timeTags, expectedRomajies);
var expected = RomanizationGenerateResultHelper.ParseRomanizationGenerateResults(timeTags, expectedRomanizedSyllables);
CheckGenerateResult(lyric, expected, selector);
}

protected override void AssertEqual(IReadOnlyDictionary<TimeTag, RomajiGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomajiGenerateResult> actual)
protected override void AssertEqual(IReadOnlyDictionary<TimeTag, RomanizationGenerateResult> expected, IReadOnlyDictionary<TimeTag, RomanizationGenerateResult> actual)
{
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Language;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Notes;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.ReferenceLyric;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RubyTags.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags.Zh;
Expand Down Expand Up @@ -50,7 +50,7 @@ protected override void InitialiseDefaults()
SetDefault<ZhTimeTagGeneratorConfig>();

// Romaji generator
SetDefault<JaRomajiGeneratorConfig>();
SetDefault<JaRomanizationGeneratorConfig>();

// Note generator
SetDefault<NoteGeneratorConfig>();
Expand Down Expand Up @@ -79,7 +79,7 @@ protected static KaraokeRulesetEditGeneratorSetting GetSettingByType<TValue>() =
Type t when t == typeof(JaRubyTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaRubyTagGeneratorConfig,
Type t when t == typeof(JaTimeTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaTimeTagGeneratorConfig,
Type t when t == typeof(ZhTimeTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.ZhTimeTagGeneratorConfig,
Type t when t == typeof(JaRomajiGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaRomajiGeneratorConfig,
Type t when t == typeof(JaRomanizationGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaRomajiGeneratorConfig,
Type t when t == typeof(NoteGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.NoteGeneratorConfig,
_ => throw new NotSupportedException(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Language;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Notes;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.ReferenceLyric;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romanization;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RubyTags;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
Expand Down Expand Up @@ -55,7 +55,7 @@ public bool CanGenerate(AutoGenerateType type)
return canGenerate(timeTagGenerator);

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomanizationGenerateResult>, RomanizationGeneratorConfig>();
return canGenerate(timeTagRomajiGenerator);

case AutoGenerateType.AutoGenerateNotes:
Expand Down Expand Up @@ -96,7 +96,7 @@ public IDictionary<Lyric, LocalisableString> GetGeneratorNotSupportedLyrics(Auto
return getInvalidMessageFromGenerator(timeTagGenerator);

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomanizationGenerateResult>, RomanizationGeneratorConfig>();
return getInvalidMessageFromGenerator(timeTagRomajiGenerator);

case AutoGenerateType.AutoGenerateNotes:
Expand Down Expand Up @@ -171,16 +171,16 @@ public void AutoGenerate(AutoGenerateType type)
break;

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomanizationGenerateResult>, RomanizationGeneratorConfig>();
PerformOnSelection(lyric =>
{
var results = timeTagRomajiGenerator.Generate(lyric);

foreach (var (key, value) in results)
{
var matchedTimeTag = lyric.TimeTags.Single(x => x == key);
matchedTimeTag.FirstSyllable = value.InitialRomaji;
matchedTimeTag.RomanizedSyllable = value.RomajiText;
matchedTimeTag.FirstSyllable = value.FirstSyllable;
matchedTimeTag.RomanizedSyllable = value.RomanizedSyllable;
}
});
break;
Expand Down
Loading

0 comments on commit d63d5cf

Please sign in to comment.