-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtilitiy.cs
233 lines (208 loc) · 9.17 KB
/
StringUtilitiy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NHMPh_music_player
{
internal static class StringUtilitiy
{
//Number to word converter <10
public static string ReplaceNumbersWithWords(string input)
{
string[] parts = input.Split(' ');
for (int i = 0; i < parts.Length; i++)
{
// Attempt to parse the substring as an integer
if (int.TryParse(parts[i], out int num))
{
// If the parsed number is less than 10, replace it with its string representation
if (num < 10)
{
Console.WriteLine(num + " Found " + parts[i]);
for (int j = 1; j <= 10; j++)
{
parts[i] = NumberToWords(num);
}
}
}
}
input = string.Join(" ", parts);
return input;
}
private static string NumberToWords(int number)
{
if (number < 1 || number > 10)
{
throw new ArgumentOutOfRangeException("Number must be between 1 and 10.");
}
return numberWords[number];
}
private static readonly Dictionary<int, string> numberWords = new Dictionary<int, string>()
{
{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"},
{6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"}, {10, "ten"}
};
public static int ExtractTrackId(string input)
{
// Define a regular expression pattern to match the ID number
string pattern = @"^(\d+)\.";
// Match the pattern in the input string
Match match = Regex.Match(input, pattern);
// Check if the match is successful and extract the ID
if (match.Success)
{
// Extract the ID from the first capturing group
if (int.TryParse(match.Groups[1].Value, out int id))
{
return id;
}
}
// Return -1 if ID extraction fails
return -1;
}
private static readonly Dictionary<string, string> songException = new Dictionary<string, string>()
{
{ "WITHYOU", "With You Hoaprox"}, { "BoneyM-Rasputin", "Rasputin"},{ "BoneyM.-Rasputin", "Rasputin"},{"DMDOKURO-Stained,BrutalCalamity", "Stained, Brutal Calamity"},{"NineInchNails-HurtLyricsVideo","Nine Inch Nails - Hurt " },{"FoolsGarden-LemonTree","Lemon Tree"},{"Fool'sGarden-LemonTree","Lemon Tree"},{"O-Zone-DragosteaDinTei","O-zone"}
};
public static string ProcessInvailName(string name)
{
if (name.Contains('‒'))
name = name.Replace('‒', '-');
if (name.Contains(" - Tik Tok"))
name = name.Replace(" - Tik Tok", "");
if(name.Contains(" x "))
name = name.Replace(" x ", " ");
name = Regex.Replace(name, @"(\([^)]*\)|\[[^\]]*\])|【|】|""""[^""""]*""""""", "");
string result = name;
if (!result.Contains("-"))
{
result = Regex.Replace(result, @"\|.*$|(\([^)]*\)|\[[^\]]*\])|ft\..*|FT\..*|Ft\..*|feat\..*|Feat\..*|FEAT\..*|【|】|""[^""]*""|LYRICS|VIDEO|★|!", "");
try { result = songException[result.Replace(" ", "")]; } catch { };
return result.Replace(" ", "%20");
}
string[] parts = Regex.Split(result, @"(?<=\s-\s)|(?<=\s--\s)|(?<=-\s)");
// string combinedPattern = @"\|.*$|(\([^)]*\)|\[[^\]]*\])|ft\..*|FT\..*|Ft\..*|feat\..*|Feat\..*|FEAT\..*|【|】|""[^""]*""|LYRICS|VIDEO";
string combinedPattern = @"\|.*$|(\([^)]*\)|\[[^\]]*\])|【|】|""[^""]*""|LYRICS|VIDEO|★|!";
parts[0] = Regex.Replace(parts[0], combinedPattern, "");
parts[1] = Regex.Replace(parts[1], combinedPattern, "");
if (parts[0].Contains(','))
parts[0] = parts[0].Replace(',', '&');
parts[0] = Regex.Replace(parts[0], @"(?<!\w)x(?!x|\w)", ",");
if (parts[0].Contains('&'))
{
parts[0] = parts[0].Split('&')[parts[0].Split('&').Length - 1];
}
parts[1] = ReplaceNumbersWithWords(parts[1]);
string processName = parts[0] + "" + parts[1];
Console.WriteLine(processName.Replace(" ", ""));
try { processName = songException[processName.Replace(" ", "")]; } catch { }
return processName.Replace(" ", "%20");
}
public static string ExtractId(string href)
{
int indexV = href.IndexOf("v=");
if (indexV != -1)
{
// Extract the video ID starting from the index of "v="
string videoId = href.Substring(indexV + 2);
// Remove any additional parameters by finding the index of "&"
int indexAmpersand = videoId.IndexOf("&");
if (indexAmpersand != -1)
{
videoId = videoId.Substring(0, indexAmpersand);
}
return videoId;
}
return null;
}
public static int EvaluateKeyWord(string key)
{
if (IsYouTubePlaylistLink(key))
{
Console.WriteLine("playlink");
return 2;
}
else if (IsYouTubeVideoLink(key))
{
Console.WriteLine("link");
return 1;
}
else
{
Console.WriteLine("search");
return 3;
}
}
public static bool IsYouTubeVideoLink(string input)
{
string pattern = @"^(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)";
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
return match.Success;
}
public static bool IsYouTubePlaylistLink(string input)
{
string pattern = @"^(https?://)?(www\.)?(youtube\.com/playlist\?list=|youtube\.com/watch\?v=.+&list=)([a-zA-Z0-9_-]+)";
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
return match.Success;
}
public static bool IsYouTubeAutoPlaylistLink(string input)
{
string pattern = @"^(https?://)?(www\.)?(youtube\.com/playlist\?list=|youtube\.com/watch\?v=.+&((list=[a-zA-Z0-9_-]+)|list=RD[a-zA-Z0-9_-]+(&start_radio=\d)?))";
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
return match.Success;
}
public static string RemoveIdAndParentheses(string input)
{
// Define a regular expression pattern to match the ID and parentheses and content inside parentheses
string pattern = @"^\d+\.\s|\([^()]*\)";
// Replace the matched pattern with an empty string
string result = Regex.Replace(input, pattern, "");
return result;
}
public static JObject ReadJsonFile(string filePath)
{
if (System.IO.File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
string jsonString = reader.ReadToEnd();
return JObject.Parse(jsonString);
}
}
else { Console.WriteLine("NotFOund"); }
return null;
}
public static List<(double, string)> ExtractAndParseTimestampsAndLyricsToMilliseconds(string text)
{
List<(double, string)> timestampedLyrics = new List<(double, string)>();
// Regular expression to match the timestamps and lyrics
Regex regex = new Regex(@"\[(\d{2}):(\d{2})\.(\d{2})\] (.*)");
MatchCollection matches = regex.Matches(text);
foreach (Match match in matches)
{
if (match.Success)
{
// Extract minutes, seconds, and milliseconds from the match
int minutes = int.Parse(match.Groups[1].Value);
int seconds = int.Parse(match.Groups[2].Value);
int milliseconds = int.Parse(match.Groups[3].Value); // Convert to milliseconds
TimeSpan timeSpan = new TimeSpan(0,0,minutes, seconds, milliseconds);
// Convert the entire timestamp to milliseconds
// int totalMilliseconds = (minutes * 60 * 1000) + (seconds * 1000) + milliseconds;
double totalSeconds = timeSpan.TotalSeconds;
string lyric = match.Groups[4].Value; // Extract the lyric
// Add the total milliseconds and lyric to the list
timestampedLyrics.Add((totalSeconds, lyric));
}
}
return timestampedLyrics;
}
}
}