Graeme_Grant
Вот быстрое и грязное решение с использованием API-сервиса. К сожалению, сервис не так хорош, как Grammarly...
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
namespace TextGearsApi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Coding challenge: detect poor grammar");
Console.WriteLine("=====================================\n");
// Get you API key here: https://textgears.com
var api = new TextGearsApi("[key_goes_here");
for (;;)
{
Console.Write("Text to check: ");
var text = Console.ReadLine();
if (string.IsNullOrEmpty(text)) break;
try
{
var result = api.Check(text);
if (result?.Errors?.Count != 0)
{
Console.WriteLine("Recommendations:");
for (int i = 0; i < result.Errors.Count; i++)
{
var item = result.Errors[i];
Console.WriteLine($" {i + 1}: {item.Bad} >> {string.Join(", ", item.Better.Select(x => $"\"{x}\""))}");
}
Console.WriteLine();
}
else
{
Console.WriteLine("Looks okay.\n");
}
}
catch (Exception ex)
{
Console.WriteLine($"**Error: {ex.Message}\n");
}
}
}
}
class TextGearsApi
{
public TextGearsApi(string key)
{
Key = key;
}
public string Key { get; private set; }
public CheckResult Check(string text)
{
var request = WebRequest.Create($"https://api.textgears.com/check.php?text={WebUtility.UrlEncode(text)}&key={Key}") as HttpWebRequest;
WebResponse response = null;
try
{
response = request.GetResponse();
if (response != null)
{
var xxx = response.GetResponseStream();
using (var reader = new StreamReader(response.GetResponseStream()))
{
string data = reader.ReadToEnd();
return JsonConvert.DeserializeObject<CheckResult>(data);
}
}
}
catch (Exception)
{
throw;
}
return null;
}
}
public class CheckResult
{
[JsonProperty("result")]
public bool Result { get; set; }
[JsonProperty("errors")]
public IList<Error> Errors { get; set; }
[JsonProperty("score")]
public int Score { get; set; }
}
public class Error
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("offset")]
public int Offset { get; set; }
[JsonProperty("length")]
public int Length { get; set; }
[JsonProperty("bad")]
public string Bad { get; set; }
[JsonProperty("better")]
public IList<string> Better { get; set; }
}
}
Выход:
Coding challenge: detect poor grammar
=====================================
Text to check: The quick brown fox jumped over the lazy dog
Looks okay.
Text to check: Its a rainy day today
Recommendations:
1: Its a >> "It's a"
2: Its >> "It's", "It is"
Text to check: i was walking along beach
Recommendations:
1: i >> "I"
Text to check: Does this sentence have have a double word?
Recommendations:
1: have have >> "have"
2: have >> "had"
Text to check: Am I missing a question mark.
Looks okay.
Text to check: I looking for dog
Looks okay.
Будет продолжать искать лучший сервис или решение...
Обновление №1: Нашел более точный сервис под названием Ginger. Все равно не так хорошо, как Grammarly...
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
namespace GingerIt
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Coding challenge: detect poor grammar");
Console.WriteLine("=====================================\n");
var api = new GingerItApi();
for (;;)
{
Console.Write("Text to check: ");
var text = Console.ReadLine();
if (string.IsNullOrEmpty(text)) break;
try
{
var result = api.Check(text);
if (result?.Corrections?.Count != 0)
{
Console.WriteLine("Recommendations:");
for (int i = 0; i < result.Corrections.Count; i++)
{
var item = result.Corrections[i];
var mistakes = string.Join(", ", item.Mistakes.Select(x => $"\"{text.Substring(x.From, x.To - x.From + 1)}\""));
var suggestions = string.Join(", ", item.Suggestions.Select(x => $"\"{x.Text}\""));
Console.WriteLine($" {i + 1}: {mistakes} >> {suggestions}");
}
Console.WriteLine();
}
else
{
Console.WriteLine("Looks okay.\n");
}
}
catch (Exception ex)
{
Console.WriteLine($"**Error: {ex.Message}\n");
}
}
}
}
class GingerItApi
{
public CheckResult Check(string text)
{
var request = WebRequest.Create($"https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull?callback=jQuery172015406464511272344_1490987331365&apiKey=GingerWebSite&lang=US&clientVersion=2.0&text={text}&_=1490987518060") as HttpWebRequest;
WebResponse response = null;
try
{
response = request.GetResponse();
if (response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
string data = reader.ReadToEnd();
var first = data.IndexOf('{');
var last = data.LastIndexOf('}');
var json = data.Substring(first, last - first + 1);
return JsonConvert.DeserializeObject<CheckResult>(json);
}
}
}
catch (Exception)
{
throw;
}
return null;
}
}
public class LrnFrgOrigIndx
{
[JsonProperty("From")]
public int From { get; set; }
[JsonProperty("To")]
public int To { get; set; }
}
public class Mistake
{
[JsonProperty("Definition")]
public string Definition { get; set; }
[JsonProperty("CanAddToDict")]
public bool CanAddToDict { get; set; }
[JsonProperty("From")]
public int From { get; set; }
[JsonProperty("To")]
public int To { get; set; }
}
public class Suggestion
{
[JsonProperty("Definition")]
public string Definition { get; set; }
[JsonProperty("LrnCatId")]
public int LrnCatId { get; set; }
[JsonProperty("Text")]
public string Text { get; set; }
}
public class Correction
{
[JsonProperty("Confidence")]
public int Confidence { get; set; }
[JsonProperty("From")]
public int From { get; set; }
[JsonProperty("LrnFrg")]
public string LrnFrg { get; set; }
[JsonProperty("LrnFrgOrigIndxs")]
public IList<LrnFrgOrigIndx> LrnFrgOrigIndxs { get; set; }
[JsonProperty("Mistakes")]
public IList<Mistake> Mistakes { get; set; }
[JsonProperty("ShouldReplace")]
public bool ShouldReplace { get; set; }
[JsonProperty("Suggestions")]
public IList<Suggestion> Suggestions { get; set; }
[JsonProperty("To")]
public int To { get; set; }
[JsonProperty("TopLrnCatId")]
public int TopLrnCatId { get; set; }
[JsonProperty("Type")]
public int Type { get; set; }
[JsonProperty("UXFrgFrom")]
public int UXFrgFrom { get; set; }
[JsonProperty("UXFrgTo")]
public int UXFrgTo { get; set; }
}
public class Sentence
{
[JsonProperty("FromIndex")]
public int FromIndex { get; set; }
[JsonProperty("IsEnglish")]
public bool IsEnglish { get; set; }
[JsonProperty("ToIndex")]
public int ToIndex { get; set; }
}
public class CheckResult
{
[JsonProperty("Corrections")]
public IList<Correction> Corrections { get; set; }
[JsonProperty("Sentences")]
public IList<Sentence> Sentences { get; set; }
}
}
Выход:
Coding challenge: detect poor grammar
=====================================
Text to check: The quick brown fox jumped over the lazy dog
Looks okay.
Text to check: Its a rainy day today
Recommendations:
1: "Its" >> "It's"
Text to check: i was walking along beach
Recommendations:
1: "i" >> "I"
2: "beach" >> "the beach", "a beach"
Text to check: Does this sentence