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

update chinese g2p #86

Merged
merged 1 commit into from
Sep 10, 2024
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
2 changes: 1 addition & 1 deletion TuneLab/TuneLab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.11" />
<PackageReference Include="BunLabs.NAudio.Flac" Version="2.0.1" />
<PackageReference Include="csharp-pinyin" Version="1.0.0" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="IkG2p" Version="1.0.2" />
<PackageReference Include="Markdown.Avalonia" Version="11.0.2" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.6" />
<PackageReference Include="NAudio" Version="2.2.1" />
Expand Down
33 changes: 9 additions & 24 deletions TuneLab/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using IKg2p;
using Microsoft.International.Converters.PinYinConverter;
using Pinyin;
using System.Collections.Generic;
using System.Linq;
using TuneLab.Base.Structures;
using TuneLab.Base.Utils;

namespace TuneLab.Utils;
Expand All @@ -18,11 +16,11 @@ public struct LyricResult

public static List<LyricResult> Split(string lyrics)
{
var g2pResults = ZhG2p.MandarinInstance.Convert(lyrics, false, true);
var pinyinResults = Pinyin.Pinyin.Instance.HanziToPinyin(lyrics, ManTone.Style.NORMAL, Error.Default, true, false);
var results = new List<LyricResult>();
foreach (var g2pRes in g2pResults)
foreach (var pinyinRes in pinyinResults)
{
results.Add(new LyricResult() { Lyric = g2pRes.lyric, Pronunciation = g2pRes.syllable/*, Candidates = g2pRes.candidates*/ });
results.Add(new LyricResult() { Lyric = pinyinRes.hanzi, Pronunciation = pinyinRes.pinyin/*, Candidates = pinyinRes.candidates*/ });
}
return results;
}
Expand All @@ -41,7 +39,7 @@ public static List<string> SplitLyrics(string lyrics)

public static List<string> SplitToWords(string lyric)
{
return ZhG2p.SplitString(lyric);
return Pinyin.ChineseG2p.SplitString(lyric);
}

public static IEnumerable<string> SplitByInvailidChars(string lyric)
Expand All @@ -54,40 +52,27 @@ public static IEnumerable<string> SplitByInvailidChars(string lyric)

public static string GetPreferredPronunciation(string lyric)
{
var pinyins = ZhG2p.MandarinInstance.Convert(lyric, false);
var pinyins = Pinyin.Pinyin.Instance.HanziToPinyin(lyric, ManTone.Style.NORMAL, Error.Default, true, false);
if (pinyins.IsEmpty())
return string.Empty;

var pinyin = pinyins[0];
if (pinyin.error)
return string.Empty;

return pinyin.syllable;
return pinyin.pinyin;
}

public static IReadOnlyCollection<string> GetPronunciations(string lyric)
{
if (lyric.Length == 1)
{
var c = lyric[0];
if (ChineseChar.IsValidChar(c))
if (Pinyin.Pinyin.Instance.IsHanzi(lyric))
{
var chineseChar = new ChineseChar(c);
return chineseChar.Pinyins.Take(chineseChar.PinyinCount).Convert(ToPinyin).ToHashSet();
return Pinyin.Pinyin.Instance.GetDefaultPinyin(lyric, ManTone.Style.NORMAL, false, false);
}
}

return [];
}

static string ToPinyin(string pinyin)
{
if (string.IsNullOrEmpty(pinyin))
return string.Empty;

if (char.IsNumber(pinyin[^1]))
return pinyin[..^1].ToLower();

return pinyin.ToLower();
}
}
Loading