Пример #1
0
 public void Start()
 {
     string dictionaryPath = Hunspell.NativeDllPath;
     spellEngine = new SpellEngine();
     LanguageConfig enConfig = new LanguageConfig();
     enConfig.LanguageCode = "en";
     enConfig.HunspellAffFile = Path.Combine(dictionaryPath, "en_us.aff");
     enConfig.HunspellDictFile = Path.Combine(dictionaryPath, "en_us.dic");
     enConfig.HunspellKey = "";
     enConfig.HyphenDictFile = Path.Combine(dictionaryPath, "hyph_en_us.dic");
     enConfig.MyThesDatFile = Path.Combine(dictionaryPath, "th_en_us_new.dat");
     spellEngine.AddLanguage(enConfig);
 }
        protected void Application_Start(object sender, EventArgs e)
        {
            string dictionaryPath = Hunspell.NativeDllPath;

            spellEngine = new SpellEngine();
            var enConfig = new LanguageConfig();
            enConfig.LanguageCode = "en";
            enConfig.HunspellAffFile = Path.Combine(dictionaryPath, "en_us.aff");
            enConfig.HunspellDictFile = Path.Combine(dictionaryPath, "en_us.dic");
            enConfig.HunspellKey = "";
            enConfig.HyphenDictFile = Path.Combine(dictionaryPath, "hyph_en_us.dic");
            enConfig.MyThesDatFile = Path.Combine(dictionaryPath, "th_en_us_new.dat");
            spellEngine.AddLanguage(enConfig);
        }
        public ThreadSafeCachedSpellCheckerService()
        {
            this.disposing = false;

            this.spellEngine = new SpellEngine();

            var languageConfig = new LanguageConfig
                                 {
                                     LanguageCode = LanguageCode,
                                     HunspellAffFile = Path.Combine(HunspellDictionariesFolder, HunspellAffFile),
                                     HunspellDictFile = Path.Combine(HunspellDictionariesFolder, HunspellDictFile)
                                 };
            this.spellEngine.AddLanguage(languageConfig);

            this.spellCache = new ConcurrentDictionary<string, SpellCheckResponse>();
            this.nonAlphaNumericRegex = new Regex("[^a-zA-Z ]");
        }
        //=====================================================================
        /// <summary>
        /// Create a global dictionary for the specified culture
        /// </summary>
        /// <param name="culture">The language to use for the dictionary.</param>
        /// <param name="serviceProvider">A service provider used to interact with the solution/project</param>
        /// <param name="additionalDictionaryFolders">An enumerable list of additional folders to search for
        /// other dictionaries.</param>
        /// <param name="recognizedWords">An optional list of recognized words that will be added to the
        /// dictionary (i.e. from a code analysis dictionary).</param>
        /// <returns>The global dictionary to use or null if one could not be created.</returns>
        public static GlobalDictionary CreateGlobalDictionary(CultureInfo culture, IServiceProvider serviceProvider,
            IEnumerable<string> additionalDictionaryFolders, IEnumerable<string> recognizedWords)
        {
            GlobalDictionary globalDictionary = null;
            string affixFile, dictionaryFile, userWordsFile;

            try
            {
                // The configuration editor should disallow creating a configuration without at least one
                // language but if someone edits the file manually, they could remove them all.  If that
                // happens, just use the English-US dictionary.
                if(culture == null)
                    culture = new CultureInfo("en-US");

                if(globalDictionaries == null)
                    globalDictionaries = new Dictionary<string, GlobalDictionary>();

                // If not already loaded, create the dictionary and the thread-safe spell factory instance for
                // the given culture.
                if(!globalDictionaries.ContainsKey(culture.Name))
                {
                    string dllPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                        "NHunspell");

                    if(spellEngine == null)
                    {
                        Hunspell.NativeDllPath = dllPath;
                        spellEngine = new SpellEngine();
                    }

                    // Look for all available dictionaries and get the one for the requested culture
                    var dictionaries = SpellCheckerDictionary.AvailableDictionaries(additionalDictionaryFolders);
                    SpellCheckerDictionary userDictionary;

                    if(dictionaries.TryGetValue(culture.Name, out userDictionary))
                    {
                        affixFile = userDictionary.AffixFilePath;
                        dictionaryFile = userDictionary.DictionaryFilePath;
                        userWordsFile = userDictionary.UserDictionaryFilePath;
                    }
                    else
                        affixFile = dictionaryFile = userWordsFile = null;

                    // If not found, default to the US English dictionary supplied with the package.  This can at
                    // least clue us in that it didn't find the language-specific dictionary when the suggestions
                    // are in US English.
                    if(affixFile == null || dictionaryFile == null || !File.Exists(affixFile) ||
                      !File.Exists(dictionaryFile))
                    {
                        affixFile = Path.Combine(dllPath, "en_US.aff");
                        dictionaryFile = Path.ChangeExtension(affixFile, ".dic");
                        userWordsFile = Path.Combine(SpellingConfigurationFile.GlobalConfigurationFilePath,
                            "en-US_User.dic");
                    }

                    spellEngine.AddLanguage(new LanguageConfig
                    {
                        LanguageCode = culture.Name,
                        HunspellAffFile = affixFile,
                        HunspellDictFile = dictionaryFile
                    });

                    globalDictionaries.Add(culture.Name, new GlobalDictionary(culture, spellEngine[culture.Name],
                        dictionaryFile, userWordsFile, serviceProvider));
                }

                globalDictionary = globalDictionaries[culture.Name];

                // Add recognized words that are not already there
                globalDictionary.AddRecognizedWords(recognizedWords);
            }
            catch(Exception ex)
            {
                // Ignore exceptions.  Not much we can do, we just won't spell check anything in this language.
                System.Diagnostics.Debug.WriteLine(ex);
            }

            return globalDictionary;
        }
        /// <summary>
        /// This is called to clear the dictionary cache and dispose of the spelling engine
        /// </summary>
        /// <remarks>This is done whenever a change in solution is detected.  This allows for solution and
        /// project-specific dictionaries to override global dictionaries that may have been in use in a previous
        /// solution.</remarks>
        public static void ClearDictionaryCache()
        {
            if(globalDictionaries != null)
            {
                foreach(var gd in globalDictionaries.Values)
                    gd.Dispose();

                globalDictionaries.Clear();
            }

            if(spellEngine != null)
            {
                spellEngine.Dispose();
                spellEngine = null;
            }
        }
Пример #6
0
        //=====================================================================

        /// <summary>
        /// Create a global dictionary for the specified culture
        /// </summary>
        /// <param name="culture">The language to use for the dictionary</param>
        /// <returns>The spell factory to use or null if one could not be created</returns>
        public static GlobalDictionary CreateGlobalDictionary(CultureInfo culture)
        {
            GlobalDictionary globalDictionary = null;

            try
            {
                if(globalDictionaries == null)
                    globalDictionaries = new Dictionary<string, GlobalDictionary>();

                // If no culture is specified, use the default culture
                if(culture == null)
                    culture = SpellCheckerConfiguration.DefaultLanguage;

                // If not already loaded, create the dictionary and the thread-safe spell factory instance for
                // the given culture.
                if(!globalDictionaries.ContainsKey(culture.Name))
                {
                    string dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                    if(spellEngine == null)
                    {
                        Hunspell.NativeDllPath = dllPath;
                        spellEngine = new SpellEngine();
                    }

                    // Look in the configuration folder first for user-supplied dictionaries
                    string dictionaryFile = Path.Combine(SpellCheckerConfiguration.ConfigurationFilePath,
                        culture.Name.Replace("-", "_") + ".aff");

                    // If not found, default to the English dictionary supplied with the package.  This can at
                    // least clue us in that it didn't find the language-specific dictionary when the suggestions
                    // are in English.
                    if(!File.Exists(dictionaryFile))
                        dictionaryFile = Path.Combine(dllPath, "Spelling", "en_US.aff");

                    LanguageConfig lc = new LanguageConfig();
                    lc.LanguageCode = culture.Name;
                    lc.HunspellAffFile = dictionaryFile;
                    lc.HunspellDictFile = Path.ChangeExtension(dictionaryFile, ".dic");

                    spellEngine.AddLanguage(lc);

                    globalDictionaries.Add(culture.Name, new GlobalDictionary(culture,
                        spellEngine[culture.Name]));
                }

                globalDictionary = globalDictionaries[culture.Name];
            }
            catch(Exception ex)
            {
                // Ignore exceptions.  Not much we can do, we'll just not spell check anything.
                System.Diagnostics.Debug.WriteLine(ex);
            }

            return globalDictionary;
        }
        public HunspellSpellingPlugin()
        {
            // Set up the spell engine for multi-threaded access.
            SpellEngine = new SpellEngine();

            // Assume we are disabled unless we can configure it properly.
            projectPlugin = new DisabledSpellingProjectPlugin();

            // Figure out the paths to the dictionary files. For the time being,
            // we're going to assume we're using U.S. English.
            string affixFilename;
            string dictionaryFilename;

            if (!GetDictionaryPaths("en_US", out affixFilename, out dictionaryFilename))
            {
                // We couldn't get the dictionary paths, so just stick with the
                // disabled spelling plugin.
                return;
            }

            // Attempt to load the NHunspell plugin. This is a high-quality
            // plugin based on Managed C++. This works nicely in Windows, but
            // has no support for Linux.
            try
            {
                // Attempt to load the U.S. English language. This will throw
                // an exception if it cannot load. If it does, then we use it.
                var englishUnitedStates = new LanguageConfig
                {
                    LanguageCode = "en_US",
                    HunspellAffFile = affixFilename,
                    HunspellDictFile = dictionaryFilename,
                    HunspellKey = string.Empty
                };

                SpellEngine.AddLanguage(englishUnitedStates);

                // If we got this far, set the project plugin to the one that
                // uses the SpellEngine from NHunspell.
                projectPlugin = new SpellEngineSpellingProjectPlugin(this);
                return;
            }
            catch (Exception exception)
            {
                // Report that we can't load the first attempt.
                Console.WriteLine("Cannot load NHunspell: " + exception);
            }

            // If we got this far, we couldn't load the NHunspell plugin.
            // Attempt to load in the PInvoke implementation instead.
            try
            {
                // Create a new Hunspell P/Invoke loader.
                var pinvokePlugin = new PInvokeSpellingProjectPlugin(
                    affixFilename, dictionaryFilename);
                projectPlugin = pinvokePlugin;
            }
            catch (Exception exception)
            {
                // Report that we can't load the first attempt.
                Console.WriteLine("Cannot load NHunspell via P/Invoke: " + exception);
            }
        }
 protected void Application_End(object sender, EventArgs e)
 {
     if (spellEngine != null)
         spellEngine.Dispose();
     spellEngine = null;
 }
Пример #9
0
        public List<string> NhunSpellThesaurus(string queryText)
        {
            string dictionaryPath = Hunspell.NativeDllPath;
            spellEngine = new SpellEngine();
            LanguageConfig enConfig = new LanguageConfig();
            enConfig.LanguageCode = "en";
            enConfig.HunspellAffFile = Path.Combine(dictionaryPath, "en_us.aff");
            enConfig.HunspellDictFile = Path.Combine(dictionaryPath, "en_us.dic");
            enConfig.HunspellKey = "";
            enConfig.HyphenDictFile = Path.Combine(dictionaryPath, "hyph_en_us.dic");
            enConfig.MyThesDatFile = Path.Combine(dictionaryPath, "th_en_us_new.dat");
            spellEngine.AddLanguage(enConfig);
            List<string> myThes = new List<string>();
            bool correct = SpellEngine["en"].Spell(queryText);

            if (correct)
            {

                ThesResult meanings = SpellEngine["en"].LookupSynonyms(queryText, true);
                if (meanings != null)
                {
                    foreach (ThesMeaning meaning in meanings.Meanings)
                    {
                        foreach (string synonym in meaning.Synonyms)
                        {
                            myThes.Add(Server.HtmlEncode(synonym));
                        }
                    }
                }
            }
            return myThes;
        }
Пример #10
0
        public Result GetSuggestions(string input)
        {
            string dictionaryPath = Hunspell.NativeDllPath;
            spellEngine = new SpellEngine();
            LanguageConfig enConfig = new LanguageConfig();
            enConfig.LanguageCode = "en";
            enConfig.HunspellAffFile = Path.Combine(dictionaryPath, "en_us.aff");
            enConfig.HunspellDictFile = Path.Combine(dictionaryPath, "en_us.dic");
            enConfig.HunspellKey = "";
            enConfig.HyphenDictFile = Path.Combine(dictionaryPath, "hyph_en_us.dic");
            enConfig.MyThesDatFile = Path.Combine(dictionaryPath, "th_en_us_new.dat");
            spellEngine.AddLanguage(enConfig);
            Result objResult = new Result();

            bool correct1;
            using (var hunspell = new Hunspell(enConfig.HunspellAffFile, enConfig.HunspellDictFile))
            {

                string[] lines = System.IO.File.ReadAllLines(@ConfigurationManager.AppSettings["FilePath"].ToString());

                foreach (var line in lines)
                {
                    hunspell.Add(line.ToLower());
                }
                correct1 = hunspell.Spell(input.ToLower());
            }
            bool correct = false;
            try
            {
                correct = SpellEngine["en"].Spell(input);

                if (correct = correct1 == false)
                {
                    objResult.Suggestions = SpellEngine["en"].Suggest(input);
                    objResult.State = "false";
                }
                else
                {
                    objResult.State = "true";

                }
            }
            catch (Exception ex)
            {

            }
            return objResult;
        }