Advertisement
sdfxs

Untitled

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