示例#1
0
        private void SpellChecker_MisspelledWord(object sender, NetSpell.SpellChecker.SpellingEventArgs e)
        {
            this.UpdateDisplay(this.SpellChecker.Text, e.Word,
                               e.WordIndex, e.TextIndex);

            //turn on ignore all option
            this.IgnoreAllButton.Enabled  = true;
            this.ReplaceAllButton.Enabled = true;

            //generate suggestions
            SpellChecker.Suggest();

            //display suggestions
            this.SuggestionList.Items.AddRange((string[])SpellChecker.Suggestions.ToArray(typeof(string)));
        }
    /// <summary>
    /// Did you mean suggestion.
    /// </summary>
    private static string DidYouMean(string dictionaryFile, string searchQuery, List<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 eventLog = new EventLogProvider();
                eventLog.LogEvent(EventLogProvider.EVENT_TYPE_ERROR, DateTime.Now, "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;
    }