diff --git a/SpellChecker.Console/Program.cs b/SpellChecker.Console/Program.cs
index f076301..4450130 100644
--- a/SpellChecker.Console/Program.cs
+++ b/SpellChecker.Console/Program.cs
@@ -1,4 +1,8 @@
-using SpellChecker.Contracts;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SpellChecker.Contracts;
using SpellChecker.Core;
namespace SpellChecker.Console
@@ -47,13 +51,79 @@ public static void Main(string[] args)
// capturing distinct words that are misspelled
// use this spellChecker to evaluate the words
- var spellChecker = new Core.SpellChecker(new ISpellChecker[]
+ var spellChecker = new Core.SpellChecker(new string[]
{
- new MnemonicSpellCheckerIBeforeE(),
- new DictionaryDotComSpellChecker(),
+ typeof(MnemonicSpellCheckerIBeforeE).AssemblyQualifiedName,
+ typeof(DictionaryDotComSpellChecker).AssemblyQualifiedName,
});
+
+
+ Task.Run(() => TestSentence(sentence, spellChecker));
+ System.Console.WriteLine("\nSpellcheckers are running...");
+
+ System.Console.ReadKey();
}
- }
+ #region Added Methods
+
+ ///
+ /// The asynchronous method to use inside Main.
+ ///
+ /// The sentence to test.
+ /// The spell checker to use.
+ public static async void TestSentence(string sentence, Core.SpellChecker spellChecker)
+ {
+ var badWords = new StringBuilder("Misspelled words: ");
+ Dictionary uniqueWords = GetUniqueWords(sentence);
+
+ foreach (var word in uniqueWords.Keys)
+ {
+ bool isSpelledCorrectly = await spellChecker.Check(word);
+ if (!isSpelledCorrectly)
+ {
+ badWords.Append($" {word},");
+ }
+ }
+
+ // Remove the last comma if present.
+ if (badWords[badWords.Length - 1] == ',')
+ {
+ badWords.Remove(badWords.Length - 1, 1);
+ }
+ System.Console.WriteLine(badWords);
+ System.Console.WriteLine("\n\nPress any key to quit.");
+ }
+
+ ///
+ /// Get all unique words in the sentence.
+ ///
+ /// The sentence to parse.
+ /// Collection of all unique words and how frequent they were.
+ ///
+ /// I decided to go ahead and track which of the words are unique. It's not truly necessary,
+ /// but could be useful for statistics. If memory is really an issue, use a List instead.
+ ///
+ public static Dictionary GetUniqueWords(string sentence)
+ {
+ var runOnSentence = new string(sentence.Where(c => !char.IsPunctuation(c) && !char.IsSymbol(c)).ToArray());
+ string[] words = runOnSentence.Split(' ');
+ var wordsDic = new Dictionary();
+ foreach (var word in words)
+ {
+ if (!wordsDic.ContainsKey(word))
+ {
+ wordsDic.Add(word, 1);
+ }
+ else
+ {
+ wordsDic[word]++;
+ }
+ }
+
+ return wordsDic;
+ }
+
+ #endregion
+ }
}
diff --git a/SpellChecker.Console/SpellChecker.Console.csproj b/SpellChecker.Console/SpellChecker.Console.csproj
index ec9a449..22caf52 100644
--- a/SpellChecker.Console/SpellChecker.Console.csproj
+++ b/SpellChecker.Console/SpellChecker.Console.csproj
@@ -8,6 +8,12 @@
+
+ DEBUG;TRACE
+ full
+ true
+
+
diff --git a/SpellChecker.Contracts/ISpellChecker.cs b/SpellChecker.Contracts/ISpellChecker.cs
index f70fb0a..4d440ec 100644
--- a/SpellChecker.Contracts/ISpellChecker.cs
+++ b/SpellChecker.Contracts/ISpellChecker.cs
@@ -1,4 +1,6 @@
-namespace SpellChecker.Contracts
+using System.Threading.Tasks;
+
+namespace SpellChecker.Contracts
{
///
@@ -13,7 +15,7 @@ public interface ISpellChecker
///
/// The word that needs to be checked
/// true, if the word is spelled correctly, false otherwise
- bool Check(string word);
+ Task Check(string word);
}
}
diff --git a/SpellChecker.Core/DictionaryDotComSpellChecker.cs b/SpellChecker.Core/DictionaryDotComSpellChecker.cs
index a9dff56..4788e77 100644
--- a/SpellChecker.Core/DictionaryDotComSpellChecker.cs
+++ b/SpellChecker.Core/DictionaryDotComSpellChecker.cs
@@ -1,5 +1,7 @@
using System;
-
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
using SpellChecker.Contracts;
namespace SpellChecker.Core
@@ -19,9 +21,23 @@ public class DictionaryDotComSpellChecker :
ISpellChecker
{
- public bool Check(string word)
+ public async Task Check(string word)
{
- throw new NotImplementedException();
+ string url = "http://dictionary.reference.com/browse/" + word;
+
+ try
+ {
+ var request = HttpWebRequest.Create(url);
+ var webResponse = await request.GetResponseAsync() as HttpWebResponse;
+ bool result = webResponse.StatusCode == HttpStatusCode.OK;
+ webResponse.Close();
+
+ return result;
+ }
+ catch (WebException)
+ {
+ return false;
+ }
}
}
diff --git a/SpellChecker.Core/MnemonicSpellCheckerIBeforeE.cs b/SpellChecker.Core/MnemonicSpellCheckerIBeforeE.cs
index 7bc87c0..f38f0f6 100644
--- a/SpellChecker.Core/MnemonicSpellCheckerIBeforeE.cs
+++ b/SpellChecker.Core/MnemonicSpellCheckerIBeforeE.cs
@@ -1,5 +1,5 @@
using System;
-
+using System.Threading.Tasks;
using SpellChecker.Contracts;
namespace SpellChecker.Core
@@ -23,11 +23,22 @@ public class MnemonicSpellCheckerIBeforeE :
///
/// The word to be checked
/// true when the word is spelled correctly, false otherwise
- public bool Check(string word)
+ public async Task Check(string word)
{
- throw new NotImplementedException();
+ return await Task.FromResult(CheckSynchronous(word));
}
+ public bool CheckSynchronous(string word)
+ {
+ string invWord = word.ToLowerInvariant();
+ if (invWord.Contains("cie") && invWord != "science")
+ {
+ // I couldn't really let "science" go by...
+ return false;
+ }
+
+ return true;
+ }
}
}
diff --git a/SpellChecker.Core/SpellChecker.cs b/SpellChecker.Core/SpellChecker.cs
index be6aa01..1e5f4c9 100644
--- a/SpellChecker.Core/SpellChecker.cs
+++ b/SpellChecker.Core/SpellChecker.cs
@@ -1,5 +1,5 @@
using System;
-
+using System.Threading.Tasks;
using SpellChecker.Contracts;
namespace SpellChecker.Core
@@ -13,16 +13,15 @@ namespace SpellChecker.Core
public class SpellChecker :
ISpellChecker
{
-
- readonly ISpellChecker[] spellCheckers;
+ readonly string[] spellCheckers;
///
/// Initializes a new instance.
///
- ///
- public SpellChecker(ISpellChecker[] spellCheckers)
+ /// The asssembly qualified names of the types of spell checkers.
+ public SpellChecker(string[] spellCheckers)
{
-
+ this.spellCheckers = spellCheckers;
}
///
@@ -31,9 +30,25 @@ public SpellChecker(ISpellChecker[] spellCheckers)
///
/// Word to check
/// True if all spell checkers agree that a word is spelled correctly, false otherwise
- public bool Check(string word)
+ public async Task Check(string word)
{
- throw new NotImplementedException();
+ foreach (var spellCheckerType in this.spellCheckers)
+ {
+ var checker = Activator.CreateInstance(Type.GetType(spellCheckerType)) as ISpellChecker;
+ if (checker == null)
+ {
+ Console.WriteLine($"The {spellCheckerType} checker was not instantiated properly");
+ continue;
+ }
+
+ bool isCorrect = await checker.Check(word);
+ if (!isCorrect)
+ {
+ return false;
+ }
+ }
+
+ return true;
}
}
diff --git a/SpellChecker.Tests/DictionaryDotComSpellCheckerTests.cs b/SpellChecker.Tests/DictionaryDotComSpellCheckerTests.cs
index eedd108..31c581c 100644
--- a/SpellChecker.Tests/DictionaryDotComSpellCheckerTests.cs
+++ b/SpellChecker.Tests/DictionaryDotComSpellCheckerTests.cs
@@ -23,13 +23,23 @@ public void TestFixureSetUp()
[TestMethod]
public void Check_That_FileAndServe_Is_Misspelled()
{
- throw new NotImplementedException();
+ string word = "FileAndServe";
+ bool result;
+
+ result = spellChecker.Check(word).Result;
+
+ Assert.AreEqual(false, result);
}
[TestMethod]
public void Check_That_South_Is_Not_Misspelled()
{
- throw new NotImplementedException();
+ string word = "South";
+ bool result;
+
+ result = spellChecker.Check(word).Result;
+
+ Assert.AreEqual(true, result);
}
}
diff --git a/SpellChecker.Tests/MnemonicSpellCheckerIBeforeETests.cs b/SpellChecker.Tests/MnemonicSpellCheckerIBeforeETests.cs
index 59ede10..9a5454e 100644
--- a/SpellChecker.Tests/MnemonicSpellCheckerIBeforeETests.cs
+++ b/SpellChecker.Tests/MnemonicSpellCheckerIBeforeETests.cs
@@ -1,5 +1,5 @@
using System;
-
+using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SpellChecker.Contracts;
@@ -23,13 +23,23 @@ public void TestFixtureSetUp()
[TestMethod]
public void Check_Word_That_Contains_I_Before_E_Is_Spelled_Correctly()
{
- throw new NotImplementedException();
+ string word = "fierce"; // correctly spelled
+ bool result;
+
+ result = spellChecker.Check(word).Result;
+
+ Assert.AreEqual(true, result);
}
[TestMethod]
public void Check_Word_That_Contains_I_Before_E_Is_Spelled_Incorrectly()
{
- throw new NotImplementedException();
+ string word = "decieve"; // incorrectly spelled
+ bool result;
+
+ result = spellChecker.Check(word).Result;
+
+ Assert.AreEqual(false, result);
}
}