-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88f4c9c
commit d769ea3
Showing
3 changed files
with
139 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace TuneLab.Utils; | ||
|
||
internal static class LyricUtils | ||
{ | ||
public static List<string> SplitLyrics(string lyrics) | ||
{ | ||
List<string> result = new List<string>(); | ||
var splitedLyrics = SplitByInvailidChars(lyrics); | ||
foreach (var lyric in splitedLyrics) | ||
{ | ||
result.AddRange(SplitToWords(lyric)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static List<string> SplitToWords(string lyric) | ||
{ | ||
List<string> lyrics = []; | ||
if (string.IsNullOrEmpty(lyric)) | ||
return lyrics; | ||
|
||
int flag = 0; | ||
bool lastIsLetter = char.IsAsciiLetter(lyric[0]); | ||
for (int i = 1; i < lyric.Length; i++) | ||
{ | ||
bool isLetter = char.IsAsciiLetter(lyric[i]); | ||
if (isLetter && lastIsLetter) | ||
continue; | ||
|
||
lyrics.Add(lyric.Substring(flag, i - flag)); | ||
flag = i; | ||
} | ||
lyrics.Add(lyric.Substring(flag, lyric.Length - flag)); | ||
|
||
return lyrics; | ||
} | ||
|
||
public static IEnumerable<string> SplitByInvailidChars(string lyric) | ||
{ | ||
return lyric.Split(['\n', ' ', '\t', '\r', | ||
'.', ',', '!', '?', ';', ':', '"', '(', ')', '[', ']', '{', '}', '/', '\'', '%', '$', '£', '€', | ||
'。', ',', '!', '?', ';', ':', '“', '”', '‘', '’', '(', ')', '【', '】', '『', '』', '—', '·']) | ||
.Where(s => !string.IsNullOrEmpty(s)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters