コード例 #1
0
        /// <summary>
        /// Creates a new instance of <see cref="HunspellCulture"/> from the given dictionary file name.
        /// </summary>
        /// <param name="fileName">The name of the dictionary file.</param>
        /// <returns>An instance to a <see cref="HunspellCulture"/> class.</returns>
        public static HunspellData FromDictionaryFile(string fileName)
        {
            HunspellData result = new HunspellData();

            // the files are excepted to be in format i.e. "en_US.dic"..
            string cultureName = Regex.Match(Path.GetFileName(fileName), "\\D{2}(_|-)\\D{2}").Value;

            cultureName = cultureName.Replace('_', '-');

            // get a CultureInfo value for the Hunspell dictionary file..
            result.HunspellCulture = CultureInfo.GetCultureInfo(cultureName);

            // set the file..
            result.DictionaryFile = fileName;

            // set the affix file..
            result.AffixFile = Path.ChangeExtension(fileName, "aff");

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Crawls the directory containing Hunspell dictionary and affix files.
        /// </summary>
        /// <param name="path">The path to search the dictionaries from.</param>
        /// <returns>List&lt;HunspellData&gt;.</returns>
        public static List <HunspellData> CrawlDirectory(string path)
        {
            // create a new instance of the DirectoryCrawler class by user given "arguments"..
            DirectoryCrawler crawler = new DirectoryCrawler(path, DirectoryCrawler.SearchTypeMatch.Regex,
                                                            "*.dic", true);

            // search for the Hunspell dictionary files (*.dic)..
            var files = crawler.GetCrawlResult();

            // initialize a return value..
            List <HunspellData> result = new List <HunspellData>();

            // loop through the found dictionary files (*.dic)..
            foreach (var file in files)
            {
                try
                {
                    // create a new HunspellData class instance..
                    var data = HunspellData.FromDictionaryFile(file);

                    // validate that there is a affix (*.aff) pair for the found dictionary file (*.dic)..
                    if (!File.Exists(data.DictionaryFile) || !File.Exists(data.AffixFile))
                    {
                        // ..if not, do continue..
                        continue;
                    }

                    // the validation was successful, so add the data to the result..
                    result.Add(data);
                }
                catch (Exception ex)
                {
                    // log the exception..
                    ExceptionLogAction?.Invoke(ex);
                }
            }

            // return the result..
            return(result);
        }