Exemplo n.º 1
0
        public void NamesListAddMultiWord()
        {
            // Arrange
            var namesList = new NamesList(Directory.GetCurrentDirectory(), "en", false, null);

            // Act
            namesList.Add("Charlie Parker123");
            var exists = namesList.GetMultiNames().Contains("Charlie Parker123");

            // Assert
            Assert.IsTrue(exists);
        }
Exemplo n.º 2
0
        public SpellCheckWordLists(string dictionaryFolder, string languageName, IDoSpell doSpell)
        {
            if (languageName == null)
            {
                throw new NullReferenceException("languageName");
            }
            if (doSpell == null)
            {
                throw new NullReferenceException("doSpell");
            }

            _languageName = languageName;
            _doSpell      = doSpell;
            _namesList    = new NamesList(Configuration.DictionariesDirectory, languageName, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl);
            _namesEtcList = _namesList.GetNames();
            var namesEtcMultiWordList = _namesList.GetMultiNames();

            foreach (string namesItem in _namesEtcList)
            {
                _namesEtcListUppercase.Add(namesItem.ToUpper());
            }

            if (languageName.StartsWith("en_", StringComparison.OrdinalIgnoreCase))
            {
                foreach (string namesItem in _namesEtcList)
                {
                    if (!namesItem.EndsWith('s'))
                    {
                        _namesEtcListWithApostrophe.Add(namesItem + "'s");
                        _namesEtcListWithApostrophe.Add(namesItem + "’s");
                    }
                    else if (!namesItem.EndsWith('\''))
                    {
                        _namesEtcListWithApostrophe.Add(namesItem + "'");
                    }
                }
            }

            if (File.Exists(dictionaryFolder + languageName + "_user.xml"))
            {
                var userWordDictionary = new XmlDocument();
                userWordDictionary.Load(dictionaryFolder + languageName + "_user.xml");
                if (userWordDictionary.DocumentElement != null)
                {
                    var xmlNodeList = userWordDictionary.DocumentElement.SelectNodes("word");
                    if (xmlNodeList != null)
                    {
                        foreach (XmlNode node in xmlNodeList)
                        {
                            string word = node.InnerText.Trim().ToLower();
                            if (word.Contains(' '))
                            {
                                _userPhraseList.Add(word);
                            }
                            else
                            {
                                _userWordList.Add(word);
                            }
                        }
                    }
                }
            }
            // Add names/userdic with "." or " " or "-"
            foreach (var word in namesEtcMultiWordList)
            {
                if (word.Contains(PeriodAndDash))
                {
                    _wordsWithDashesOrPeriods.Add(word);
                }
            }
            foreach (string name in _namesEtcList)
            {
                if (name.Contains(PeriodAndDash))
                {
                    _wordsWithDashesOrPeriods.Add(name);
                }
            }
            foreach (string word in _userWordList)
            {
                if (word.Contains(PeriodAndDash))
                {
                    _wordsWithDashesOrPeriods.Add(word);
                }
            }
            foreach (var phrase in _userPhraseList)
            {
                if (phrase.Contains(PeriodAndDash))
                {
                    _wordsWithDashesOrPeriods.Add(phrase);
                }
            }
        }
        private void LoadSpellingDictionariesViaDictionaryFileName(string threeLetterIsoLanguageName, CultureInfo culture, string dictionaryFileName, bool resetSkipList)
        {
            fiveLetterWordListLanguageName = Path.GetFileNameWithoutExtension(dictionaryFileName);
            if (fiveLetterWordListLanguageName != null && fiveLetterWordListLanguageName.Length > 5)
            {
                fiveLetterWordListLanguageName = fiveLetterWordListLanguageName.Substring(0, 5);
            }

            string dictionary = Utilities.DictionaryFolder + fiveLetterWordListLanguageName;
            if (resetSkipList)
            {
                wordSkipList = new HashSet<string> { Configuration.Settings.Tools.MusicSymbol, "*", "%", "#", "+", "$" };
            }

            // Load names etc list (names/noise words)
            namesList = new NamesList(Configuration.DictionariesFolder, fiveLetterWordListLanguageName, Configuration.Settings.WordLists.UseOnlineNamesEtc, Configuration.Settings.WordLists.NamesEtcUrl);
            namesEtcList = namesList.GetNames();
            namesEtcMultiWordList = namesList.GetMultiNames();
            namesEtcListUppercase = new HashSet<string>();
            foreach (string name in namesEtcList)
            {
                namesEtcListUppercase.Add(name.ToUpper());
            }

            namesEtcListWithApostrophe = new HashSet<string>();
            if (threeLetterIsoLanguageName.Equals("eng", StringComparison.OrdinalIgnoreCase))
            {
                foreach (string namesItem in namesEtcList)
                {
                    if (!namesItem.EndsWith('s'))
                    {
                        namesEtcListWithApostrophe.Add(namesItem + "'s");
                    }
                    else
                    {
                        namesEtcListWithApostrophe.Add(namesItem + "'");
                    }
                }
            }

            // Load user words
            userWordList = new HashSet<string>();
            userWordListXmlFileName = Utilities.LoadUserWordList(userWordList, fiveLetterWordListLanguageName);

            // Find abbreviations
            abbreviationList = new HashSet<string>();
            foreach (string name in namesEtcList.Where(name => name.EndsWith('.')))
            {
                abbreviationList.Add(name);
            }

            if (threeLetterIsoLanguageName.Equals("eng", StringComparison.OrdinalIgnoreCase))
            {
                if (!abbreviationList.Contains("a.m."))
                {
                    abbreviationList.Add("a.m.");
                }

                if (!abbreviationList.Contains("p.m."))
                {
                    abbreviationList.Add("p.m.");
                }

                if (!abbreviationList.Contains("o.r."))
                {
                    abbreviationList.Add("o.r.");
                }
            }

            foreach (string name in userWordList.Where(name => name.EndsWith('.')))
            {
                abbreviationList.Add(name);
            }

            // Load Hunspell spell checker
            try
            {
                if (!File.Exists(dictionary + ".dic"))
                {
                    var fileMatches = Directory.GetFiles(Utilities.DictionaryFolder, fiveLetterWordListLanguageName + "*.dic");
                    if (fileMatches.Length > 0)
                    {
                        dictionary = fileMatches[0].Substring(0, fileMatches[0].Length - 4);
                    }
                }

                if (hunspell != null)
                {
                    hunspell.Dispose();
                }

                hunspell = Hunspell.GetHunspell(dictionary);
                IsDictionaryLoaded = true;
                spellCheckDictionaryName = dictionary;
                DictionaryCulture = culture;
            }
            catch
            {
                IsDictionaryLoaded = false;
            }
        }