protected void SpellChecker_MisspelledWord(object sender, SpellingEventArgs e) { // Ignore macro content if (IsInMacro(e.TextIndex)) { SpellChecker.IgnoreWord(); SpellChecker.SpellCheck(); return; } SaveValues(); CurrentWord.Text = SpellChecker.CurrentWord; SpellChecker.Suggest(); Suggestions.DataSource = SpellChecker.Suggestions; Suggestions.DataBind(); if (Suggestions.Items.Count > 0) { Suggestions.SelectedIndex = 0; } ReplacementWord.Text = Suggestions.SelectedValue; SpellMode.Value = "suggest"; StatusText.Text = string.Format(wordCounterString, SpellChecker.WordIndex + 1, SpellChecker.WordCount); }
public void NoText() { Spelling _SpellChecker = NewSpellChecker(); Assert.AreEqual(string.Empty, _SpellChecker.CurrentWord, "Incorrect Current Word"); _SpellChecker.WordIndex = 1; Assert.AreEqual(0, _SpellChecker.WordIndex, "Incorrect Word Index"); Assert.AreEqual(0, _SpellChecker.WordCount, "Incorrect Word Count"); Assert.AreEqual(0, _SpellChecker.TextIndex, "Incorrect Text Index"); _SpellChecker.DeleteWord(); Assert.AreEqual(string.Empty, _SpellChecker.Text, "Incorrect Text"); _SpellChecker.IgnoreWord(); Assert.AreEqual(string.Empty, _SpellChecker.Text, "Incorrect Text"); _SpellChecker.ReplaceWord("Test"); Assert.AreEqual(string.Empty, _SpellChecker.Text, "Incorrect Text"); Assert.IsFalse(_SpellChecker.SpellCheck(), "Spell Check not false"); _SpellChecker.Suggest(); Assert.AreEqual(0, _SpellChecker.Suggestions.Count, "Generated Suggestions with no text"); }
protected void SpellChecker_MisspelledWord(object sender, SpellingEventArgs e) { SaveValues(); CurrentWord.Text = SpellChecker.CurrentWord; SpellChecker.Suggest(); Suggestions.DataSource = SpellChecker.Suggestions; Suggestions.DataBind(); ReplacementWord.Text = string.Empty; SpellMode.Value = "suggest"; StatusText.Text = string.Format(wordCounterString, SpellChecker.WordIndex + 1, SpellChecker.WordCount); }
public void Suggest() { Spelling _SpellChecker = NewSpellChecker(); _SpellChecker.Text = "this is tst"; _SpellChecker.SpellCheck(); Assertion.AssertEquals("Incorrect WordOffset", 2, _SpellChecker.WordIndex); Assertion.AssertEquals("Incorrect CurrentWord", "tst", _SpellChecker.CurrentWord); _SpellChecker.Suggest(); Assertion.AssertEquals("Incorrect Suggestion Count", 25, _SpellChecker.Suggestions.Count); Assertion.AssertEquals("Could not find 'test' in suggestions", true, _SpellChecker.Suggestions.Contains("test")); }
public void Suggest() { Spelling _SpellChecker = NewSpellChecker(); _SpellChecker.Text = "this is tst"; _SpellChecker.SpellCheck(); Assert.True(2 == _SpellChecker.WordIndex, "Incorrect WordOffset"); Assert.True("tst" == _SpellChecker.CurrentWord, "Incorrect CurrentWord"); _SpellChecker.Suggest(); Assert.True(25 == _SpellChecker.Suggestions.Count, "Incorrect Suggestion Count"); Assert.True(true == _SpellChecker.Suggestions.Contains("test"), "Could not find 'test' in suggestions"); }
private string FindSomeSuggestions(string word) { spellCheck.Suggest(word); List <string> suggs = new List <string>(); for (int i = 0; i < spellCheck.Suggestions.Count && i < 3; i++) { suggs.Add(CapFirst(spellCheck.Suggestions[i] as string)); } string sugg = string.Join(" or ", suggs); return(sugg.Length > 0 ? " Did you mean: " + sugg + "?" : ""); }
/// <summary> /// Provides a few suggestions for spelling mistakes to the given word strings /// </summary> /// <param name="words">as many strings to provide suggestions for as necessary</param> /// <returns>a string listing the suggestions</returns> private string FindSomeSuggestions(params string[] words) { List <string> suggs = new List <string>(); foreach (string word in words) { //find suggestions spellCheck.Suggest(word); //add a max of 2 suggestions/word for (int i = 0; i < spellCheck.Suggestions.Count && i < MAX_SUGGESTIONS; i++) { suggs.Add(Name.FixupCapitals(spellCheck.Suggestions[i] as string)); } } //join all suggestions together string sugg = string.Join(" or ", suggs); return(sugg.Length > 0 ? " Did you mean: " + sugg + "?" : ""); }
private void SpellCheckContextMenuOpening(object sender, CancelEventArgs e) { SpellCheckContextMenu.Items.Clear(); try { var pos = TextBox.GetCharIndexFromPosition(TextBox.PointToClient(MousePosition)); if (pos <= 0) { e.Cancel = true; return; } _spelling.Text = TextBox.Text; _spelling.WordIndex = _spelling.GetWordIndexFromTextIndex(pos); _spelling.ShowDialog = false; _spelling.MaxSuggestions = 10; //generate suggestions _spelling.Suggest(); var addToDictionary = SpellCheckContextMenu.Items.Add(addToDictionaryText.Text); addToDictionary.Click += AddToDictionaryClick; var ignoreWord = SpellCheckContextMenu.Items.Add(ignoreWordText.Text); ignoreWord.Click += IgnoreWordClick; var removeWord = SpellCheckContextMenu.Items.Add(removeWordText.Text); removeWord.Click += RemoveWordClick; SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); bool suggestionsFound = false; foreach (var suggestion in (string[])_spelling.Suggestions.ToArray(typeof(string))) { var suggestionToolStripItem = SpellCheckContextMenu.Items.Add(suggestion); suggestionToolStripItem.Click += SuggestionToolStripItemClick; suggestionsFound = true; } if (suggestionsFound) { SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); } } catch (Exception ex) { Trace.WriteLine(ex); } if (!string.IsNullOrEmpty(_spelling.CurrentWord)) { var translate = new ToolStripMenuItem(string.Format(translateCurrentWord.Text, _spelling.CurrentWord, CultureCodeToString(Settings.Dictionary))); translate.Click += translate_Click; SpellCheckContextMenu.Items.Add(translate); } var translateText = new ToolStripMenuItem(string.Format(translateEntireText.Text, CultureCodeToString(Settings.Dictionary))); translateText.Click += translateText_Click; SpellCheckContextMenu.Items.Add(translateText); SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); try { var dictionaryToolStripMenuItem = new ToolStripMenuItem(dictionaryText.Text); SpellCheckContextMenu.Items.Add(dictionaryToolStripMenuItem); var toolStripDropDown = new ContextMenuStrip(); var noDicToolStripMenuItem = new ToolStripMenuItem("None"); noDicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == "None") { noDicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(noDicToolStripMenuItem); foreach ( var fileName in Directory.GetFiles(Settings.GetDictionaryDir(), "*.dic", SearchOption.TopDirectoryOnly)) { var file = new FileInfo(fileName); var dic = file.Name.Replace(".dic", ""); var dicToolStripMenuItem = new ToolStripMenuItem(dic); dicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == dic) { dicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(dicToolStripMenuItem); } dictionaryToolStripMenuItem.DropDown = toolStripDropDown; } catch (Exception ex) { Trace.WriteLine(ex); } SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); var mi = new ToolStripMenuItem(markIllFormedLinesText.Text) { Checked = Settings.MarkIllFormedLinesInCommitMsg }; mi.Click += MarkIllFormedLinesInCommitMsgClick; SpellCheckContextMenu.Items.Add(mi); }
public void SuggestionRank() { string invalidFile = @"..\src\NetSpell.Tests\Data\SuggestionTest.txt"; // open file FileStream fs = new FileStream(invalidFile, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(fs, Encoding.UTF7); int totalFound = 0; int totalChecked = 0; int totalFirst = 0; int totalTopFive = 0; int totalTopTen = 0; int totalTopTwentyFive = 0; Console.WriteLine("Misspelled\tCorrect\tPosition\tCount"); // read line by line while (sr.Peek() >= 0) { string tempLine = sr.ReadLine().Trim(); if (tempLine.Length > 0) { string[] parts = tempLine.Split(); string misSpelled = parts[0]; string correctSpelled = parts[1]; if (parts.Length > 2) { correctSpelled += " " + parts[2]; } bool found = false; if (_SpellChecker.SpellCheck(misSpelled)) { totalChecked++; _SpellChecker.Suggest(); int position = 0; foreach (string suggestion in _SpellChecker.Suggestions) { position++; if (suggestion.ToLower() == correctSpelled.ToLower()) { Console.WriteLine("{0}\t{1}\t{2}\t{3}", misSpelled, correctSpelled, position.ToString(), _SpellChecker.Suggestions.Count.ToString()); found = true; totalFound++; if (position == 1) { totalFirst++; } else if (position <= 5) { totalTopFive++; } else if (position <= 10) { totalTopTen++; } else if (position <= 25) { totalTopTwentyFive++; } break; } } if (!found) { if (_SpellChecker.Suggestions.Count > 0) { Console.WriteLine("{0}\t{1}\t{2}\t{3}", misSpelled, correctSpelled, "0", _SpellChecker.Suggestions.Count.ToString()); } else { Console.WriteLine("{0}\t{1}\t{2}\t{3}", misSpelled, correctSpelled, "-1", "-1"); } } } else { Console.WriteLine("{0}\t{1}\t{2}\t{3}", misSpelled, correctSpelled, "1", "1"); } } } totalTopFive += totalFirst; totalTopTen += totalTopFive; totalTopTwentyFive += totalTopTen; Console.WriteLine("Total Tested\t{0}", totalChecked); Console.WriteLine("Total Found\t{0}\t{1}%", totalFound, ((float)totalFound / (float)totalChecked * 100f)); Console.WriteLine("First Suggestions\t{0}\t{1}%", totalFirst, ((float)totalFirst / (float)totalChecked * 100f)); Console.WriteLine("Top 5 Suggestions\t{0}\t{1}%", totalTopFive, ((float)totalTopFive / (float)totalChecked * 100f)); Console.WriteLine("Top 10 Suggestions\t{0}\t{1}%", totalTopTen, ((float)totalTopTen / (float)totalChecked * 100f)); Console.WriteLine("Top 25 Suggestions\t{0}\t{1}%", totalTopTwentyFive, ((float)totalTopTwentyFive / (float)totalChecked * 100f)); sr.Close(); fs.Close(); }
private void SpellCheckContextMenuOpening(object sender, CancelEventArgs e) { TextBox.Focus(); SpellCheckContextMenu.Items.Clear(); try { var pos = TextBox.GetCharIndexFromPosition(TextBox.PointToClient(MousePosition)); if (pos < 0) { e.Cancel = true; return; } _spelling.Text = TextBox.Text; _spelling.WordIndex = _spelling.GetWordIndexFromTextIndex(pos); if (_spelling.CurrentWord.Length != 0 && !_spelling.TestWord()) { _spelling.ShowDialog = false; _spelling.MaxSuggestions = 5; //generate suggestions _spelling.Suggest(); foreach (var suggestion in _spelling.Suggestions) { var si = AddContextMenuItem(suggestion, SuggestionToolStripItemClick); si.Font = new System.Drawing.Font(si.Font, FontStyle.Bold); } AddContextMenuItem(addToDictionaryText.Text, AddToDictionaryClick) .Enabled = (_spelling.CurrentWord.Length > 0); AddContextMenuItem(ignoreWordText.Text, IgnoreWordClick) .Enabled = (_spelling.CurrentWord.Length > 0); AddContextMenuItem(removeWordText.Text, RemoveWordClick) .Enabled = (_spelling.CurrentWord.Length > 0); if (_spelling.Suggestions.Count > 0) { AddContextMenuSeparator(); } } } catch (Exception ex) { Trace.WriteLine(ex); } AddContextMenuItem(cutMenuItemText.Text, CutMenuItemClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(copyMenuItemText.Text, CopyMenuItemdClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(pasteMenuItemText.Text, PasteMenuItemClick) .Enabled = Clipboard.ContainsText(); AddContextMenuItem(deleteMenuItemText.Text, DeleteMenuItemClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(selectAllMenuItemText.Text, SelectAllMenuItemClick); /*AddContextMenuSeparator(); * * if (!string.IsNullOrEmpty(_spelling.CurrentWord)) * { * string text = string.Format(translateCurrentWord.Text, _spelling.CurrentWord, CultureCodeToString(Settings.Dictionary)); * AddContextMenuItem(text, translate_Click); * } * * string entireText = string.Format(translateEntireText.Text, CultureCodeToString(Settings.Dictionary)); * AddContextMenuItem(entireText, translateText_Click);*/ AddContextMenuSeparator(); try { var dictionaryToolStripMenuItem = new ToolStripMenuItem(dictionaryText.Text); SpellCheckContextMenu.Items.Add(dictionaryToolStripMenuItem); var toolStripDropDown = new ContextMenuStrip(); var noDicToolStripMenuItem = new ToolStripMenuItem("None"); noDicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == "None") { noDicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(noDicToolStripMenuItem); foreach ( var fileName in Directory.GetFiles(AppSettings.GetDictionaryDir(), "*.dic", SearchOption.TopDirectoryOnly)) { var file = new FileInfo(fileName); var dic = file.Name.Replace(".dic", ""); var dicToolStripMenuItem = new ToolStripMenuItem(dic); dicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == dic) { dicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(dicToolStripMenuItem); } dictionaryToolStripMenuItem.DropDown = toolStripDropDown; } catch (Exception ex) { Trace.WriteLine(ex); } AddContextMenuSeparator(); var mi = new ToolStripMenuItem(markIllFormedLinesText.Text) { Checked = AppSettings.MarkIllFormedLinesInCommitMsg }; mi.Click += MarkIllFormedLinesInCommitMsgClick; SpellCheckContextMenu.Items.Add(mi); }
/// <summary> /// Did you mean suggestion. /// </summary> private static string DidYouMean(string dictionaryFile, string searchQuery, IEnumerable <string> searchTerms) { if (searchTerms != null) { Spelling SpellChecker = null; WordDictionary WordDictionary = null; #region "Word dictionary" // If not in cache, create new WordDictionary = new WordDictionary(); WordDictionary.EnableUserFile = false; // Getting folder for dictionaries string folderName = HttpContext.Current.Request.MapPath("~/App_Data/Dictionaries/"); // Check if dictionary file exists string fileName = Path.Combine(folderName, dictionaryFile); if (!File.Exists(fileName)) { EventLogProvider.LogEvent(EventType.ERROR, "DidYouMean webpart", "Dictionary file not found!"); return(String.Empty); } WordDictionary.DictionaryFolder = folderName; WordDictionary.DictionaryFile = dictionaryFile; // Load and initialize the dictionary WordDictionary.Initialize(); #endregion #region "SpellCheck" // Prepare spellchecker SpellChecker = new Spelling(); SpellChecker.Dictionary = WordDictionary; SpellChecker.SuggestionMode = Spelling.SuggestionEnum.NearMiss; bool suggest = false; // Check all searched terms foreach (string term in searchTerms) { if (term.Length > 2) { SpellChecker.Suggest(term); ArrayList al = SpellChecker.Suggestions; // If there are some suggestions if ((al != null) && (al.Count > 0)) { suggest = true; // Expression to find term Regex regex = RegexHelper.GetRegex("([\\+\\-\\s\\(]|^)" + term + "([\\s\\)]|$)"); // Change term in original search query string suggestion = "$1" + startTag + al[0] + endTag + "$2"; searchQuery = regex.Replace(searchQuery, suggestion); } } } #endregion if (suggest) { return(searchQuery); } } return(String.Empty); }
private XmlDocument CreateServerXmlDocumentResponse(string action, string text) { Regex _wordEx, _htmlEx; XmlDocument result = new XmlDocument(); result.PreserveWhitespace = true; XmlDeclaration xmlDecl = result.CreateXmlDeclaration("1.0", "utf-8", ""); result.AppendChild(xmlDecl); XmlElement webmailNode = result.CreateElement("webmail"); result.AppendChild(webmailNode); XmlElement spellcheckNode = result.CreateElement("spellcheck"); webmailNode.AppendChild(spellcheckNode); if (action == "spell") { spellcheckNode.SetAttribute("action", "spellcheck"); _htmlEx = new Regex(@"(<.*?\>)|(&[^;]{1,5};)", RegexOptions.IgnoreCase & RegexOptions.Compiled); text = _htmlEx.Replace(text, ReplaceHtml); Log.WriteLine("", ">>>>>>>>>>>>>>>> TEXT >>>>>>>>>>>>>>>>"); Log.WriteLine("", text); Log.WriteLine("", ">>>>>>>>>>>>>>>> TEXT >>>>>>>>>>>>>>>>"); _wordEx = new Regex(@"\b[" + WordDictionary.TryCharacters + @"']+\b", RegexOptions.Compiled); MatchCollection words = _wordEx.Matches(text); foreach (Match word in words) { if (!SpellChecker.TestWord(word.ToString())) { XmlElement node = result.CreateElement("misp"); spellcheckNode.AppendChild(node); node.SetAttribute("pos", word.Index.ToString()); node.SetAttribute("len", word.Length.ToString()); } } } else if (action == "suggest") { spellcheckNode.SetAttribute("action", "suggest"); SpellChecker.Suggest(text); int i = 0; foreach (string word in SpellChecker.Suggestions) { if (i++ < MAX_SUGGESTIONS) { XmlElement node = result.CreateElement("word"); spellcheckNode.AppendChild(node); //node.SetAttribute("name", "word"); node.AppendChild(webmailNode.OwnerDocument.CreateCDataSection(Utils.EncodeCDATABody(word))); //node.SetAttribute("value", word); } } } return(result); }