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

Implement auto-detect lyric's language. #264

Merged
merged 4 commits into from
Nov 28, 2020
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
@@ -0,0 +1,39 @@
// Copyright (c) andy840119 <[email protected]>. 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.Edit.Generator.Languages;
using osu.Game.Rulesets.Karaoke.Objects;
using System.Globalization;

namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.Languages
{
[TestFixture]
public class LanguageDetectorTest
{
[TestCase("花火大会", "zh-CN")]
[TestCase("花火大會", "zh-TW")]
[TestCase("Testing", "en-US")]
public void TestDetectLanguage(string text, string language)
{
var detector = new LanguageDetector(generageConfig());
var result = detector.DetectLanguage(new Lyric { Text = text });
Assert.AreEqual(result, new CultureInfo(language));
}

[Ignore("Japanese text not supported now")]
[TestCase("ハナビ", "ja-jp")]
[TestCase("はなび", "ja-jp")]
public void TestJapaneseLanguage(string text, string language)
{
// todo : should fix this dictionary to add all Hiragana and Katakana
// https://github.com/pdonald/language-detection/blob/master/LanguageDetection/Profiles/ja
TestDetectLanguage(text, language);
}

private LanguageDetectorConfig generageConfig()
{
return new LanguageDetectorConfig();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Languages
{
public class LanguageDetector
{
private readonly LanguageDetection.LanguageDetector detector = new LanguageDetection.LanguageDetector();

public LanguageDetector(LanguageDetectorConfig config)
{
var targetLanguages = config?.AcceptLanguage?.Where(x => x != null).ToList() ?? new List<CultureInfo>();
if (targetLanguages.Any())
{
detector.AddLanguages(targetLanguages.Select(x => x.Name).ToArray());
}
else
{
detector.AddAllLanguages();
}
}

public CultureInfo DetectLanguage(Lyric lyric)
{
var result = detector.DetectAll(lyric.Text);
var languageCode = result.FirstOrDefault()?.Language;

if (languageCode == null)
return null;

// make some language conversion here
switch (languageCode)
{
// todo : need to think about is this needed?
case "en":
return new CultureInfo("en-US");
default:
return new CultureInfo(languageCode);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;

namespace osu.Game.Rulesets.Karaoke.Edit.Generator.Languages
{
public class LanguageDetectorConfig
{
public CultureInfo[] AcceptLanguage { get; set; }
}
}
1 change: 1 addition & 0 deletions osu.Game.Rulesets.Karaoke/osu.Game.Rulesets.Karaoke.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.13" />
<PackageReference Include="LanguageDetection" Version="1.2.0" />
<PackageReference Include="Octokit" Version="0.48.0" />
<PackageReference Include="osu.Framework.KaraokeFont" Version="1.2.0" />
<PackageReference Include="osu.Framework.Microphone" Version="1.0.10" />
Expand Down