Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using MathNet.Numerics;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.Text.RegularExpressions;
  5. using TEAMModelOS.SDK.DI;
  6. using TEAMModelOS.SDK.Extension;
  7. namespace HTEX.Test
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. Test();
  14. var builder = WebApplication.CreateBuilder(args);
  15. // Add services to the container.
  16. builder.Services.AddControllers();
  17. builder.Services.AddAzureStorage(builder.Configuration.GetValue<string>("Azure:Storage:ConnectionString"));
  18. builder.Services.AddAzureRedis(builder.Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
  19. builder.Services.AddAzureCosmos(builder.Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
  20. builder.Services.AddAzureServiceBus(builder.Configuration.GetValue<string>("Azure:ServiceBus:ConnectionString"));
  21. builder.Services.AddAzureSignalR(builder.Configuration.GetValue<string>("Azure:SignalR:ConnectionString"));
  22. builder.Services.AddSnowflakeId(Convert.ToInt64(builder.Configuration.GetValue<string>("Option:LocationNum")), 1);
  23. builder.Services.AddHttpClient();
  24. var app = builder.Build();
  25. // Configure the HTTP request pipeline.
  26. app.UseHttpsRedirection();
  27. app.UseAuthorization();
  28. app.MapControllers();
  29. app.Run();
  30. }
  31. public static string Test()
  32. {
  33. string originalString = "这 是 一 个 测 试。\n \r\t ";
  34. string stringWithoutSpaces = Regex.Replace(originalString, @"\s+", "");
  35. // 标准答案集合
  36. List<string> standardAnswers = new List<string> { "apple", "banana", "cherry" };
  37. // 学生作答集合
  38. List<string> studentAnswers = new List<string> { "bana1na", "orange", "grape", "111" };
  39. // 使用LINQ查询来判断是否有匹配的答案
  40. bool hasMatchingAnswer = standardAnswers.Intersect(studentAnswers).Any();
  41. // 输出结果
  42. Console.WriteLine("学生答案中是否有符合标准答案的? " + (hasMatchingAnswer ? "是" : "否"));
  43. string sentence1 = "学生答案中是否有符合标准答案的";
  44. string sentence2 = "学生答案中是否有标合标准答案的";
  45. double areSimilar = AreSentencesSimilar(sentence1, sentence2);
  46. Console.WriteLine($"Are the sentences similar? {areSimilar}");
  47. List<string> a = new List<string>() { "a", "d" };
  48. List<string> b = new List<string>() { "b", "a", "c" };
  49. var v = (a.All(item => b.Contains(item)));
  50. string c = "[\r\n 1\r\n]";
  51. List<int> d = c.ToObject<List<int>>();
  52. return "Hello World!";
  53. }
  54. public static void calculate_text_similarity()
  55. {
  56. string pythonExe = @"C:\Path\To\Python\python.exe"; // 修改为你的Python解释器路径
  57. string scriptPath = @"C:\Path\To\Your\Script\calculate_similarity.py"; // 修改为你的脚本路径
  58. string text1 = "今天天气真好";
  59. string text2 = "今天天气不错";
  60. ProcessStartInfo start = new ProcessStartInfo();
  61. start.FileName = pythonExe;
  62. start.Arguments = $"\"{scriptPath}\" \"{text1}\" \"{text2}\"";
  63. start.UseShellExecute = false;
  64. start.RedirectStandardOutput = true;
  65. using (Process process = Process.Start(start))
  66. {
  67. using (StreamReader reader = process.StandardOutput)
  68. {
  69. string result = reader.ReadToEnd();
  70. Console.Write(result);
  71. }
  72. }
  73. }
  74. public static double CalculateSimilarity(string s1, string s2)
  75. {
  76. int n = s1.Length;
  77. int m = s2.Length;
  78. int[,] d = new int[n + 1, m + 1];
  79. for (int i = 0; i <= n; i++)
  80. {
  81. d[i, 0] = i;
  82. }
  83. for (int j = 0; j <= m; j++)
  84. {
  85. d[0, j] = j;
  86. }
  87. for (int i = 1; i <= n; i++)
  88. {
  89. for (int j = 1; j <= m; j++)
  90. {
  91. int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
  92. d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
  93. }
  94. }
  95. return (1.0 - ((double)d[n, m] / Math.Max(s1.Length, s2.Length))) * 100;
  96. }
  97. public static double AreSentencesSimilar(string sentence1, string sentence2)
  98. {
  99. // double threshold = 70; // 设置一个阈值,超过这个百分比则认为是相同的句子
  100. double similarity = CalculateSimilarity(sentence1, sentence2);
  101. return similarity ;
  102. }
  103. }
  104. }