From c01c0fa864db2cc1c2d4e8d002c5a0dab562c6ff Mon Sep 17 00:00:00 2001 From: andy840119 Date: Wed, 2 Dec 2020 11:07:41 +0900 Subject: [PATCH 1/3] Creater default class for implementation. --- .../TimeTags/Zh/ZhTimeTagGeneratorTest.cs | 17 +++++++++++++++++ .../Generator/TimeTags/Zh/ZhTimeTagGenerator.cs | 9 +++++++++ .../TimeTags/Zh/ZhTimeTagGeneratorConfig.cs | 9 +++++++++ 3 files changed, 35 insertions(+) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs create mode 100644 osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorConfig.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs new file mode 100644 index 000000000..cc9739ec8 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs @@ -0,0 +1,17 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Linq; +using NUnit.Framework; +using osu.Framework.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Zh; +using osu.Game.Rulesets.Karaoke.Objects; + +namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.TimeTags.Zh +{ + [TestFixture] + public class ZhTimeTagGeneratorTest + { + } +} diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs new file mode 100644 index 000000000..acd570c80 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs @@ -0,0 +1,9 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +namespace osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Zh +{ + public class ZhTimeTagGenerator : TimeTagGenerator + { + } +} diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorConfig.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorConfig.cs new file mode 100644 index 000000000..0474bc169 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorConfig.cs @@ -0,0 +1,9 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +namespace osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Zh +{ + public class ZhTimeTagGeneratorConfig : TimeTagGeneratorConfig + { + } +} From 0a5339818ae54ee1bbdfb615779acbd1a266b0b0 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Thu, 3 Dec 2020 21:55:49 +0900 Subject: [PATCH 2/3] Add utility to check is chinses character. And it's test. --- .../Utils/ZhStringUtilsTest.cs | 30 +++++++++++++++++++ .../Utils/ZhStringUtils.cs | 18 +++++++++++ 2 files changed, 48 insertions(+) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs new file mode 100644 index 000000000..3abe19db7 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs @@ -0,0 +1,30 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Game.Rulesets.Karaoke.Utils; + +namespace osu.Game.Rulesets.Karaoke.Tests.Utils +{ + [TestFixture] + public class ZhStringUtilsTest + { + [TestCase('你', true)] + [TestCase('好', true)] + [TestCase('世', true)] + [TestCase('界', true)] + [TestCase('A', false)] + [TestCase('a', false)] + [TestCase('A', false)] + [TestCase('a', false)] + [TestCase('~', false)] + [TestCase('~', false)] + [TestCase('ハ', false)] + [TestCase('は', false)] + [TestCase('ハ', false)] + public void TestIsChinese(char c, bool result) + { + Assert.AreEqual(ZhStringUtils.IsChinese(c), result); + } + } +} diff --git a/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs b/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs new file mode 100644 index 000000000..ca4236c38 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs @@ -0,0 +1,18 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Text.Unicode; + +namespace osu.Game.Rulesets.Karaoke.Utils +{ + public static class ZhStringUtils + { + public static bool IsChinese(char character) + { + // From : https://stackoverflow.com/a/61738863/4105113 + var minValue = UnicodeRanges.CjkUnifiedIdeographs.FirstCodePoint; + var maxValue = minValue + UnicodeRanges.CjkUnifiedIdeographs.Length; + return character >= minValue && character < maxValue; + } + } +} From 8af76b38125f0aadd8d0bb28666cdf14705b6e86 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Thu, 3 Dec 2020 22:10:50 +0900 Subject: [PATCH 3/3] Implement time creator with not much setting. Also move isChinses method to CharUtils. --- .../TimeTags/Zh/ZhTimeTagGeneratorTest.cs | 58 +++++++++++++++++++ .../Utils/CharUtilsTest.cs | 19 ++++++ .../Utils/ZhStringUtilsTest.cs | 30 ---------- .../TimeTags/Zh/ZhTimeTagGenerator.cs | 22 +++++++ osu.Game.Rulesets.Karaoke/Utils/CharUtils.cs | 15 +++++ .../Utils/ZhStringUtils.cs | 18 ------ 6 files changed, 114 insertions(+), 48 deletions(-) delete mode 100644 osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs delete mode 100644 osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs index cc9739ec8..24e4603ff 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Edit/Generator/TimeTags/Zh/ZhTimeTagGeneratorTest.cs @@ -13,5 +13,63 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.TimeTags.Zh [TestFixture] public class ZhTimeTagGeneratorTest { + [TestCase("測試一些歌詞", new double[] { 0, 1, 2, 3, 4, 5, 5.5 })] + [TestCase("拉拉拉~~~", new double[] { 0, 1, 2, 5.5 })] + [TestCase("拉~拉~拉~", new double[] { 0, 2, 4, 5.5 })] + public void TestLyricWithCheckLineEndKeyUp(string lyric, double[] index) + { + var config = generatorConfig(nameof(ZhTimeTagGeneratorConfig.CheckLineEndKeyUp)); + RunTimeTagCheckTest(lyric, index, config); + } + + #region test helper + + protected void RunTimeTagCheckTest(string lyricText, double[] index, ZhTimeTagGeneratorConfig config) + { + var generator = new ZhTimeTagGenerator(config); + var lyric = generateLyric(lyricText); + + // create time tag and actually time tag. + var timeTags = getTimeTagIndex(generator.CreateTimeTags(lyric)); + var actualIndexed = getTimeTagIndexByArray(index); + + // check should be equal + Assert.AreEqual(timeTags, actualIndexed); + } + + private TimeTagIndex[] getTimeTagIndex(Tuple[] timeTags) + => timeTags.Select((v, i) => v.Item1).ToArray(); + + private ZhTimeTagGeneratorConfig generatorConfig(params string[] properties) + { + var config = new ZhTimeTagGeneratorConfig(); + + foreach (var propertyName in properties) + { + if (propertyName == null) + continue; + + var theMethod = config.GetType().GetProperty(propertyName); + if (theMethod == null) + throw new MissingMethodException("Config is not exist."); + + theMethod.SetValue(config, true); + } + + return config; + } + + private TimeTagIndex[] getTimeTagIndexByArray(double[] timeTags) + => timeTags.Select(timeTag => + { + var state = Math.Abs(timeTag) % 1 == 0.5 ? TimeTagIndex.IndexState.End : TimeTagIndex.IndexState.Start; + var index = (int)timeTag; + return new TimeTagIndex(index, state); + }).ToArray(); + + private Lyric generateLyric(string text) + => new Lyric { Text = text }; + + #endregion } } diff --git a/osu.Game.Rulesets.Karaoke.Tests/Utils/CharUtilsTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Utils/CharUtilsTest.cs index 0fac62491..27d244fa4 100644 --- a/osu.Game.Rulesets.Karaoke.Tests/Utils/CharUtilsTest.cs +++ b/osu.Game.Rulesets.Karaoke.Tests/Utils/CharUtilsTest.cs @@ -48,5 +48,24 @@ public void TestIsAsciiSymbol(char c, bool match) var isAsciiSymbol = CharUtils.IsAsciiSymbol(c); Assert.AreEqual(isAsciiSymbol, match); } + + [TestCase('你', true)] + [TestCase('好', true)] + [TestCase('世', true)] + [TestCase('界', true)] + [TestCase('A', false)] + [TestCase('a', false)] + [TestCase('A', false)] + [TestCase('a', false)] + [TestCase('~', false)] + [TestCase('~', false)] + [TestCase('ハ', false)] + [TestCase('は', false)] + [TestCase('ハ', false)] + public void TestIsChinese(char c, bool result) + { + var isChinses = CharUtils.IsChinese(c); + Assert.AreEqual(isChinses, result); + } } } diff --git a/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs deleted file mode 100644 index 3abe19db7..000000000 --- a/osu.Game.Rulesets.Karaoke.Tests/Utils/ZhStringUtilsTest.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) andy840119 . Licensed under the GPL Licence. -// See the LICENCE file in the repository root for full licence text. - -using NUnit.Framework; -using osu.Game.Rulesets.Karaoke.Utils; - -namespace osu.Game.Rulesets.Karaoke.Tests.Utils -{ - [TestFixture] - public class ZhStringUtilsTest - { - [TestCase('你', true)] - [TestCase('好', true)] - [TestCase('世', true)] - [TestCase('界', true)] - [TestCase('A', false)] - [TestCase('a', false)] - [TestCase('A', false)] - [TestCase('a', false)] - [TestCase('~', false)] - [TestCase('~', false)] - [TestCase('ハ', false)] - [TestCase('は', false)] - [TestCase('ハ', false)] - public void TestIsChinese(char c, bool result) - { - Assert.AreEqual(ZhStringUtils.IsChinese(c), result); - } - } -} diff --git a/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs index acd570c80..44fbfea62 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Generator/TimeTags/Zh/ZhTimeTagGenerator.cs @@ -1,9 +1,31 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Objects; +using osu.Game.Rulesets.Karaoke.Utils; +using System; +using System.Collections.Generic; + namespace osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Zh { public class ZhTimeTagGenerator : TimeTagGenerator { + public ZhTimeTagGenerator(ZhTimeTagGeneratorConfig config) + : base(config) + { + } + + protected override void TimeTagLogic(Lyric lyric, List> timeTags) + { + var text = lyric.Text; + for (var i = 1; i < text.Length; i++) + { + if (CharUtils.IsChinese(text[i])) + { + timeTags.Add(TimeTagsUtils.Create(new TimeTagIndex(i, TimeTagIndex.IndexState.Start), null)); + } + } + } } } diff --git a/osu.Game.Rulesets.Karaoke/Utils/CharUtils.cs b/osu.Game.Rulesets.Karaoke/Utils/CharUtils.cs index cf4c2aeb9..54a4c5558 100644 --- a/osu.Game.Rulesets.Karaoke/Utils/CharUtils.cs +++ b/osu.Game.Rulesets.Karaoke/Utils/CharUtils.cs @@ -1,6 +1,8 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System.Text.Unicode; + namespace osu.Game.Rulesets.Karaoke.Utils { public static class CharUtils @@ -43,5 +45,18 @@ public static bool IsAsciiSymbol(char c) c >= '[' && c <= '`' || c >= '{' && c <= '~'; } + + /// + /// Check this char is chinese character + /// + /// + /// + public static bool IsChinese(char character) + { + // From : https://stackoverflow.com/a/61738863/4105113 + var minValue = UnicodeRanges.CjkUnifiedIdeographs.FirstCodePoint; + var maxValue = minValue + UnicodeRanges.CjkUnifiedIdeographs.Length; + return character >= minValue && character < maxValue; + } } } diff --git a/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs b/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs deleted file mode 100644 index ca4236c38..000000000 --- a/osu.Game.Rulesets.Karaoke/Utils/ZhStringUtils.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) andy840119 . Licensed under the GPL Licence. -// See the LICENCE file in the repository root for full licence text. - -using System.Text.Unicode; - -namespace osu.Game.Rulesets.Karaoke.Utils -{ - public static class ZhStringUtils - { - public static bool IsChinese(char character) - { - // From : https://stackoverflow.com/a/61738863/4105113 - var minValue = UnicodeRanges.CjkUnifiedIdeographs.FirstCodePoint; - var maxValue = minValue + UnicodeRanges.CjkUnifiedIdeographs.Length; - return character >= minValue && character < maxValue; - } - } -}