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.InstitutionsList.OrderByDescending(s => s.name.Length).First().name.Length;
- public List<string> makeSearch(string _query)
- {
- List<PreResult> completeMatchResults = new List<PreResult>();
- List<PreResult> notMatchData = new List<PreResult>();
- Utils utils = new Utils();
- string query = _query.ToLower();
- Search.data.InstitutionsList.ForEach(institut =>
- {
- if (institut.name.ToLower().Contains(query))
- {
- PreResult result = new PreResult();
- result.Priority = institut.name.IndexOf(query);
- result.Id = institut.id;
- completeMatchResults.Add(result);
- }
- else
- {
- PreResult result = new PreResult();
- result.Priority = utils.LevenshteinDistance(utils.PadEnd(institut.name, ' ', maxNameLength), query);
- result.Id = institut.id;
- notMatchData.Add(result);
- }
- });
- 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<PreResult> results)
- {
- List<int> idsList = new List<int>();
- List<PreResult> idAndValue = new List<PreResult>();
- List<PreResult> _results = results.OrderBy(record => record.Id).ThenByDescending(record => record.Priority).ToList();
- for (int i = 0; i < _results.Count; i++)
- {
- int curId = _results[i].Id;
- int curVal = _results[i].Priority;
- PreResult result = new PreResult();
- result.Id = curId;
- result.Priority = curVal;
- if (i == 0) {
- idAndValue.Add(result);
- }
- else
- {
- int prevId = _results[i - 1].Id;
- if (prevId != curId)
- {
- idAndValue.Add(result);
- }
- }
- }
- return idAndValue.OrderBy(list => list.Priority).Select(list => list.Id).ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement