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 ]");
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpellFactory"/> class.
        /// </summary>
        /// <param name="config">
        /// The configuration of the language. 
        /// </param>
        public SpellFactory(LanguageConfig config)
        {
            this.processors = config.Processors;

            if (config.HunspellAffFile != null && config.HunspellAffFile != string.Empty)
            {
                this.hunspells = new Stack<Hunspell>(this.processors);
                for (int count = 0; count < this.processors; ++count)
                {
                    if (config.HunspellKey != null && config.HunspellKey != string.Empty)
                    {
                        this.hunspells.Push(new Hunspell(config.HunspellAffFile, config.HunspellDictFile, config.HunspellKey));
                    }
                    else
                    {
                        this.hunspells.Push(new Hunspell(config.HunspellAffFile, config.HunspellDictFile));
                    }
                }
            }

            if (config.HyphenDictFile != null && config.HyphenDictFile != string.Empty)
            {
                this.hyphens = new Stack<Hyphen>(this.processors);
                for (int count = 0; count < this.processors; ++count)
                {
                    this.hyphens.Push(new Hyphen(config.HyphenDictFile));
                }
            }

            if (config.MyThesIdxFile != null && config.MyThesIdxFile != string.Empty)
            {
                this.myTheses = new Stack<MyThes>(this.processors);
                for (int count = 0; count < this.processors; ++count)
                {
                    this.myTheses.Push(new MyThes(config.MyThesDatFile));
                }
            }

            this.hunspellSemaphore = new Semaphore(this.processors, this.processors);
            this.hyphenSemaphore = new Semaphore(this.processors, this.processors);
            this.myThesSemaphore = new Semaphore(this.processors, this.processors);
        }
Example #5
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;
        }
Example #6
0
        /// <summary>
        /// Adds the language.
        /// </summary>
        /// <param name="config">
        /// The language configuration. 
        /// </param>
        public void AddLanguage(LanguageConfig config)
        {
            string languageCode = config.LanguageCode;
            languageCode = languageCode.ToLower();
            if (config.Processors < 1)
            {
                config.Processors = this.Processors;
            }

            var factory = new SpellFactory(config);

            lock (this.dictionaryLock) this.languages.Add(languageCode, factory);
        }
        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);
            }
        }
        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;
        }
        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;
        }