Skip to content

Commit

Permalink
优化歌词转拼音的正确率
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Jun 22, 2024
1 parent a1e17d7 commit 30dd6cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions TuneLab/Data/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ protected override void Set(string value)
{
base.Set(value);
mNote.Phonemes.Clear();
mNote.Pronunciation.Set(string.Empty);
mNote.Pronunciation.Set(LyricUtils.GetPreferredPronunciation(Value));
}

Note mNote;
readonly Note mNote;
}

class DataPronunciation(Note note) : DataString(note)
Expand Down
42 changes: 37 additions & 5 deletions TuneLab/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
using IKg2p;
using Microsoft.International.Converters.PinYinConverter;
using System.Collections.Generic;
using System.Linq;
using TuneLab.Base.Structures;

namespace TuneLab.Utils;

internal static class LyricUtils
{
public struct LyricResult
{
public string Lyric;
public string Pronunciation;
/*public IReadOnlyList<string> Candidates;*/
}

public static List<LyricResult> Split(string lyrics)
{
var g2pResults = ZhG2p.MandarinInstance.Convert(lyrics, false, true);
var results = new List<LyricResult>();
foreach (var g2pRes in g2pResults)
{
results.Add(new LyricResult() { Lyric = g2pRes.lyric, Pronunciation = g2pRes.syllable/*, Candidates = g2pRes.candidates*/ });
}
return results;
}

public static List<string> SplitLyrics(string lyrics)
{
List<string> result = new List<string>();
var result = new List<string>();
var splitedLyrics = SplitByInvailidChars(lyrics);
foreach (var lyric in splitedLyrics)
{
Expand All @@ -31,13 +51,25 @@ public static IEnumerable<string> SplitByInvailidChars(string lyric)
.Where(s => !string.IsNullOrEmpty(s));
}

public static string GetPreferredPronunciation(string lyric)
{
var pinyin = ZhG2p.MandarinInstance.Convert(lyric, false, true)[0];
if (!pinyin.error)
return pinyin.syllable;

return string.Empty;
}

public static IReadOnlyCollection<string> GetPronunciations(string lyric)
{
if (lyric.Length == 1)
{
var pinyin = ZhG2p.MandarinInstance.Convert(lyric, false, true)[0];
if (!pinyin.error)
return new List<string> { pinyin.syllable }.AsReadOnly();
var c = lyric[0];
if (ChineseChar.IsValidChar(c))
{
var chineseChar = new ChineseChar(c);
return chineseChar.Pinyins.Take(chineseChar.PinyinCount).Convert(ToPinyin).ToHashSet();
}
}

return [];
Expand All @@ -49,7 +81,7 @@ static string ToPinyin(string pinyin)
return string.Empty;

if (char.IsNumber(pinyin[^1]))
return pinyin.Substring(0, pinyin.Length - 1).ToLower();
return pinyin[..^1].ToLower();

return pinyin.ToLower();
}
Expand Down
11 changes: 8 additions & 3 deletions TuneLab/Views/LyricInput.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,20 @@ void OnLyricInputConfirm()
if (mNotes.Count == 0)
return;

var lyrics = LyricUtils.SplitLyrics(mLyricInputBox.Text);
var lyricResults = LyricUtils.Split(mLyricInputBox.Text);
var notes = mSkipTenutoCheckBox.IsChecked ? mNotes.Where(note => note.Lyric.Value != "-") : mNotes;
using var enumerator = (mSkipTenutoCheckBox.IsChecked ? lyrics.Where(lyric => lyric != "-") : lyrics).GetEnumerator();
using var enumerator = (mSkipTenutoCheckBox.IsChecked ? lyricResults.Where(lyricResult => lyricResult.Lyric != "-") : lyricResults).GetEnumerator();
foreach (var note in notes)
{
if (!enumerator.MoveNext())
break;

note.Lyric.Set(enumerator.Current);
var current = enumerator.Current;
note.Lyric.Set(current.Lyric);
if (current.Lyric == "-")
continue;

note.Pronunciation.Set(current.Pronunciation);
}

mNotes.First().Commit();
Expand Down

0 comments on commit 30dd6cf

Please sign in to comment.