/// <summary> /// Deletes the CurrentWord from the Text Property /// </summary> /// <remarks> /// Note, calling ReplaceWord with the ReplacementWord property set to /// an empty string has the same behavior as DeleteWord. /// </remarks> public void DeleteWord() { if (_words is null || _words.Count == 0) { TraceWriter.TraceWarning("No Words to Delete"); return; } int replacedIndex = WordIndex; int index = _words[replacedIndex].Index; int length = _words[replacedIndex].Length; // adjust length to remove extra white space after first word if (index == 0 && index + length < _text.Length && _text[index + length] == ' ') { length++; // removing trailing space } // adjust length to remove double white space else if (index > 0 && index + length < _text.Length && _text[index - 1] == ' ' && _text[index + length] == ' ') { length++; // removing trailing space } // adjust index to remove extra white space before punctuation else if (index > 0 && index + length < _text.Length && _text[index - 1] == ' ' && char.IsPunctuation(_text[index + length])) { index--; length++; } // adjust index to remove extra white space before last word else if (index > 0 && index + length == _text.Length && _text[index - 1] == ' ') { index--; length++; } string deletedWord = _text.ToString(index, length); _text.Remove(index, length); CalculateWords(); OnDeletedWord(new SpellingEventArgs(deletedWord, replacedIndex, index)); }
/// <summary> /// Ignores all instances of the CurrentWord in the Text Property /// </summary> public void IgnoreAllWord() { if (this.CurrentWord.Length == 0) { TraceWriter.TraceWarning("No current word"); return; } // Add current word to ignore list _ignoreList.Add(this.CurrentWord); this.IgnoreWord(); }
/// <summary> /// Replaces all instances of the CurrentWord in the Text Property /// </summary> public void ReplaceAllWord() { if (this.CurrentWord.Length == 0) { TraceWriter.TraceWarning("No current word"); return; } // if not in list and replacement word has length if (!_replaceList.ContainsKey(this.CurrentWord) && _replacementWord.Length > 0) { _replaceList.Add(this.CurrentWord, _replacementWord); } this.ReplaceWord(); }
/// <summary> /// Ignores the instances of the CurrentWord in the Text Property /// </summary> /// <remarks> /// Must call SpellCheck after call this method to resume /// spell checking /// </remarks> public void IgnoreWord() { if (_words == null || _words.Count == 0 || this.CurrentWord.Length == 0) { TraceWriter.TraceWarning("No text or current word"); return; } this.OnIgnoredWord(new SpellingEventArgs( this.CurrentWord, this.WordIndex, _words[this.WordIndex].Index)); // increment Word Index to skip over this word _wordIndex++; }
/// <summary> /// Ignores the instances of the CurrentWord in the Text Property /// </summary> /// <remarks> /// Must call SpellCheck after call this method to resume /// spell checking /// </remarks> public void IgnoreWord() { if (_words == null || _words.Count == 0 || CurrentWord.Length == 0) { TraceWriter.TraceWarning("No text or current word"); return; } OnIgnoredWord(new SpellingEventArgs( CurrentWord, WordIndex, GetWordIndex())); // increment Word Index to skip over this word WordIndex = _wordIndex + 1; }
/// <summary> /// Gets the word index from the text index. Use this method to /// find a word based on the text position. /// </summary> /// <param name="textIndex"> /// <para> /// The index to search for /// </para> /// </param> /// <returns> /// The word index that the text index falls on /// </returns> public int GetWordIndexFromTextIndex(int textIndex) { if (_words == null || _words.Count == 0 || textIndex < 1) { TraceWriter.TraceWarning("No words to get text index from."); return(0); } if (_words.Count == 1) { return(0); } int low = 0; int high = _words.Count - 1; // binary search while (low <= high) { int mid = (low + high) / 2; int wordStartIndex = _words[mid].Index; int wordEndIndex = _words[mid].Index + _words[mid].Length - 1; // add white space to end of word by finding the start of the next word if ((mid + 1) < _words.Count) { wordEndIndex = _words[mid + 1].Index - 1; } if (textIndex < wordStartIndex) { high = mid - 1; } else if (textIndex > wordEndIndex) { low = mid + 1; } else if (wordStartIndex <= textIndex && textIndex <= wordEndIndex) { return(mid); } } // return last word if not found return(_words.Count - 1); }
/// <summary> /// Replaces the instances of the CurrentWord in the Text Property /// </summary> public void ReplaceWord() { if (_words is null || _words.Count == 0 || CurrentWord.Length == 0) { TraceWriter.TraceWarning("No text or current word"); return; } if (_replacementWord.Length == 0) { DeleteWord(); return; } string replacedWord = CurrentWord; int replacedIndex = WordIndex; int index = _words[replacedIndex].Index; int length = _words[replacedIndex].Length; _text.Remove(index, length); // if first letter upper case, match case for replacement word if (char.IsUpper(_words[replacedIndex].ToString(), 0)) { _replacementWord = _replacementWord.Substring(0, 1).ToUpper(CultureInfo.CurrentUICulture) + _replacementWord.Substring(1); } _text.Insert(index, _replacementWord); CalculateWords(); OnReplacedWord(new ReplaceWordEventArgs( _replacementWord, replacedWord, replacedIndex, index)); }
/// <summary> /// Populates the <see cref="Suggestions"/> property with word suggestions /// for the <see cref="CurrentWord"/> /// </summary> /// <remarks> /// <see cref="TestWord"/> must have been called before calling this method /// </remarks> /// <seealso cref="CurrentWord"/> /// <seealso cref="Suggestions"/> /// <seealso cref="TestWord"/> public void Suggest() { // can't generate suggestions with out current word if (this.CurrentWord.Length == 0) { TraceWriter.TraceWarning("No current word"); return; } this.Initialize(); List <Word> tempSuggestion = new List <Word>(); if ((_suggestionMode == SuggestionEnum.PhoneticNearMiss || _suggestionMode == SuggestionEnum.Phonetic) && _dictionary.PhoneticRules.Count > 0) { // generate phonetic code for possible root word Hashtable codes = new Hashtable(); foreach (string tempWord in _dictionary.PossibleBaseWords) { string tempCode = _dictionary.PhoneticCode(tempWord); if (tempCode.Length > 0 && !codes.ContainsKey(tempCode)) { codes.Add(tempCode, tempCode); } } if (codes.Count > 0) { // search root words for phonetic codes foreach (Word word in _dictionary.BaseWords.Values) { if (codes.ContainsKey(word.PhoneticCode)) { List <string> words = _dictionary.ExpandWord(word); // add expanded words foreach (string expandedWord in words) { Word newWord = new Word(); newWord.Text = expandedWord; newWord.EditDistance = this.EditDistance(this.CurrentWord, expandedWord); tempSuggestion.Add(newWord); } } } } TraceWriter.TraceVerbose("Suggestiongs Found with Phonetic Stratagy: {0}", tempSuggestion.Count); } if (_suggestionMode == SuggestionEnum.PhoneticNearMiss || _suggestionMode == SuggestionEnum.NearMiss) { // suggestions for a typical fault of spelling, that // differs with more, than 1 letter from the right form. this.ReplaceChars(ref tempSuggestion); // swap out each char one by one and try all the tryme // chars in its place to see if that makes a good word this.BadChar(ref tempSuggestion); // try omitting one char of word at a time this.ExtraChar(ref tempSuggestion); // try inserting a tryme character before every letter this.ForgotChar(ref tempSuggestion); // split the string into two pieces after every char // if both pieces are good words make them a suggestion this.TwoWords(ref tempSuggestion); // try swapping adjacent chars one by one this.SwapChar(ref tempSuggestion); } TraceWriter.TraceVerbose("Total Suggestiongs Found: {0}", tempSuggestion.Count); tempSuggestion.Sort(); // sorts by edit score _suggestions.Clear(); for (int i = 0; i < tempSuggestion.Count; i++) { string word = ((Word)tempSuggestion[i]).Text; // looking for duplicates if (!_suggestions.Contains(word)) { // populating the suggestion list _suggestions.Add(word); } if (_suggestions.Count >= _maxSuggestions && _maxSuggestions > 0) { break; } } } // suggest
/// <summary> /// Populates the <see cref="Suggestions"/> property with word suggestions /// for the <see cref="CurrentWord"/> /// </summary> /// <remarks> /// <see cref="TestWord()"/> or <see cref="TestWord(string)"/> must have been called before calling this method /// </remarks> /// <seealso cref="CurrentWord"/> /// <seealso cref="Suggestions"/> /// <seealso cref="TestWord()"/> /// <seealso cref="TestWord(string)"/> public void Suggest() { // can't generate suggestions with out current word if (CurrentWord.Length == 0) { TraceWriter.TraceWarning("No current word"); return; } Initialize(); var tempSuggestion = new List <Word>(); if ((SuggestionMode == SuggestionEnum.PhoneticNearMiss || SuggestionMode == SuggestionEnum.Phonetic) && _dictionary.PhoneticRules.Count > 0) { // generate phonetic code for possible root word Dictionary <string, string> codes = new Dictionary <string, string>(); foreach (string tempWord in _dictionary.PossibleBaseWords) { string tempCode = _dictionary.PhoneticCode(tempWord); if (tempCode.Length > 0 && !codes.ContainsKey(tempCode)) { codes.Add(tempCode, tempCode); } } if (codes.Count > 0) { // search root words for phonetic codes foreach (Word word in _dictionary.BaseWords.Values) { if (codes.ContainsKey(word.PhoneticCode)) { List <string> words = _dictionary.ExpandWord(word); // add expanded words foreach (string expandedWord in words) { SuggestWord(expandedWord, tempSuggestion); } } } } TraceWriter.TraceVerbose("Suggestions Found with Phonetic Strategy: {0}", tempSuggestion.Count); } if (SuggestionMode == SuggestionEnum.PhoneticNearMiss || SuggestionMode == SuggestionEnum.NearMiss) { // suggestions for a typical fault of spelling, that // differs with more, than 1 letter from the right form. ReplaceChars(tempSuggestion); // swap out each char one by one and try all the tryme // chars in its place to see if that makes a good word BadChar(tempSuggestion); // try omitting one char of word at a time ExtraChar(tempSuggestion); // try inserting a tryme character before every letter ForgotChar(tempSuggestion); // split the string into two pieces after every char // if both pieces are good words make them a suggestion TwoWords(tempSuggestion); // try swapping adjacent chars one by one SwapChar(tempSuggestion); } TraceWriter.TraceVerbose("Total Suggestions Found: {0}", tempSuggestion.Count); tempSuggestion.Sort(); // sorts by edit score Suggestions.Clear(); foreach (var suggestion in tempSuggestion) { string word = suggestion.Text; // looking for duplicates if (!Suggestions.Contains(word)) { // populating the suggestion list Suggestions.Add(word); } if (Suggestions.Count >= MaxSuggestions && MaxSuggestions > 0) { break; } } }