From fd2ff711b3b5195d42813a39c2c19ba8ad3038fa Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 29 Nov 2020 17:17:34 +0900 Subject: [PATCH 1/2] Add option to make create duplicated ruby as false. --- .../RubyTags/Ja/JaRubyTagGeneratorTest.cs | 40 ++++++++++++++----- .../TimeTags/Ja/JaTimeTagGeneratorTest.cs | 22 +++++----- .../RubyTags/Ja/JaRubyTagGenerator.cs | 14 ++++--- .../RubyTags/Ja/JaRubyTagGeneratorConfig.cs | 8 ++++ 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs index 9cf532fb7..13a89cc11 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs @@ -12,30 +12,52 @@ 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) { diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Ja/JaTimeTagGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Ja/JaTimeTagGeneratorTest.cs index ab2795ca4..bd3da8f6e 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Ja/JaTimeTagGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Ja/JaTimeTagGeneratorTest.cs @@ -17,7 +17,7 @@ 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)] @@ -25,14 +25,14 @@ public void TestLyricWithCheckLineEnd(string lyric, double[] index, bool applyCo 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)] @@ -40,14 +40,14 @@ public void TestLyricWithCheckBlankLine(string lyric, double[] index, bool apply 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)] @@ -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)] @@ -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)] @@ -78,7 +78,7 @@ 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)] @@ -86,7 +86,7 @@ public void TestLyricWitCheckWhiteSpaceAsciiSymbol(string lyric, double[] index, 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)] @@ -94,12 +94,12 @@ public void TestLyricWithCheckWhiteCheckん(string lyric, double[] index, bool a 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); diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGenerator.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGenerator.cs index 84f8869a9..1131b2aa0 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGenerator.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGenerator.cs @@ -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 }); diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorConfig.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorConfig.cs index dc6bfd013..3cd73bf0c 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorConfig.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorConfig.cs @@ -5,6 +5,14 @@ namespace osu.Game.Rulesets.Karaoke.Edit.Generator.RubyTags.Ja { public class JaRubyTagGeneratorConfig : RubyTagGeneratorConfig { + /// + /// Generate ruby as Katakana. + /// public bool RubyAsKatakana { get; set; } + + /// + /// Generate ruby even it's same as lyric. + /// + public bool EnableDuplicatedRuby { get; set; } } } From fb204ef101b078654a54e7a336c0ad617c84879e Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 29 Nov 2020 17:20:31 +0900 Subject: [PATCH 2/2] Fix codefactor's complain. --- .../Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs index 13a89cc11..b687bfdd1 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/RubyTags/Ja/JaRubyTagGeneratorTest.cs @@ -45,7 +45,6 @@ protected void RunRubyCheckTest(string text, string[] ruby, JaRubyTagGeneratorCo var lyric = new Lyric { Text = text }; var rubyTags = generator.CreateRubyTags(lyric); - Assert.AreEqual(rubyTags.Length, ruby.Length); foreach (var rubyTag in rubyTags) {