Skip to content

Commit

Permalink
Merge pull request #87 from wolfgitpr/master
Browse files Browse the repository at this point in the history
add kana-to-romaji converter
  • Loading branch information
LiuYunPlayer authored Sep 12, 2024
2 parents 73225f2 + 4da6220 commit d4b89b7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
8 changes: 3 additions & 5 deletions TuneLab/Data/Note.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TuneLab.Base.Data;
using TuneLab.Base.Event;
using TuneLab.Base.Properties;
using TuneLab.Base.Structures;
using TuneLab.Utils;
using TuneLab.Base.Utils;
using TuneLab.Extensions.Formats.DataInfo;
using TuneLab.Extensions.Voices;
using TuneLab.Base.Utils;
using TuneLab.Utils;

namespace TuneLab.Data;

Expand Down
1 change: 1 addition & 0 deletions TuneLab/TuneLab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<!--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-kana" Version="1.0.1" />
<PackageReference Include="csharp-pinyin" Version="1.0.0" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Markdown.Avalonia" Version="11.0.2" />
Expand Down
44 changes: 25 additions & 19 deletions TuneLab/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Pinyin;
using System.Collections.Generic;
using System.Linq;
using TuneLab.Base.Utils;

namespace TuneLab.Utils;

Expand All @@ -16,11 +15,22 @@ public struct LyricResult

public static List<LyricResult> Split(string lyrics)
{
var pinyinResults = Pinyin.Pinyin.Instance.HanziToPinyin(lyrics, ManTone.Style.NORMAL, Error.Default, true, false);
var lyricList = SplitToWords(lyrics);
// 设置Error.Default、转换失败时,发音元素会返回原文本,error为true;Size与输入列表相同
var pinyinResults = Pinyin.Pinyin.Instance.HanziToPinyin(lyricList, ManTone.Style.NORMAL, Pinyin.Error.Default, true, false);
var kanaResults = Kana.Kana.KanaToRomaji(lyricList);

var results = new List<LyricResult>();
foreach (var pinyinRes in pinyinResults)

for (int i = 0; i < lyricList.Count; i++)
{
results.Add(new LyricResult() { Lyric = pinyinRes.hanzi, Pronunciation = pinyinRes.pinyin/*, Candidates = pinyinRes.candidates*/ });
if (pinyinResults[i].error == false)
results.Add(new LyricResult() { Lyric = pinyinResults[i].hanzi, Pronunciation = pinyinResults[i].pinyin/*, Candidates = pinyinRes.candidates*/ });
else if (kanaResults[i].Error == false)
results.Add(new LyricResult() { Lyric = kanaResults[i].Kana, Pronunciation = kanaResults[i].Romaji });
else
results.Add(new LyricResult() { Lyric = lyricList[i], Pronunciation = string.Empty });

}
return results;
}
Expand Down Expand Up @@ -52,26 +62,22 @@ public static IEnumerable<string> SplitByInvailidChars(string lyric)

public static string GetPreferredPronunciation(string lyric)
{
var pinyins = Pinyin.Pinyin.Instance.HanziToPinyin(lyric, ManTone.Style.NORMAL, Error.Default, true, false);
if (pinyins.IsEmpty())
return string.Empty;
// 校验字符后可直接返回发音
if (Pinyin.Pinyin.Instance.IsHanzi(lyric))
return Pinyin.Pinyin.Instance.GetDefaultPinyin(lyric, ManTone.Style.NORMAL, false, false)[0];
else if (Kana.Kana.IsKana(lyric))
return Kana.Kana.KanaToRomaji(lyric)[0].Romaji;

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

return pinyin.pinyin;
return string.Empty;
}

public static IReadOnlyCollection<string> GetPronunciations(string lyric)
{
if (lyric.Length == 1)
{
if (Pinyin.Pinyin.Instance.IsHanzi(lyric))
{
return Pinyin.Pinyin.Instance.GetDefaultPinyin(lyric, ManTone.Style.NORMAL, false, false);
}
}
// 校验字符后可直接返回发音
if (Pinyin.Pinyin.Instance.IsHanzi(lyric))
return Pinyin.Pinyin.Instance.GetDefaultPinyin(lyric, ManTone.Style.NORMAL, false, false);
else if (Kana.Kana.IsKana(lyric))
return Kana.Kana.KanaToRomaji(lyric).ToStrList();

return [];
}
Expand Down

0 comments on commit d4b89b7

Please sign in to comment.