Guest User

SpellCheck Challenge

a guest
Nov 13th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. /*
  2.  * http://www.reddit.com/r/CS_Questions/comments/maz1l/spellcheck/
  3.  * http://en.wikipedia.org/wiki/Lama_%28genus%29
  4.  *
  5.  *
  6.  */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.IO;
  13.  
  14. namespace Challenges
  15. {
  16.     class Program
  17.     {
  18.         static Dictionary<String, List<String>> words = new Dictionary<String, List<String>>();
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             String word = "lAmaa";
  23.  
  24.             LoadTxt();
  25.  
  26.             //PrintWords();
  27.  
  28.             if (SpellCheck(word))
  29.             {
  30.                 PrintSuggestions(word);
  31.             }
  32.         }
  33.  
  34.         private static void PrintSuggestions(String word)
  35.         {
  36.             foreach (var item in Suggestions(word))
  37.             {
  38.                 Console.WriteLine(item);
  39.             }
  40.         }
  41.  
  42.         private static void PrintWords()
  43.         {
  44.  
  45.             foreach (var pair in words)
  46.             {
  47.                 //if (pair.Value.Count<2)
  48.                 //    continue;
  49.  
  50.                 Console.Write(pair.Key +": ("+ pair.Value.Count +") ");
  51.                 foreach (var value in pair.Value)
  52.                 {
  53.                     Console.Write(value + " ");
  54.                 }
  55.                 Console.WriteLine();
  56.             }
  57.         }
  58.  
  59.         private static void LoadTxt()
  60.         {
  61.             String txt = File.ReadAllText("lama.txt");
  62.  
  63.             txt = txt.
  64.                 Replace("(", " ").
  65.                 Replace(")", " ").
  66.                 Replace("[", " ").
  67.                 Replace("]", " ").
  68.                 Replace(";", " ").
  69.                 Replace(",", " ").
  70.                 Replace(":", " ").
  71.                 Replace(".", " ").
  72.                 Replace("-", " ").
  73.                 Replace("_", " ").
  74.                 Replace("\\", " ").
  75.                 Replace("/", " ").
  76.                 Replace("&", " ").
  77.                 Replace("%", " ").
  78.                 Replace("$", " ").
  79.                 Replace("ยง", " ").
  80.                 Replace("!", " ").
  81.                 Replace("=", " ").
  82.                 Replace("?", " ").
  83.                 Replace("+", " ").
  84.                 Replace("*", " ").
  85.                 Replace("#", " ").
  86.                 Replace("\n", " ").
  87.                 Replace("\r", " ").
  88.                 Replace("\t", " ").
  89.                 Replace("  ", " ");
  90.  
  91.             String[] list = txt.Split(' ');
  92.  
  93.             for (int i = 0; i < list.Length; i++)
  94.             {
  95.                 AddWord(list[i]);
  96.             }
  97.  
  98.         }
  99.  
  100.         private static void AddWord(string word)
  101.         {
  102.             if (string.IsNullOrEmpty(word))
  103.             {
  104.                 return;
  105.             }
  106.  
  107.             String key = ReduceWord(word);
  108.  
  109.             if (words.ContainsKey(key))
  110.             {
  111.                 if (!words[key].Contains(word))
  112.                     words[key].Add(word);
  113.             }
  114.             else
  115.             {
  116.                 words.Add(key, new List<String>() { word });
  117.             }
  118.  
  119.         }
  120.  
  121.         private static bool SpellCheck(String word)
  122.         {
  123.             word = ReduceWord(word);
  124.  
  125.             return words.ContainsKey(word);
  126.         }
  127.  
  128.         private static String[] Suggestions(String word)
  129.         {
  130.             if (String.IsNullOrEmpty(word))
  131.             {
  132.                 return new String[0];
  133.             }
  134.  
  135.             String key = ReduceWord(word);
  136.  
  137.             if (words.ContainsKey(key))
  138.             {
  139.                 return words[key].ToArray();
  140.             }
  141.  
  142.             return new String[0];
  143.         }
  144.  
  145.         private static String ReduceWord(String word)
  146.         {
  147.             // 1. condtion: Capitalization
  148.             word = word.ToLower();
  149.  
  150.             // 2. condtion: Vowels
  151.             word = word.Replace("a", "u").Replace("e", "u").Replace("i", "u").Replace("o", "u");
  152.  
  153.             // 3. condtion: Doubles
  154.             foreach (var ch in "abcdefghijklmnopqrstuvwxyz")
  155.             {
  156.                 word = word.Replace(ch.ToString() + ch.ToString(), ch.ToString());
  157.             }
  158.  
  159.             return word;
  160.         }
  161.     }
  162. }
  163.  
  164.  
Add Comment
Please, Sign In to add comment