Skip to content

Instantly share code, notes, and snippets.

@Keboo
Last active Dec 4, 2015
Embed
What would you like to do?
Serialize Spell Check
#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