Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 75 additions & 5 deletions SpellChecker.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

/// <summary>
/// The asynchronous method to use inside Main.
/// </summary>
/// <param name="sentence">The sentence to test.</param>
/// <param name="spellChecker">The spell checker to use.</param>
public static async void TestSentence(string sentence, Core.SpellChecker spellChecker)
{
var badWords = new StringBuilder("Misspelled words: ");
Dictionary<string, int> 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.");
}

/// <summary>
/// Get all unique words in the sentence.
/// </summary>
/// <param name="sentence">The sentence to parse.</param>
/// <returns>Collection of all unique words and how frequent they were.</returns>
/// <remarks>
/// 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.
/// </remarks>
public static Dictionary<string, int> 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<string, int>();
foreach (var word in words)
{
if (!wordsDic.ContainsKey(word))
{
wordsDic.Add(word, 1);
}
else
{
wordsDic[word]++;
}
}

return wordsDic;
}

#endregion
}
}
6 changes: 6 additions & 0 deletions SpellChecker.Console/SpellChecker.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<StartupObject />
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SpellChecker.Core\SpellChecker.Core.csproj" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions SpellChecker.Contracts/ISpellChecker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SpellChecker.Contracts
using System.Threading.Tasks;

namespace SpellChecker.Contracts
{

/// <summary>
Expand All @@ -13,7 +15,7 @@ public interface ISpellChecker
/// </summary>
/// <param name="word">The word that needs to be checked</param>
/// <returns><c>true</c>, if the word is spelled correctly, <c>false</c> otherwise</returns>
bool Check(string word);
Task<bool> Check(string word);
}

}
22 changes: 19 additions & 3 deletions SpellChecker.Core/DictionaryDotComSpellChecker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;

using System.IO;
using System.Net;
using System.Threading.Tasks;
using SpellChecker.Contracts;

namespace SpellChecker.Core
Expand All @@ -19,9 +21,23 @@ public class DictionaryDotComSpellChecker :
ISpellChecker
{

public bool Check(string word)
public async Task<bool> 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;
}
}

}
Expand Down
17 changes: 14 additions & 3 deletions SpellChecker.Core/MnemonicSpellCheckerIBeforeE.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using System.Threading.Tasks;
using SpellChecker.Contracts;

namespace SpellChecker.Core
Expand All @@ -23,11 +23,22 @@ public class MnemonicSpellCheckerIBeforeE :
/// </summary>
/// <param name="word">The word to be checked</param>
/// <returns>true when the word is spelled correctly, false otherwise</returns>
public bool Check(string word)
public async Task<bool> 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;
}
}

}
31 changes: 23 additions & 8 deletions SpellChecker.Core/SpellChecker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using System.Threading.Tasks;
using SpellChecker.Contracts;

namespace SpellChecker.Core
Expand All @@ -13,16 +13,15 @@ namespace SpellChecker.Core
public class SpellChecker :
ISpellChecker
{

readonly ISpellChecker[] spellCheckers;
readonly string[] spellCheckers;

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="spellCheckers"></param>
public SpellChecker(ISpellChecker[] spellCheckers)
/// <param name="spellCheckers">The asssembly qualified names of the types of spell checkers.</param>
public SpellChecker(string[] spellCheckers)
{

this.spellCheckers = spellCheckers;
}

/// <summary>
Expand All @@ -31,9 +30,25 @@ public SpellChecker(ISpellChecker[] spellCheckers)
/// </summary>
/// <param name="word">Word to check</param>
/// <returns>True if all spell checkers agree that a word is spelled correctly, false otherwise</returns>
public bool Check(string word)
public async Task<bool> 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;
}

}
Expand Down
14 changes: 12 additions & 2 deletions SpellChecker.Tests/DictionaryDotComSpellCheckerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
16 changes: 13 additions & 3 deletions SpellChecker.Tests/MnemonicSpellCheckerIBeforeETests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SpellChecker.Contracts;
Expand All @@ -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);
}

}
Expand Down