Guest User

Untitled

a guest
Jul 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. static class Program
  8. {
  9. static void Main()
  10. {
  11. Func<string, IEnumerable<string>> words = (text) =>
  12. from match in (Regex.Matches(text.ToLowerInvariant(), "[a-z]+").Cast<Match>())
  13. select match.Value;
  14.  
  15. Func<IEnumerable<string>, IDictionary<string, int>> train = (features) =>
  16. (from f in features
  17. group f by f into g
  18. select g)
  19. .ToDictionary(g => g.Key, g => g.Count());
  20.  
  21. var NWORDS = train(words(File.ReadAllText("big.txt")));
  22.  
  23. var alphabet = "abcdefghijklmnopqrstuvwxyz";
  24.  
  25. Func<string, IEnumerable<string>> edits1 = (word) =>
  26. {
  27. var s = from item in Enumerable.Range(0, word.Length+1)
  28. select Tuple.Create(word.Substring(0, item), word.Substring(item));
  29.  
  30. var deletes = from item in s let a=item.Item1 let b=item.Item2
  31. where b.Length > 0
  32. select a + b.Substring(1);
  33. var transposes = from item in s let a=item.Item1 let b=item.Item2
  34. where b.Length > 1
  35. select a + b[1] + b[0] + b.Substring(2);
  36. var replaces = from c in alphabet
  37. from item in s let a=item.Item1 let b=item.Item2
  38. where b.Length > 0
  39. select a + c + b.Substring(1);
  40. var inserts = from c in alphabet
  41. from item in s let a=item.Item1 let b = item.Item2
  42. select a + c + b;
  43. return deletes.Union(transposes).Union(replaces).Union(inserts).Distinct();
  44. };
  45.  
  46. Func<string, IEnumerable<string>> known_edits2 = (word) =>
  47. (from e1 in edits1(word)
  48. from e2 in edits1(e1)
  49. where NWORDS.ContainsKey(e2)
  50. select e2).Distinct();
  51.  
  52. Func<IEnumerable<string>, IEnumerable<string>> known = (words2) => (from w in words2 where NWORDS.ContainsKey(w) select w).Distinct();
  53.  
  54. Func<string, string> correct = (word) =>
  55. {
  56. var candidates = known(new[] { word }).NullIfEmpty() ?? known(edits1(word)).NullIfEmpty() ?? known_edits2(word).NullIfEmpty() ?? Enumerable.Repeat(word, 1);
  57.  
  58. return (from candidate in candidates
  59. orderby (int?)NWORDS[candidate] ?? 0 descending
  60. select candidate).First();
  61. };
  62.  
  63. Console.WriteLine("speling => " + correct("speling"));
  64. Console.WriteLine("korrecter => " + correct("korrecter"));
  65. Console.WriteLine("acess => " + correct("acess"));
  66. Console.WriteLine("acount => " + correct("acount"));
  67. Console.WriteLine("forbiden => " + correct("forbiden"));
  68.  
  69. Console.ReadLine();
  70. }
  71.  
  72. public static IEnumerable<T> NullIfEmpty<T>(this IEnumerable<T> source)
  73. {
  74. return source.Any() ? source : null;
  75. }
  76. }
Add Comment
Please, Sign In to add comment