Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Laba.Models;
- using Newtonsoft.Json;
- using System;
- using System.Web.Http.Filters;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using Laba.Model;
- namespace Laba.Controllers
- {
- public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
- {
- if (actionExecutedContext.Response != null)
- actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
- base.OnActionExecuted(actionExecutedContext);
- }
- }
- [AllowCrossSiteJson]
- public class DataController : ApiController
- {
- // GET api/Data?query={query}
- public string Get(string query)
- {
- Search search = new Search();
- return JsonConvert.SerializeObject(search.makeSearch(query));
- }
- }
- class Search
- {
- static Data data = new Data();
- static int maxNameLength = data.OriginalInstitutionsList.OrderByDescending(s => s.name.Length).First().name.Length;
- public List<string> makeSearch(string _query)
- {
- List<List<string>> completeMatchResults = new List<List<string>>();
- List<List<string>> notMatchData = new List<List<string>>();
- Utils utils = new Utils();
- string query = _query.ToLower();
- Search.data.OriginalInstitutionsList.ForEach(institut =>
- {
- if (institut.name.ToLower().Contains(query))
- {
- completeMatchResults.Add(new List<string>() { institut.name.IndexOf(query).ToString(), institut.id.ToString(), institut.name, institut.link });
- }
- else
- {
- notMatchData.Add(new List<string>() { utils.LevenshteinDistance(utils.PadEnd(institut.name, ' ', maxNameLength), query).ToString(), institut.id.ToString() });
- }
- });
- List<int> sortedCompleteMatchResults = utils.RemoveSynonyms(completeMatchResults);
- List<int> sortedByLevenshtein = utils.RemoveSynonyms(notMatchData);
- List<int> allResults = sortedCompleteMatchResults.Concat<int>(sortedByLevenshtein).ToList();
- return utils.ConvertIdsListToNamesList(Search.data.OriginalInstitutionsList, allResults);
- }
- }
- class Utils
- {
- public int LevenshteinDistance(string string1, string string2)
- {
- if (string1 == null) throw new ArgumentNullException("string1");
- if (string2 == null) throw new ArgumentNullException("string2");
- int diff;
- int[,] m = new int[string1.Length + 1, string2.Length + 1];
- for (int i = 0; i <= string1.Length; i++) { m[i, 0] = i; }
- for (int j = 0; j <= string2.Length; j++) { m[0, j] = j; }
- for (int i = 1; i <= string1.Length; i++)
- {
- for (int j = 1; j <= string2.Length; j++)
- {
- diff = (Char.ToLower(string1[i - 1]) == Char.ToLower(string2[j - 1])) ? 0 : 1;
- m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1, m[i, j - 1] + 1), m[i - 1, j - 1] + diff);
- }
- }
- return m[string1.Length, string2.Length];
- }
- public string PadEnd(string str, char symbol, int maxLength)
- {
- int numCharsMissing = maxLength - str.Length;
- return String.Concat(str, new String(symbol, numCharsMissing));
- }
- public string FindNameById(List<Institut> array, int id)
- {
- return array.Find(institut => institut.id == id).name;
- }
- public List<string> ConvertIdsListToNamesList(List<Institut> data, List<int> idsList)
- {
- return idsList.Select(id => this.FindNameById(data, id)).ToList();
- }
- public List<int> RemoveSynonyms(List<List<string>> results)
- {
- List<int> idsList = new List<int>();
- List<List<int>> idAndValue = new List<List<int>>();
- List<List<string>> _results = results.OrderBy(record => Int32.Parse(record[1])).ThenByDescending(record => Int32.Parse(record[0])).ToList();
- for (int i = 0; i < _results.Count; i++)
- {
- int curId = Int32.Parse(_results[i][1]);
- int curVal = Int32.Parse(_results[i][0]);
- if (i == 0) { idAndValue.Add(new List<int>() { curId, curVal }); }
- else
- {
- int prevId = Int32.Parse(_results[i - 1][1]);
- if (prevId != curId)
- {
- idAndValue.Add(new List<int>() { curId, curVal });
- }
- }
- }
- return idAndValue.OrderBy(list => list[1]).Select(list => list[0]).ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement