예제 #1
0
        /// <summary>
        /// The suggest.
        /// </summary>
        /// <param name="word">
        /// The word.
        /// </param>
        /// <returns>
        /// </returns>
        /// <exception cref="ObjectDisposedException">
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public List <string> Suggest(string word)
        {
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException("SpellFactory");
            }

            if (this.hunspells == null)
            {
                throw new InvalidOperationException("Hunspell Dictionary isn't loaded");
            }

            this.hunspellSemaphore.WaitOne();
            Hunspell current = null;

            try
            {
                current = this.hunspells.Pop();
                return(current.Suggest(word));
            }
            finally
            {
                if (current != null)
                {
                    this.hunspells.Push(current);
                }

                this.hunspellSemaphore.Release();
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: AliHosny/weet-it
        static void Main(string[] args)
        {
            Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic");

            //The folliwng is the trying of the spell checking
            Console.WriteLine("Trying Spell Checking for the word 'Recommendation'");
            Console.WriteLine(hunspell.Spell("Recommendation"));

            //The following is the trying of the suggesstions
            Console.WriteLine("\n\n");
            Console.WriteLine("Trying the suggesstions of the word 'Recommnedatio'");
            List<string> suggesstions = new List<string>();
            suggesstions = hunspell.Suggest("Recommnedatio");
            foreach (string item in suggesstions)
            {
                Console.WriteLine("    --" + item);
            }

            //The following is the trying of analysis of word
            Console.WriteLine("\n\n");
            Console.WriteLine("Analyze the word 'children'");
            List<string> morphs = hunspell.Analyze("children");
            foreach (string morph in morphs)
            {
                Console.WriteLine("Morph is: " + morph);
            }

            //The following is the trying of Stemming
            Console.WriteLine("\n\n");
            Console.WriteLine("Find the word stem of the word 'children'");
            List<string> stems = hunspell.Stem("children");
            foreach (string stem in stems)
            {
                Console.WriteLine("Word Stem is: " + stem);
            }

            //Now for the synonym functions
            Console.WriteLine("\n\n\nThesaurus/Synonym Functions");
            Console.WriteLine("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");

            //Creating a new instance of the thesarus
            MyThes thes = new MyThes("th_en_us_v2.dat");

            //Synonyms for words
            Console.WriteLine("Get the synonyms of the plural word 'children'");
            ThesResult tr = thes.Lookup("how", hunspell);

            if (tr.IsGenerated)
                Console.WriteLine("Generated over stem (The original word form wasn't in the thesaurus)");
            foreach (ThesMeaning meaning in tr.Meanings)
            {
                Console.WriteLine();
                Console.WriteLine("  Meaning: " + meaning.Description);
                foreach (string synonym in meaning.Synonyms)
                {
                    Console.WriteLine("    Synonym: " + synonym);

                }
            }
        }
예제 #3
0
        /// <summary>
        /// The suggest.
        /// </summary>
        /// <param name="word">
        /// The word.
        /// </param>
        /// <returns>
        /// The <see cref="List{T}"/>.
        /// </returns>
        /// <exception cref="ObjectDisposedException">
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public List <string> Suggest(string word)
        {
            Hunspell hunspell = this.HunspellsPop();

            try
            {
                return(hunspell.Suggest(word));
            }
            finally
            {
                this.HunspellsPush(hunspell);
            }
        }
        internal string CheckEnglishSpelling()
        {
            bool correct;
            List<string> suggestions = new List<string>();
            //Generate XML for the AtD plugin to read.
            StringWriter stringwriter = new StringWriter();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            XmlWriter xmlWriter = XmlWriter.Create(stringwriter, settings);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("results");
            string lastWord = string.Empty;
            //We create hunspell object and pass the location of the english dictionary.
            using (Hunspell hunspell = new Hunspell(System.Web.HttpContext.Current.Server.MapPath("~/Content/Dictionaries/en_US.aff"),
            System.Web.HttpContext.Current.Server.MapPath("~/Content/Dictionaries/en_US.dic")))
            {
                //Split the paragraph to words
                List<string> words = Regex.Split(EnglishSynopsis, @"\W+").ToList();
                foreach (string word in words)
                {
                    //Check the spelling and returns true or false
                    correct = hunspell.Spell(word);

                    if (!correct)
                    {
                        xmlWriter.WriteStartElement("error");
                        xmlWriter.WriteElementString("string", word);
                        xmlWriter.WriteElementString("description", "Spelling");
                        xmlWriter.WriteElementString("precontext", lastWord);
                        xmlWriter.WriteStartElement("suggestions");
                        //Returns list of suggestion for the incorrect word
                        suggestions = hunspell.Suggest(word);
                        foreach (string suggestion in suggestions)
                        {
                            xmlWriter.WriteElementString("option", suggestion);
                        }
                        xmlWriter.WriteEndElement();
                        xmlWriter.WriteElementString("type", "spelling");
                        xmlWriter.WriteEndElement();
                    }
                    lastWord = word;
                }
            }
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return stringwriter.ToString();
        }
예제 #5
0
        /// <summary>
        /// Gets spelling suggestions for the specified word
        /// </summary>
        /// <param name="affFile">The affFile path</param>
        /// <param name="dicFile">The dictionary file path</param>
        /// <param name="word">The word to get suggestions</param>
        /// <returns>A json list of suggestions for the specified word.</returns>
        public static string GetWordSuggestions(string affFile, string dicFile, string word)
        {
            List<string> suggestions = new List<string>();

            try
            {
                using (Hunspell hunspell = new Hunspell(affFile, dicFile))
                {
                    suggestions = hunspell.Suggest(word);
                }
            }
            catch (Exception exception)
            {
                //need to handle this better and/or add logging
            }

            return JsonConvert.SerializeObject(suggestions);
        }
        private void CheckWords(Word.Document doc, Hunspell hunspell, string selectedText, string[] khmerwords, out bool stopcheck, out bool repeatcheck, string objecttype = "", object wordobject = null)
        {
            int startposition = 0;
            Object oMissing = System.Reflection.Missing.Value;
            stopcheck = repeatcheck = false;

            //Check all the Khmer words from the selected line
            foreach (string khmerword in khmerwords)
            {
                DialogResult dialogResult = DialogResult.None;
                frmKhmer frmKhmer = null;
                String newKhmerWord = String.Empty;

                if (!hunspell.Spell(khmerword))
                {
                    if (!ignoreAllWords.Any(ignoreAllWord => ignoreAllWord.khmerword == khmerword))
                    {
                        if (!ignoreWords.Contains(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition, ignoreAll = false }))
                        {
                            Word.Range start = null;
                            Word.WdColorIndex highlightcolorindex = Word.WdColorIndex.wdNoHighlight;
                            Word.WdUnderline fontunderline = Word.WdUnderline.wdUnderlineNone;
                            Word.WdColor fontcolor = Word.WdColor.wdColorBlack;
                            Word.Range selectionRange = null;

                            //Select the erroneous word on the main document
                            if (String.IsNullOrWhiteSpace(objecttype))
                            {
                                //Set the initial selection
                                start = doc.ActiveWindow.Selection.Range;

                                //Set the search area
                                doc.ActiveWindow.Selection.Start += startposition;
                                Word.Selection searchArea = doc.ActiveWindow.Selection;

                                //Set the find object
                                Word.Find findObject = searchArea.Find;
                                findObject.ClearFormatting();
                                findObject.Text = khmerword;


                                //Find the mis-spelled word
                                findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                                //Temp store the current formatting
                                highlightcolorindex = doc.ActiveWindow.Selection.Range.HighlightColorIndex;
                                fontunderline = doc.ActiveWindow.Selection.Range.Font.Underline;
                                fontcolor = doc.ActiveWindow.Selection.Range.Font.UnderlineColor;

                                //Highlight the selection
                                doc.ActiveWindow.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                                doc.ActiveWindow.Selection.Range.Font.Underline = Word.WdUnderline.wdUnderlineWavy;
                                doc.ActiveWindow.Selection.Range.Font.UnderlineColor = Word.WdColor.wdColorRed;
                                selectionRange = doc.ActiveWindow.Selection.Range;
                                doc.ActiveWindow.Selection.Collapse();
                            }
                            else
                            {
                                if (objecttype == "table")
                                {
                                    start = ((Word.Cell)wordobject).Range;
                                }
                                else if (objecttype == "shape")
                                {
                                    start = ((Word.Shape)wordobject).TextFrame.TextRange;
                                    start.Start += startposition;
                                }

                                //Set the find object
                                Word.Find findObject = start.Find;
                                findObject.ClearFormatting();
                                findObject.Text = khmerword;

                                //Temp store the current formatting
                                highlightcolorindex = start.HighlightColorIndex;
                                fontunderline = start.Font.Underline;
                                fontcolor = start.Font.UnderlineColor;

                                //Find the mis-spelled word
                                findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                                //Highlight the selection
                                start.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                                start.Font.Underline = Word.WdUnderline.wdUnderlineWavy;
                                start.Font.UnderlineColor = Word.WdColor.wdColorRed;
                                start.Select();
                            }

                            bool isObject = !String.IsNullOrWhiteSpace(objecttype);
                            frmKhmer = new frmKhmer(selectedText, khmerword, startposition, hunspell.Suggest(khmerword), isObject);
                            dialogResult = frmKhmer.ShowDialog();

                            //Select the line again
                            if (String.IsNullOrWhiteSpace(objecttype))
                            {
                                //Revert the highlights
                                selectionRange.Select();
                                doc.ActiveWindow.Selection.Range.HighlightColorIndex = highlightcolorindex;
                                doc.ActiveWindow.Selection.Range.Font.Underline = fontunderline;
                                doc.ActiveWindow.Selection.Range.Font.UnderlineColor = fontcolor;

                                if (dialogResult != DialogResult.Abort) start.Select();
                            }
                            else
                            {
                                start.HighlightColorIndex = highlightcolorindex;
                                start.Font.Underline = fontunderline;
                                start.Font.UnderlineColor = fontcolor;

                                if (dialogResult != DialogResult.Abort)
                                {
                                    if (objecttype == "table")
                                    {
                                        ((Word.Cell)wordobject).Select();
                                    }
                                    else if (objecttype == "shape")
                                    {
                                        ((Word.Shape)wordobject).Select();
                                    }
                                }
                            }
                        }
                    }
                }

                #region Cancel Button Clicked
                //Return if the user hits Cancel Button
                if (dialogResult == DialogResult.Cancel || dialogResult == DialogResult.Abort)
                {
                    stopcheck = true;
                    repeatcheck = false;
                    return;
                }
                #endregion

                #region Ignore or Ignore All Clicked
                //Ignore the word
                if (dialogResult == DialogResult.Ignore)
                {
                    if (frmKhmer.ignoreAll)
                    {
                        ignoreAllWords.Add(new IgnoreWord { khmerword = khmerword, ignoreAll = frmKhmer.ignoreAll });
                    }
                    else
                    {
                        ignoreWords.Add(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition });
                    }
                }
                #endregion

                #region Change or Change All Clicked
                if (dialogResult == DialogResult.Yes)
                {
                    if (String.IsNullOrWhiteSpace(objecttype))
                    {
                        //Set the initial selection
                        Word.Range start = doc.ActiveWindow.Selection.Range;

                        //Set the searcharea
                        if (frmKhmer.changeAll)
                        {
                            doc.Content.Select();
                        }
                        Word.Selection searchArea = doc.ActiveWindow.Selection;

                        //Set the find object
                        Word.Find findObject = searchArea.Find;
                        findObject.ClearFormatting();
                        findObject.Text = khmerword;
                        findObject.Replacement.ClearFormatting();
                        findObject.Replacement.Text = frmKhmer.selectedSuggestion;

                        object replaceAll = frmKhmer.changeAll ? Word.WdReplace.wdReplaceAll : Word.WdReplace.wdReplaceOne;

                        findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                        newKhmerWord = frmKhmer.selectedSuggestion;

                        //Set back the selection
                        start.Select();

                        //Set repeatcheck to true
                        if (frmKhmer.changeAll)
                        {
                            stopcheck = false;
                            repeatcheck = true;
                            return;
                        }
                    }
                    else
                    {
                        var resultingText = selectedText.Replace(khmerword, frmKhmer.selectedSuggestion);

                        if (objecttype == "table")
                        {
                            Word.Range range = ((Word.Cell)wordobject).Range;
                            range.Text = resultingText;
                        }
                        else if (objecttype == "shape")
                        {
                            Word.Shape shape = (Word.Shape)wordobject;
                            shape.TextFrame.TextRange.Text = resultingText;
                        }

                        stopcheck = false;
                        repeatcheck = true;
                        return;
                    }
                }
                #endregion

                startposition += String.IsNullOrWhiteSpace(newKhmerWord) ? khmerword.Length : newKhmerWord.Length;
            }
        }
        private static List<string> Suggestions(string word)
        {
            List<string> result;
             using (var hunspell = new Hunspell(AffFilePath, DictionaryFilePath))
             {
            result = hunspell.Suggest(word);
             }

             return result;
        }