public ActionResult StringCompare(string agency, Guid studyid, Guid questionid, string questiontext) { DateTime dateTime1 = DateTime.Now; List <LevenshteinItem> items = new List <LevenshteinItem>(); StudyUnitModel item1 = GetAllQuestions(agency, studyid); var item3 = from x in item1.Questions orderby x.DisplayLabel select x; foreach (var question in item3) { if (Math.Abs(questiontext.Length - question.Summary.FirstOrDefault().Value.ToString().Length) <= 4) { var item = new LevenshteinItem() { QuestionId = question.DisplayLabel, QuestionText = question.Summary.FirstOrDefault().Value.ToString(), Results = CompareString.Calculate(questiontext, question.Summary.FirstOrDefault().Value.ToString()) }; items.Add(item); } } DateTime dateTime2 = DateTime.Now; var diff = dateTime2.Subtract(dateTime1); var res = String.Format("{0}:{1}:{2}", diff.Hours, diff.Minutes, diff.Seconds); LevenshteinModel model = new LevenshteinModel(); model.QuestionId = res; model.QuestionText = questiontext; model.Results = items; return(View(model)); }
public int LevenshteinDistance(LevenshteinModel lmode) { string s = lmode.FirstParam; string t = lmode.SecondParam; int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; // Step 1 if (n == 0) { return(m); } if (m == 0) { return(n); } // Step 2 for (int i = 0; i <= n; d[i, 0] = i++) { } for (int j = 0; j <= m; d[0, j] = j++) { } // Step 3 for (int i = 1; i <= n; i++) { //Step 4 for (int j = 1; j <= m; j++) { // Step 5 int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; // Step 6 d[i, j] = Math.Min( Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } // Step 7 return(d[n, m]); }
public ActionResult Levenshtein(string agency, Guid studyid, Guid questionid, string questiontext) { List <LevenshteinItem> items = new List <LevenshteinItem>(); StudyUnitModel item1 = GetAllQuestions(agency, studyid); var item3 = from x in item1.Questions orderby x.DisplayLabel select x; string string1 = "In your household what is the number of bedrooms"; string string2 = "What are the number of bedrooms in your household"; var test2 = LevenshteinDistance.Calculate(string1, string2); var test = new LevenshteinItem() { QuestionId = questionid.ToString(), QuestionText = string2, Results = test2.ToString() }; items.Add(test); foreach (var question in item3) { var item = new LevenshteinItem() { QuestionId = question.DisplayLabel, QuestionText = question.Summary.FirstOrDefault().Value.ToString(), Results = LevenshteinDistance.Calculate(question.Summary.FirstOrDefault().Value.ToString(), questiontext).ToString() }; items.Add(item); } LevenshteinModel model = new LevenshteinModel(); model.QuestionId = questionid.ToString(); model.QuestionText = string1; model.Results = items; return(View(model)); }