Ejemplo n.º 1
0
        /// <summary>
        /// Look up the synonyms for the word.
        /// </summary>
        /// <param name="word">
        /// The word.
        /// </param>
        /// <param name="useGeneration">
        /// if set to <c>true</c> use generation to get synonyms over the word stem.
        /// </param>
        /// <returns>
        /// The <see cref="ThesResult"/>.
        /// </returns>
        public ThesResult LookupSynonyms(string word, bool useGeneration)
        {
            MyThes   currentThes     = null;
            Hunspell currentHunspell = null;

            try
            {
                currentThes = this.MyThesPop();
                if (useGeneration)
                {
                    currentHunspell = this.HunspellsPop();
                    return(currentThes.Lookup(word, currentHunspell));
                }

                return(currentThes.Lookup(word));
            }
            finally
            {
                if (currentThes != null)
                {
                    this.MyThesPush(currentThes);
                }

                if (currentHunspell != null)
                {
                    this.HunspellsPush(currentHunspell);
                }
            }
        }
Ejemplo n.º 2
0
        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);

                }
            }
        }
Ejemplo n.º 3
0
 void MyThesPush(MyThes myThes)
 {
     lock (myThesesLock)
     {
         this.myTheses.Push(myThes);
     }
     myThesSemaphore.Release();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Look up the synonyms for the word.
        /// </summary>
        /// <param name="word">
        /// The word.
        /// </param>
        /// <param name="useGeneration">
        /// if set to <c>true</c> use generation to get synonyms over the word stem.
        /// </param>
        /// <returns>
        /// </returns>
        public ThesResult LookupSynonyms(string word, bool useGeneration)
        {
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException("SpellFactory");
            }

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

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

            MyThes   currentThes     = null;
            Hunspell currentHunspell = null;

            this.myThesSemaphore.WaitOne();
            if (useGeneration)
            {
                this.hunspellSemaphore.WaitOne();
            }

            try
            {
                currentThes = this.myTheses.Pop();
                if (useGeneration)
                {
                    currentHunspell = this.hunspells.Pop();
                    return(currentThes.Lookup(word, currentHunspell));
                }

                return(currentThes.Lookup(word));
            }
            finally
            {
                if (currentThes != null)
                {
                    this.myTheses.Push(currentThes);
                }

                if (currentHunspell != null)
                {
                    this.hunspells.Push(currentHunspell);
                }

                this.myThesSemaphore.Release();
                if (useGeneration)
                {
                    this.hunspellSemaphore.Release();
                }
            }
        }
Ejemplo n.º 5
0
 public Language()
 {
     hunspell = new Hunspell(@"Language\en_us.aff", @"Language\en_us.dic");
     thes = new MyThes(@"Language\th_en_us_v2.dat");
 }
Ejemplo n.º 6
0
 void MyThesPush(MyThes myThes)
 {
     lock (myThesesLock)
     {
         this.myTheses.Push(myThes);
     }
     myThesSemaphore.Release();
 }
Ejemplo n.º 7
0
 void changeThesaurus(bool forceChange)
 {
     if (!File.Exists(Properties.Settings.Default.mythes) || forceChange)
     {
         OpenFileDialog fileDialog = new OpenFileDialog();
         fileDialog.Title = "Please select the file to use as a thesaurus";
         fileDialog.Filter = "MyThes thesaurus file (*.dat)|*.dat";
         fileDialog.FileName = Properties.Settings.Default.mythes;
         fileDialog.ShowDialog();
         Properties.Settings.Default.mythes = fileDialog.FileName;
         Properties.Settings.Default.Save();
     }
     try
     {
         thesaurus = new MyThes(Properties.Settings.Default.mythes);
     }
     catch
     {
         MessageBox.Show("Thesaurus file is invalid!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private static List<string> Synonyms(string word)
        {
            var result = new List<string>();
             var thes = new MyThes(DatFilePath);
             using (var hunspell = new Hunspell(AffFilePath, DictionaryFilePath))
             {
            var stemmedWordResult = hunspell.Stem(word);
            if (stemmedWordResult.Any())
            {
               var stemmedWord = stemmedWordResult.FirstOrDefault();
               if (!string.IsNullOrEmpty(stemmedWord))
               {
                  var thesaurusResult = thes.Lookup(stemmedWord);
                  if (thesaurusResult != null && thesaurusResult.Meanings != null && thesaurusResult.Meanings.Any())
                  {
                     thesaurusResult.Meanings.ForEach(m => m.Synonyms
                        .Where(s => s.ToLower() != stemmedWord.ToLower())
                        .Where(s => s.ToLower() != word.ToLower())
                        .ToList()
                        .ForEach(s => result.Add(s.ToLower()))
                     );
                  }
               }
            }
             }

             return result;
        }