/// <summary> /// Spell check a word /// </summary> /// Check if a word is spelled correctly /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='value'> /// Input string word /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <PostOKResponse> PostAsync(this ISpellCheck operations, string value, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.PostWithHttpMessagesAsync(value, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Spell check a sentence /// </summary> /// Check if a sentence is spelled correctly /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='value'> /// Input sentence word /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <bool?> CheckSentenceStringAsync(this ISpellCheck operations, string value, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CheckSentenceStringWithHttpMessagesAsync(value, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
public Principal() { InitializeComponent(); string execPath = Path.GetDirectoryName(Application.ExecutablePath); speller = new Hunspell(Path.Combine(execPath, @"..\..\Hunspell\bin"), Path.Combine(execPath, @"..\..\resultats\hunspell"), "catalan"); }
//Constructor public UserAnalysis(string userHandle, ITwitAPI twitService, ISpellCheck spellService) { //Forcing user to enter @ to make clear what name is needed, but we don't actually want it if (userHandle[0] == '@') { userHandle = userHandle.Substring(1); } userHandle = userHandle.ToLower(); this.spellService = spellService; this.twitService = twitService; var waitTweets = this.twitService.GetUserTweets(userHandle); waitTweets.Wait(); this.twitterUser = waitTweets.Result; }
/// <summary> /// Loops the given text through spell check <see cref="ISpellCheck.RunSpellCheck"/> interface /// method implementation and returns the user checked text. /// </summary> /// <param name="spellCheck">A class instance which implements the <see cref="ISpellCheck"/> interface.</param> /// <param name="text">The text to be spell-checked by the user.</param> /// <param name="nullOnClose">A value indicating whether to return null if the spell checking was not completed by a user.</param> /// <returns>The text spell-checked by user.</returns> public string RunSpellCheckInterfaceOnText(ISpellCheck spellCheck, string text, bool nullOnClose) { // get the spell checking correction list.. var corrections = RunSpellCheckInterface(spellCheck, text, nullOnClose); // the spell checking was interrupted, so do return null.. if (corrections == null) { return(null); } // correct the text and return the value.. foreach (var correction in corrections) { text = text.Remove(correction.StartLocation, correction.Length); text = text.Insert(correction.StartLocation, correction.CorrectedWord); } return(text); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ITwitAPI twitService, ISpellCheck spellService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); //Get auth keys string keyFile = File.ReadAllText(env.ContentRootPath + "\\APIKeys.json"); JsonDocument keys = JsonDocument.Parse(keyFile); JsonElement root = keys.RootElement; string APIKey = root.GetProperty("APIKey").ToString(); string APISecret = root.GetProperty("APISecret").ToString(); string bearer = root.GetProperty("Bearer").ToString(); twitService.Authenticate(APIKey, APISecret, bearer); spellService.loadWords(Path.Combine(env.ContentRootPath, "English.dic")); }
/// <summary> /// Loops the given text through spell check <see cref="ISpellCheck.RunSpellCheck"/> interface /// method implementation amd returns the user given corrections for the given. /// </summary> /// <param name="spellCheck">A class instance which implements the <see cref="ISpellCheck"/> interface.</param> /// <param name="text">The text to be spell-checked by the user.</param> /// <param name="nullOnClose">A value indicating whether to return null if the spell checking was not completed by a user.</param> /// <returns>A list of corrections made to the text spell-checked by user.</returns> public List <SpellingCorrection> RunSpellCheckInterface(ISpellCheck spellCheck, string text, bool nullOnClose) { ChecksDoneOnPreviousRun = false; var result = new List <SpellingCorrection>(); // create the result instance.. // attach the methods to the interface-implementing class.. spellCheck.AddIgnoreWordAction = AddIgnoreWord; spellCheck.AddUserDictionaryWord = AddUserDictionaryWord; spellCheck.RequestWordSuggestion = SuggestWords; spellCheck.CanAddToUserDictionary = CanAddToUserDictionary; spellCheck.CanAddToUserIgnoreList = CanAddToUserIgnoreList; // run the spell checking for the text.. var missSpelledWords = SpellCheckFast(text); // a value indicating whether the spell checking was created successfully.. var interrupted = false; // ReSharper disable once ForCanBeConvertedToForeach, the collection will change.. for (int i = 0; i < missSpelledWords.Count; i++) { // the misspelled word.. var word = missSpelledWords[i]; // if the user's personal ignore list contains the word, the skip it.. if (IgnoreList.Contains(word.Word)) { continue; } // check if the word is set to be replaced with the same word at all times.. var replaceAll = missSpelledWords.FirstOrDefault(f => f.ReplaceAll && f.Word == word.Word && f.CorrectedWord != null); if (replaceAll != null) { word.ReplaceAll = true; // set the replace all flag to true.. // multiply the replace all to the result for easier implementation.. result.Add(new SpellingCorrection { Word = word.Word, CorrectedWord = replaceAll.CorrectedWord, StartLocation = word.StartLocation, EndLocation = word.StartLocation + word.Length, ReplaceAll = word.ReplaceAll, }); // clone the found instance of this replace all word.. var newWord = replaceAll.Clone(); newWord.StartLocation = word.StartLocation; // change the locations.. newWord.EndLocation = word.EndLocation; newWord.CorrectedWord = replaceAll.CorrectedWord; // set the word correction.. // raise the event in case the the spell checking is done in real-time.. SpellCheckLocationChanged?.Invoke(this, new SpellCheckLocationChangeEventArgs { SpellingError = newWord, AfterSpellCheck = true }); continue; // this case there is no need to go on further.. } // the ignore all case.. if (missSpelledWords.Any(f => f.IgnoreAll && f.Word == word.Word)) { // ..the user has made choice to ignore all instances of this misspelled word.. continue; } // raise the event in case the the spell checking is done in real-time so // that the whatever-GUI can mark the word before the user makes a choice // how to handle the word.. SpellCheckLocationChanged?.Invoke(this, new SpellCheckLocationChangeEventArgs { SpellingError = word, AfterSpellCheck = false }); // call the ISpellCheck implementation instance to choose the action for a // new misspelled word.. ChecksDoneOnPreviousRun = true; if (!spellCheck.RunSpellCheck(word, SuggestWords(word.Word), i + 1, missSpelledWords.Count)) { // set the interrupted flag to true so the result of this // method can be set accordingly and break the lood.. interrupted = true; break; } // if the word is supposed to be ignored or to be ignored at all times // the loop doesn't need to continue any further.. if (word.IgnoreAll || word.Ignore) { continue; } // if for some reason the corrected word would be null // the loop doesn't need to continue any further.. if (word.CorrectedWord == null) { continue; } // raise the event in case the the spell checking is done in real-time so // the corrected word can be applied to the text.. SpellCheckLocationChanged?.Invoke(this, new SpellCheckLocationChangeEventArgs { SpellingError = word, AfterSpellCheck = true }); // add the corrected word to the result.. result.Add(new SpellingCorrection { Word = word.Word, CorrectedWord = word.CorrectedWord, StartLocation = word.StartLocation, EndLocation = word.StartLocation + word.Length, ReplaceAll = word.ReplaceAll, }); } // if the spell checking was interrupted and // a null result was requested on interruption, return null.. if (interrupted && nullOnClose) { return(null); } // order the result backwards.. result = result.OrderByDescending(f => f.StartLocation). ThenByDescending(f => f.Length).ToList(); // return the result.. return(result); }
/// <summary> /// Spell check a sentence /// </summary> /// Check if a sentence is spelled correctly /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='value'> /// Input sentence word /// </param> public static bool?CheckSentenceString(this ISpellCheck operations, string value) { return(Task.Factory.StartNew(s => ((ISpellCheck)s).CheckSentenceStringAsync(value), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// Find spelling corrections /// </summary> /// Find spelling correction suggestions and return result as JSON /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='value'> /// Input string /// </param> public static CorrectJsonOKResponse CorrectJson(this ISpellCheck operations, string value) { return(Task.Factory.StartNew(s => ((ISpellCheck)s).CorrectJsonAsync(value), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
public IActionResult UserAnalysis(string userHandle, [FromServices] ITwitAPI twitService, [FromServices] ISpellCheck spellService) { UserAnalysis u = new UserAnalysis(userHandle, twitService, spellService); if (u.IsValid()) { return(View(u)); } else { return(View("BadEntry")); } }