Advertisement
sdfxs

Untitled

Mar 27th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. using Laba.Model;
  2. using Laba.Models;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web.Http;
  8. using System.Web.Http.Filters;
  9.  
  10. namespace Laba.Controllers
  11. {
  12.  
  13. public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
  14. {
  15. public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  16. {
  17. if (actionExecutedContext.Response != null)
  18. actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  19.  
  20. base.OnActionExecuted(actionExecutedContext);
  21. }
  22. }
  23.  
  24. [AllowCrossSiteJson]
  25. public class DataController : ApiController
  26. {
  27. // GET api/Data?query={query}
  28. public string Get(string query)
  29. {
  30. Search search = new Search();
  31. return JsonConvert.SerializeObject(search.makeSearch(query));
  32. }
  33. }
  34.  
  35. class Search
  36. {
  37.  
  38. static Data data = new Data();
  39. static int maxNameLength = data.OriginalInstitutionsList.OrderByDescending(s => s.name.Length).First().name.Length;
  40.  
  41. public List<string> makeSearch(string query)
  42. {
  43. List<List<string>> completeMatchResults = new List<List<string>>();
  44. List<List<string>> notMatchData = new List<List<string>>();
  45. Utils utils = new Utils();
  46.  
  47. Search.data.OriginalInstitutionsList.ForEach(institut =>
  48. {
  49. if (institut.name.Contains(query))
  50. {
  51. completeMatchResults.Add(new List<string>() { institut.name.IndexOf(query).ToString(), institut.id.ToString(), institut.name, institut.link });
  52. }
  53. else
  54. {
  55. notMatchData.Add(new List<string>() { utils.LevenshteinDistance(utils.PadEnd(institut.name, ' ', maxNameLength), query).ToString(), institut.id.ToString() });
  56. }
  57. });
  58.  
  59. List<int> sortedCompleteMatchResults = utils.RemoveSynonyms(completeMatchResults);
  60. List<int> sortedByLevenshtein = utils.RemoveSynonyms(notMatchData);
  61.  
  62. List<int> allResults = sortedCompleteMatchResults.Concat<int>(sortedByLevenshtein).ToList();
  63.  
  64. return utils.ConvertIdsListToNamesList(Search.data.OriginalInstitutionsList, allResults);
  65. }
  66. }
  67.  
  68. class Utils
  69. {
  70. public int LevenshteinDistance(string string1, string string2)
  71. {
  72. if (string1 == null) throw new ArgumentNullException("string1");
  73. if (string2 == null) throw new ArgumentNullException("string2");
  74. int diff;
  75. int[,] m = new int[string1.Length + 1, string2.Length + 1];
  76.  
  77. for (int i = 0; i <= string1.Length; i++) { m[i, 0] = i; }
  78. for (int j = 0; j <= string2.Length; j++) { m[0, j] = j; }
  79.  
  80. for (int i = 1; i <= string1.Length; i++)
  81. {
  82. for (int j = 1; j <= string2.Length; j++)
  83. {
  84. diff = (Char.ToLower(string1[i - 1]) == Char.ToLower(string2[j - 1])) ? 0 : 1;
  85.  
  86. m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1, m[i, j - 1] + 1), m[i - 1, j - 1] + diff);
  87. }
  88. }
  89. return m[string1.Length, string2.Length];
  90. }
  91.  
  92. public string PadEnd(string str, char symbol, int maxLength)
  93. {
  94. int numCharsMissing = maxLength - str.Length;
  95. return String.Concat(str, new String(symbol, numCharsMissing));
  96. }
  97.  
  98. public string FindNameById(List<Institut> array, int id)
  99. {
  100. try
  101. {
  102. return array.Find(institut => institut.id == id).name;
  103. }
  104. catch
  105. {
  106. return "";
  107. }
  108. }
  109.  
  110. public List<string> ConvertIdsListToNamesList(List<Institut> data, List<int> idsList)
  111. {
  112. return idsList.Select(id => this.FindNameById(data, id)).ToList();
  113. }
  114.  
  115. public List<int> RemoveSynonyms(List<List<string>> results)
  116. {
  117. List<int> idsList = new List<int>();
  118. List<List<int>> idAndValue = new List<List<int>>();
  119. List<List<string>> _results = results.OrderBy(record => Int32.Parse(record[1])).ThenBy(record => Int32.Parse(record[0])).ToList();
  120. for (int i = 0; i < _results.Count; i++)
  121. {
  122. int curId = Int32.Parse(_results[i][1]);
  123. int curVal = Int32.Parse(_results[i][0]);
  124. if (i == 0) { idAndValue.Add(new List<int>() { curId, curVal }); }
  125. else
  126. {
  127. int prevId = Int32.Parse(_results[i - 1][1]);
  128. if (prevId != curId)
  129. {
  130. idAndValue.Add(new List<int>() { curId, curVal });
  131. }
  132. }
  133. }
  134. idAndValue.ForEach(list => System.Diagnostics.Debug.WriteLine($"asdf{0} gs {1}", list[0], list[1]));
  135.  
  136. idsList = idAndValue.OrderBy(list => list[1]).Select(list => list[1]).ToList();
  137. idsList.ForEach(id => System.Diagnostics.Debug.WriteLine($"id {0}", id));
  138. return idsList;
  139. }
  140. }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement