Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- static class Program
- {
- static void Main()
- {
- Func<string, IEnumerable<string>> words = (text) =>
- from match in (Regex.Matches(text.ToLowerInvariant(), "[a-z]+").Cast<Match>())
- select match.Value;
- Func<IEnumerable<string>, IDictionary<string, int>> train = (features) =>
- (from f in features
- group f by f into g
- select g)
- .ToDictionary(g => g.Key, g => g.Count());
- var NWORDS = train(words(File.ReadAllText("big.txt")));
- var alphabet = "abcdefghijklmnopqrstuvwxyz";
- Func<string, IEnumerable<string>> edits1 = (word) =>
- {
- var s = from item in Enumerable.Range(0, word.Length+1)
- select Tuple.Create(word.Substring(0, item), word.Substring(item));
- var deletes = from item in s let a=item.Item1 let b=item.Item2
- where b.Length > 0
- select a + b.Substring(1);
- var transposes = from item in s let a=item.Item1 let b=item.Item2
- where b.Length > 1
- select a + b[1] + b[0] + b.Substring(2);
- var replaces = from c in alphabet
- from item in s let a=item.Item1 let b=item.Item2
- where b.Length > 0
- select a + c + b.Substring(1);
- var inserts = from c in alphabet
- from item in s let a=item.Item1 let b = item.Item2
- select a + c + b;
- return deletes.Union(transposes).Union(replaces).Union(inserts).Distinct();
- };
- Func<string, IEnumerable<string>> known_edits2 = (word) =>
- (from e1 in edits1(word)
- from e2 in edits1(e1)
- where NWORDS.ContainsKey(e2)
- select e2).Distinct();
- Func<IEnumerable<string>, IEnumerable<string>> known = (words2) => (from w in words2 where NWORDS.ContainsKey(w) select w).Distinct();
- Func<string, string> correct = (word) =>
- {
- var candidates = known(new[] { word }).NullIfEmpty() ?? known(edits1(word)).NullIfEmpty() ?? known_edits2(word).NullIfEmpty() ?? Enumerable.Repeat(word, 1);
- return (from candidate in candidates
- orderby (int?)NWORDS[candidate] ?? 0 descending
- select candidate).First();
- };
- Console.WriteLine("speling => " + correct("speling"));
- Console.WriteLine("korrecter => " + correct("korrecter"));
- Console.WriteLine("acess => " + correct("acess"));
- Console.WriteLine("acount => " + correct("acount"));
- Console.WriteLine("forbiden => " + correct("forbiden"));
- Console.ReadLine();
- }
- public static IEnumerable<T> NullIfEmpty<T>(this IEnumerable<T> source)
- {
- return source.Any() ? source : null;
- }
- }
Add Comment
Please, Sign In to add comment