/* * Short C# Implementation of Peter Norvig's Spelling Corrector. * VERSION 1.5, last updated 28 oct 09. * * Peter Norvig's original article located at * http://norvig.com/spell-correct.html * * Copyright (c) 2008-2009 Lorenzo Stoakes * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System.Collections.Generic; using System.Linq; static class SpellCorrect { static void Main(string[] args) { System.Func> edits1 = w => (from i in Enumerable.Range(0, w.Length) select w.Substring(0, i) + w.Substring(i + 1)) // Deletion .Union(from i in Enumerable.Range(0, w.Length - 1) select w.Substring(0, i) + w.Substring(i + 1, 1) + w.Substring(i, 1) + w.Substring(i + 2)) // Transposition .Union(from i in Enumerable.Range(0, w.Length) from c in "abcdefghijklmnopqrstuvwxyz" select w.Substring(0, i) + c.ToString() + w.Substring(i + 1)) // Alteration .Union(from i in Enumerable.Range(0, w.Length + 1) from c in "abcdefghijklmnopqrstuvwxyz" select w.Substring(0, i) + c.ToString() + w.Substring(i)); // Insertion var features = from System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(System.IO.File.ReadAllText("big.txt").ToLower(), "[a-z]+", System.Text.RegularExpressions.RegexOptions.Compiled) select m.Value; var nWords = (from f in features group f by f into g select g).ToDictionary(g => g.Key, g => g.Count()); IEnumerable candidates = from w in edits1(args[0]) where nWords.ContainsKey(w) select w; if (candidates.Count() == 0) candidates = (from e1 in edits1(args[0]) from e2 in edits1(e1) where nWords.ContainsKey(e2) select e2).Distinct(); System.Console.WriteLine(nWords.ContainsKey(args[0]) ? args[0] : (from c in candidates orderby (nWords.ContainsKey(c) ? nWords[c] : 1) descending select c).First()); } }