Advertisement
sdfxs

Untitled

Mar 28th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 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.  
  40. static Data data = new Data();
  41. static int maxNameLength = data.OriginalInstitutionsList.OrderByDescending(s => s.name.Length).First().name.Length;
  42.  
  43. public List<string> makeSearch(string _query)
  44. {
  45. List<List<string>> completeMatchResults = new List<List<string>>();
  46. List<List<string>> notMatchData = new List<List<string>>();
  47. Utils utils = new Utils();
  48. string query = _query.ToLower();
  49.  
  50. Search.data.OriginalInstitutionsList.ForEach(institut =>
  51. {
  52. if (institut.name.ToLower().Contains(query))
  53. {
  54. completeMatchResults.Add(new List<string>() { institut.name.IndexOf(query).ToString(), institut.id.ToString(), institut.name, institut.link });
  55. }
  56. else
  57. {
  58. notMatchData.Add(new List<string>() { utils.LevenshteinDistance(utils.PadEnd(institut.name, ' ', maxNameLength), query).ToString(), institut.id.ToString() });
  59. }
  60. });
  61.  
  62. List<int> sortedCompleteMatchResults = utils.RemoveSynonyms(completeMatchResults);
  63. List<int> sortedByLevenshtein = utils.RemoveSynonyms(notMatchData);
  64.  
  65. List<int> allResults = sortedCompleteMatchResults.Concat<int>(sortedByLevenshtein).ToList();
  66.  
  67. return utils.ConvertIdsListToNamesList(Search.data.OriginalInstitutionsList, allResults);
  68. }
  69. }
  70.  
  71. class Utils
  72. {
  73. public int LevenshteinDistance(string string1, string string2)
  74. {
  75. if (string1 == null) throw new ArgumentNullException("string1");
  76. if (string2 == null) throw new ArgumentNullException("string2");
  77. int diff;
  78. int[,] m = new int[string1.Length + 1, string2.Length + 1];
  79.  
  80. for (int i = 0; i <= string1.Length; i++) { m[i, 0] = i; }
  81. for (int j = 0; j <= string2.Length; j++) { m[0, j] = j; }
  82.  
  83. for (int i = 1; i <= string1.Length; i++)
  84. {
  85. for (int j = 1; j <= string2.Length; j++)
  86. {
  87. diff = (Char.ToLower(string1[i - 1]) == Char.ToLower(string2[j - 1])) ? 0 : 1;
  88.  
  89. m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1, m[i, j - 1] + 1), m[i - 1, j - 1] + diff);
  90. }
  91. }
  92. return m[string1.Length, string2.Length];
  93. }
  94.  
  95. public string PadEnd(string str, char symbol, int maxLength)
  96. {
  97. int numCharsMissing = maxLength - str.Length;
  98. return String.Concat(str, new String(symbol, numCharsMissing));
  99. }
  100.  
  101. public string FindNameById(List<Institut> array, int id)
  102. {
  103. return array.Find(institut => institut.id == id).name;
  104. }
  105.  
  106. public List<string> ConvertIdsListToNamesList(List<Institut> data, List<int> idsList)
  107. {
  108. return idsList.Select(id => this.FindNameById(data, id)).ToList();
  109. }
  110.  
  111. public List<int> RemoveSynonyms(List<List<string>> results)
  112. {
  113. List<int> idsList = new List<int>();
  114. List<List<int>> idAndValue = new List<List<int>>();
  115. List<List<string>> _results = results.OrderBy(record => Int32.Parse(record[1])).ThenByDescending(record => Int32.Parse(record[0])).ToList();
  116. for (int i = 0; i < _results.Count; i++)
  117. {
  118. int curId = Int32.Parse(_results[i][1]);
  119. int curVal = Int32.Parse(_results[i][0]);
  120. if (i == 0) { idAndValue.Add(new List<int>() { curId, curVal }); }
  121. else
  122. {
  123. int prevId = Int32.Parse(_results[i - 1][1]);
  124. if (prevId != curId)
  125. {
  126. idAndValue.Add(new List<int>() { curId, curVal });
  127. }
  128. }
  129. }
  130. return idAndValue.OrderBy(list => list[1]).Select(list => list[0]).ToList();
  131. }
  132. }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement