Crunchy-Downloader/CRD/Utils/Helpers.cs

178 lines
6.0 KiB
C#
Raw Normal View History

2024-05-04 15:35:32 +00:00
using System;
2024-05-25 22:02:45 +00:00
using System.Collections.Generic;
2024-05-04 15:35:32 +00:00
using System.Diagnostics;
2024-05-25 22:02:45 +00:00
using System.Linq;
2024-05-04 15:35:32 +00:00
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
2024-05-04 15:35:32 +00:00
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace CRD.Utils;
public class Helpers{
/// <summary>
/// Deserializes a JSON string into a specified .NET type.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="json">The JSON string to deserialize.</param>
/// <param name="serializerSettings">The settings for deserialization if null default settings will be used</param>
/// <returns>The deserialized object of type T.</returns>
2024-05-25 22:02:45 +00:00
public static T? Deserialize<T>(string json, JsonSerializerSettings? serializerSettings){
2024-05-04 15:35:32 +00:00
try{
2024-05-25 22:02:45 +00:00
return JsonConvert.DeserializeObject<T>(json, serializerSettings);
2024-05-04 15:35:32 +00:00
} catch (JsonException ex){
Console.WriteLine($"Error deserializing JSON: {ex.Message}");
throw;
}
}
public static Locale ConvertStringToLocale(string? value){
foreach (Locale locale in Enum.GetValues(typeof(Locale))){
var type = typeof(Locale);
var memInfo = type.GetMember(locale.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(EnumMemberAttribute), false);
var description = ((EnumMemberAttribute)attributes[0]).Value;
if (description == value){
return locale;
}
}
if (string.IsNullOrEmpty(value)){
return Locale.DefaulT;
}
return Locale.Unknown; // Return default if not found
2024-05-04 15:35:32 +00:00
}
public static string GenerateSessionId(){
// Get UTC milliseconds
var utcNow = DateTime.UtcNow;
var milliseconds = utcNow.Millisecond.ToString().PadLeft(3, '0');
// Get a high-resolution timestamp
long timestamp = Stopwatch.GetTimestamp();
double timestampToMilliseconds = (double)timestamp / Stopwatch.Frequency * 1000;
string highResTimestamp = timestampToMilliseconds.ToString("F0").PadLeft(13, '0');
return milliseconds + highResTimestamp;
}
public static async Task<(bool IsOk, int ErrorCode)> ExecuteCommandAsync(string type, string bin, string command){
using (var process = new Process()){
process.StartInfo.FileName = bin;
process.StartInfo.Arguments = command;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine($"ERROR: {e.Data}");
}
};
2024-05-04 15:35:32 +00:00
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
2024-05-04 15:35:32 +00:00
await process.WaitForExitAsync();
2024-05-04 15:35:32 +00:00
// Define success condition more appropriately based on the application
bool isSuccess = process.ExitCode == 0;
return (IsOk: isSuccess, ErrorCode: process.ExitCode);
}
}
2024-05-25 22:02:45 +00:00
public static double CalculateCosineSimilarity(string text1, string text2){
var vector1 = ComputeWordFrequency(text1);
var vector2 = ComputeWordFrequency(text2);
return CosineSimilarity(vector1, vector2);
}
private static readonly char[] Delimiters ={ ' ', ',', '.', ';', ':', '-', '_', '\'' };
public static Dictionary<string, double> ComputeWordFrequency(string text){
var wordFrequency = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
var words = SplitText(text);
2024-05-25 22:02:45 +00:00
foreach (var word in words){
if (wordFrequency.TryGetValue(word, out double count)){
wordFrequency[word] = count + 1;
} else{
wordFrequency[word] = 1;
2024-05-25 22:02:45 +00:00
}
}
return wordFrequency;
}
private static List<string> SplitText(string text){
var words = new List<string>();
int start = 0;
for (int i = 0; i < text.Length; i++){
if (Array.IndexOf(Delimiters, text[i]) >= 0){
if (i > start){
words.Add(text.Substring(start, i - start));
}
start = i + 1;
}
}
if (start < text.Length){
words.Add(text.Substring(start));
}
return words;
}
2024-05-25 22:02:45 +00:00
private static double CosineSimilarity(Dictionary<string, double> vector1, Dictionary<string, double> vector2){
var intersection = vector1.Keys.Intersect(vector2.Keys);
double dotProduct = intersection.Sum(term => vector1[term] * vector2[term]);
double normA = Math.Sqrt(vector1.Values.Sum(val => val * val));
double normB = Math.Sqrt(vector2.Values.Sum(val => val * val));
if (normA == 0 || normB == 0){
// If either vector has zero length, return 0 similarity.
return 0;
}
return dotProduct / (normA * normB);
}
public static string? ExtractNumberAfterS(string input){
// Define the regular expression pattern to match |S followed by a number and optionally C followed by another number
string pattern = @"\|S(\d+)(?:C(\d+))?";
Match match = Regex.Match(input, pattern);
if (match.Success){
string sNumber = match.Groups[1].Value;
string cNumber = match.Groups[2].Value;
if (!string.IsNullOrEmpty(cNumber)){
return $"{sNumber}.{cNumber}";
} else{
return sNumber;
}
} else{
return null;
}
}
2024-05-04 15:35:32 +00:00
}