AIResultReader.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Metis.AutoAnalysis
  10. {
  11. public class AIResultReader
  12. {
  13. public string LoadGrainResult(string dir, ref List<string> fileList)
  14. {
  15. string filename = dir + "\\report.ai";
  16. if (File.Exists(filename) == false)
  17. return "";
  18. using (System.IO.StreamReader _file = System.IO.File.OpenText(filename))
  19. {
  20. using (JsonTextReader reader = new JsonTextReader(_file))
  21. {
  22. try
  23. {
  24. JObject o = (JObject)JToken.ReadFrom(reader);
  25. JObject arrList = JObject.Parse(o["file_results"].ToString());
  26. List<string> gradeList = new List<string>();
  27. foreach (var item in arrList)
  28. {
  29. string name = item.Key.ToString();
  30. string grade = item.Value.ToString();
  31. if(gradeList.Contains(grade) == false)
  32. {
  33. gradeList.Add(grade);
  34. }
  35. }
  36. //获取每个级别的个数
  37. List<int> gradeCountList = new List<int>();
  38. for(int i = 0; i < gradeList.Count; i++)
  39. {
  40. gradeCountList.Add(0);
  41. }
  42. foreach (var item in arrList)
  43. {
  44. string grade = item.Value.ToString();
  45. int pos = gradeList.IndexOf(grade);
  46. if (pos >= 0)
  47. {
  48. gradeCountList[pos]++;
  49. }
  50. }
  51. int maxPos = -1;
  52. int maxCount = -10000;
  53. for (int i = 0;i < gradeCountList.Count; i++)
  54. {
  55. int count = gradeCountList[i];
  56. if (count > maxCount)
  57. {
  58. maxCount = count;
  59. maxPos = i;
  60. }
  61. }
  62. string resultGrade = gradeList[maxPos];
  63. foreach (var item in arrList)
  64. {
  65. string name = item.Key.ToString();
  66. string grade = item.Value.ToString();
  67. if (grade == resultGrade)
  68. {
  69. fileList.Add(name);
  70. }
  71. }
  72. return resultGrade;
  73. }
  74. catch (Exception ex)
  75. {
  76. }
  77. }
  78. }
  79. return "";
  80. }
  81. /// <summary>
  82. /// 查找最差级别
  83. /// </summary>
  84. /// <param name="dir"></param>
  85. /// <param name="fileList"></param>
  86. /// <returns></returns>
  87. public string LoadOnlyResult(string dir)
  88. {
  89. string filename = dir + "\\report.ai";
  90. if (File.Exists(filename) == false)
  91. return "";
  92. using (System.IO.StreamReader _file = System.IO.File.OpenText(filename))
  93. {
  94. using (JsonTextReader reader = new JsonTextReader(_file))
  95. {
  96. try
  97. {
  98. JObject o = (JObject)JToken.ReadFrom(reader);
  99. JObject arrList = JObject.Parse(o["file_results"].ToString());
  100. List<double> gradeList = new List<double>();
  101. foreach (var item in arrList)
  102. {
  103. string name = item.Key.ToString();
  104. double grade = (double)item.Value;
  105. if (gradeList.Contains(grade) == false)
  106. {
  107. gradeList.Add(grade);
  108. }
  109. }
  110. gradeList.Sort();
  111. string resultGrade = gradeList.Last().ToString();
  112. return resultGrade;
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine(ex);
  117. }
  118. }
  119. }
  120. return "";
  121. }
  122. }
  123. }