Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * http://www.reddit.com/r/CS_Questions/comments/maz1l/spellcheck/
- * http://en.wikipedia.org/wiki/Lama_%28genus%29
- *
- *
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Challenges
- {
- class Program
- {
- static Dictionary<String, List<String>> words = new Dictionary<String, List<String>>();
- static void Main(string[] args)
- {
- String word = "lAmaa";
- LoadTxt();
- //PrintWords();
- if (SpellCheck(word))
- {
- PrintSuggestions(word);
- }
- }
- private static void PrintSuggestions(String word)
- {
- foreach (var item in Suggestions(word))
- {
- Console.WriteLine(item);
- }
- }
- private static void PrintWords()
- {
- foreach (var pair in words)
- {
- //if (pair.Value.Count<2)
- // continue;
- Console.Write(pair.Key +": ("+ pair.Value.Count +") ");
- foreach (var value in pair.Value)
- {
- Console.Write(value + " ");
- }
- Console.WriteLine();
- }
- }
- private static void LoadTxt()
- {
- String txt = File.ReadAllText("lama.txt");
- txt = txt.
- Replace("(", " ").
- Replace(")", " ").
- Replace("[", " ").
- Replace("]", " ").
- Replace(";", " ").
- Replace(",", " ").
- Replace(":", " ").
- Replace(".", " ").
- Replace("-", " ").
- Replace("_", " ").
- Replace("\\", " ").
- Replace("/", " ").
- Replace("&", " ").
- Replace("%", " ").
- Replace("$", " ").
- Replace("ยง", " ").
- Replace("!", " ").
- Replace("=", " ").
- Replace("?", " ").
- Replace("+", " ").
- Replace("*", " ").
- Replace("#", " ").
- Replace("\n", " ").
- Replace("\r", " ").
- Replace("\t", " ").
- Replace(" ", " ");
- String[] list = txt.Split(' ');
- for (int i = 0; i < list.Length; i++)
- {
- AddWord(list[i]);
- }
- }
- private static void AddWord(string word)
- {
- if (string.IsNullOrEmpty(word))
- {
- return;
- }
- String key = ReduceWord(word);
- if (words.ContainsKey(key))
- {
- if (!words[key].Contains(word))
- words[key].Add(word);
- }
- else
- {
- words.Add(key, new List<String>() { word });
- }
- }
- private static bool SpellCheck(String word)
- {
- word = ReduceWord(word);
- return words.ContainsKey(word);
- }
- private static String[] Suggestions(String word)
- {
- if (String.IsNullOrEmpty(word))
- {
- return new String[0];
- }
- String key = ReduceWord(word);
- if (words.ContainsKey(key))
- {
- return words[key].ToArray();
- }
- return new String[0];
- }
- private static String ReduceWord(String word)
- {
- // 1. condtion: Capitalization
- word = word.ToLower();
- // 2. condtion: Vowels
- word = word.Replace("a", "u").Replace("e", "u").Replace("i", "u").Replace("o", "u");
- // 3. condtion: Doubles
- foreach (var ch in "abcdefghijklmnopqrstuvwxyz")
- {
- word = word.Replace(ch.ToString() + ch.ToString(), ch.ToString());
- }
- return word;
- }
- }
- }
Add Comment
Please, Sign In to add comment