Last active
December 4, 2015 06:32
-
-
Save Keboo/e64ec0885f927c3ca80f to your computer and use it in GitHub Desktop.
Serialize Spell Check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "C:\Dev\microsoft\packages\Newtonsoft.Json.7.0.1\lib\portable-net45+wp80+win8+wpa81+dnxcore50\Newtonsoft.Json.dll" | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
public class Spell | |
{ | |
[JsonProperty("original")] | |
public string Original { get; set; } | |
//NB: The response may contain either false (bool) or a string with a suggestion for the original. | |
[JsonProperty("suggestion")] | |
private object InternalSuggestion { get; set; } | |
public string Suggestion | |
{ | |
get | |
{ | |
return InternalSuggestion as string; | |
} | |
} | |
[JsonProperty(PropertyName ="corrections")] | |
private JObject InternalCorrections { get; set; } | |
public IEnumerable<string> Corrections | |
{ | |
get | |
{ | |
if (!IsCorrect) | |
{ | |
return InternalCorrections?[Original].Select(x => x.ToString()) ?? Enumerable.Empty<string>(); | |
} | |
else return Enumerable.Empty<string>(); | |
} | |
} | |
public bool IsCorrect | |
{ | |
get { return InternalSuggestion is bool && !((bool)InternalSuggestion); } | |
} | |
static public Spell CheckAsync(string word) | |
{ | |
// For more info on the spell check RestAPI check out | |
// https://github.com/montanaflynn/Spellcheck-API/. | |
//NB: This is an example response with a mis-spelled word | |
// string exampleJson = | |
//@"{ | |
// ""original"": ""baddd"", | |
// ""suggestion"": ""bad"", | |
// ""corrections"": { | |
// ""baddd"": [ | |
// ""bad"", | |
// ""addd"", | |
// ""bddd"", | |
// ""badd"" | |
// ] | |
// } | |
//}"; | |
//NB: This is an example response with a word spelled correctly | |
string exampleJson = | |
@"{ | |
""original"": ""bad"", | |
""suggestion"": false, | |
}"; | |
string strsb = exampleJson; | |
Spell spell = | |
Newtonsoft.Json.JsonConvert.DeserializeObject<Spell>(strsb); | |
// Assume spelling was only requested on first word. | |
return spell; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment