Skip to content

Commit

Permalink
Merge pull request #272 from andy840119/ruby/prevent-duplicated-tag
Browse files Browse the repository at this point in the history
Add option to make create duplicated ruby as false.
  • Loading branch information
andy840119 authored Nov 29, 2020
2 parents 4fc1c29 + fb204ef commit cff3a17
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,51 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.RubyTags.Ja
[TestFixture]
public class JaRubyTagGeneratorTest
{
[TestCase("花火大会", new[] { "はなび", "たいかい" }, false)]
[TestCase("花火大会", new[] { "ハナビ", "タイカイ" }, true)]
public void TestLyricWithCheckLineEndKeyUpWithRubyAsKatakana(string text, string[] ruby, bool applyConfig)
[TestCase("花火大会", new[] { "はなび", "たいかい" })]
[TestCase("はなび", new string[] { })]
public void TestCreateRubyTags(string text, string[] ruby)
{
var config = generatorConfig(null);
RunRubyCheckTest(text, ruby, config);
}

[TestCase("花火大会", new[] { "ハナビ", "タイカイ" })]
[TestCase("ハナビ", new string[] { })]
public void TestCreateRubyTagsWithRubyAsKatakana(string text, string[] ruby)
{
var config = generatorConfig(nameof(JaRubyTagGeneratorConfig.RubyAsKatakana));
RunRubyCheckTest(text, ruby, config);
}

[TestCase("はなび", new string[] { "はな", "び" })]
[TestCase("ハナビ", new string[] { "はなび" })]
public void TestCreateRubyTagsWithEnableDuplicatedRuby(string text, string[] ruby)
{
var config = generatorConfig(nameof(JaRubyTagGeneratorConfig.EnableDuplicatedRuby));
RunRubyCheckTest(text, ruby, config);
}

#region test helper

protected void RunRubyCheckTest(string text, string[] ruby, JaRubyTagGeneratorConfig config)
{
var config = generatorConfig(applyConfig ? nameof(JaRubyTagGeneratorConfig.RubyAsKatakana) : null);
var generator = new JaRubyTagGenerator(config);

var lyric = new Lyric { Text = text };
var rubyTags = generator.CreateRubyTags(lyric);

Assert.AreEqual(rubyTags.Length, ruby.Length);
foreach (var rubyTag in rubyTags)
{
Assert.IsTrue(ruby.Contains(rubyTag.Text));
}
}

#region test helper

private Lyric generateLyric(string text)
=> new Lyric { Text = text };

private JaRubyTagGeneratorConfig generatorConfig(params string[] properties)
{
var config = new JaRubyTagGeneratorConfig();
if (properties == null)
return config;

foreach (var propertyName in properties)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ public class JaTimeTagGeneratorTest
public void TestLyricWithCheckLineEnd(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckLineEnd) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("か", new double[] { 0 }, false)]
[TestCase("か", new[] { 0, 0.5 }, true)]
public void TestLyricWithCheckLineEndKeyUp(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckLineEndKeyUp) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[Ignore("This feature has not been implemented")]
public void TestLyricWithCheckBlankLine(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckBlankLine) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase(" ", new double[] { 0, 1, 2, 3, 4 }, false)]
[TestCase(" ", new double[] { 0 }, true)]
public void TestLyricWithCheckWhiteSpace(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[Ignore("This feature has not been implemented")]
public void TestLyricWithCheckWhiteSpaceKeyUp(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("a b c d e", new double[] { 0, 2, 4, 6, 8 }, false)]
Expand All @@ -58,7 +58,7 @@ public void TestLyricWithCheckWhiteSpaceAlphabet(string lyric, double[] index, b
{
var config = generatorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAlphabet) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("0 1 2 3 4", new double[] { 0, 2, 4, 6, 8 }, false)]
Expand All @@ -69,7 +69,7 @@ public void TestLyricWithCheckWhiteSpaceDigit(string lyric, double[] index, bool
{
var config = generatorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceDigit) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("! ! ! ! !", new double[] { 0, 2, 4, 6, 8 }, false)]
Expand All @@ -78,28 +78,28 @@ public void TestLyricWitCheckWhiteSpaceAsciiSymbol(string lyric, double[] index,
{
var config = generatorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAsciiSymbol) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("がんばって", new double[] { 0, 2, 4 }, false)]
[TestCase("がんばって", new double[] { 0, 1, 2, 4 }, true)]
public void TestLyricWithCheckWhiteCheckん(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.Checkん) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

[TestCase("買って", new double[] { 0, 2 }, false)]
[TestCase("買って", new double[] { 0, 1, 2 }, true)]
public void TestLyricWithCheckっ(string lyric, double[] index, bool applyConfig)
{
var config = generatorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.Checkっ) : null);
RunTimeTagCheckText(lyric, index, config);
RunTimeTagCheckTest(lyric, index, config);
}

#region test helper

protected void RunTimeTagCheckText(string lyricText, double[] index, JaTimeTagGeneratorConfig config)
protected void RunTimeTagCheckTest(string lyricText, double[] index, JaTimeTagGeneratorConfig config)
{
var generator = new JaTimeTagGenerator(config);
var lyric = generateLyric(lyricText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ public override RubyTag[] CreateRubyTags(Lyric lyric)
tokenStream.IncrementToken();

// Get parsed result, result is Katakana.
var parsedResult = result.ToString();
if (string.IsNullOrEmpty(parsedResult))
var katakana = result.ToString();
if (string.IsNullOrEmpty(katakana))
break;

// Convert to Hiragana as default.
if (!Config.RubyAsKatakana)
var hiragana = JpStringUtils.ToHiragana(katakana);
if (!Config.EnableDuplicatedRuby)
{
parsedResult = JpStringUtils.ToHiragana(parsedResult);
// Not add deplicated ruby if same as parent.
var parentText = text.Substring(offsetAtt.StartOffset, offsetAtt.EndOffset - offsetAtt.StartOffset);
if (parentText == katakana || parentText == hiragana)
continue;
}

// Make tag
tags.Add(new RubyTag
{
Text = parsedResult,
Text = Config.RubyAsKatakana ? katakana : hiragana,
StartIndex = offsetAtt.StartOffset,
EndIndex = offsetAtt.EndOffset
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ namespace osu.Game.Rulesets.Karaoke.Edit.Generator.RubyTags.Ja
{
public class JaRubyTagGeneratorConfig : RubyTagGeneratorConfig
{
/// <summary>
/// Generate ruby as Katakana.
/// </summary>
public bool RubyAsKatakana { get; set; }

/// <summary>
/// Generate ruby even it's same as lyric.
/// </summary>
public bool EnableDuplicatedRuby { get; set; }
}
}

0 comments on commit cff3a17

Please sign in to comment.