示例#1
0
        /// <summary>
        ///     Searches all contained word lists for word
        /// </summary>
        /// <param name="word" type="string">
        ///     <para>
        ///         The word to search for
        ///     </para>
        /// </param>
        /// <returns>
        ///     Returns true if word is found
        /// </returns>
        public bool Contains(string word)
        {
            // clean up possible base word list
            _possibleBaseWords.Clear();

            // Step 1 Search UserWords
            if (_userWords.ContainsKey(word))
            {
                System.Diagnostics.Debug.WriteLine("Word Found in User Dictionary: {0}", word);
                return(true);  // word found
            }

            // Step 2 Search BaseWords
            if (_baseWords.ContainsKey(word))
            {
                System.Diagnostics.Debug.WriteLine("Word Found in Base Words: {0}", word);
                return(true); // word found
            }

            // Step 3 Remove suffix, Search BaseWords

            // save suffixed words for use when removing prefix
            List <string> suffixWords = new List <string>();

            // Add word to suffix word list
            suffixWords.Add(word);

            foreach (AffixRule rule in SuffixRules.Values)
            {
                foreach (AffixEntry entry in rule.AffixEntries)
                {
                    string tempWord = AffixUtility.RemoveSuffix(word, entry);
                    if (tempWord != word)
                    {
                        if (_baseWords.ContainsKey(tempWord))
                        {
                            if (this.VerifyAffixKey(tempWord, rule.Name[0]))
                            {
                                System.Diagnostics.Debug.WriteLine(tempWord + " " + rule.Name[0], "Word Found With Base Words: Suffix Key: ");
                                return(true); // word found
                            }
                        }

                        if (rule.AllowCombine)
                        {
                            // saving word to check if it is a word after prefix is removed
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            // saving possible base words for use in generating suggestions
                            _possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }
            // saving possible base words for use in generating suggestions
            _possibleBaseWords.AddRange(suffixWords);

            // Step 4 Remove Prefix, Search BaseWords
            foreach (AffixRule rule in PrefixRules.Values)
            {
                foreach (AffixEntry entry in rule.AffixEntries)
                {
                    foreach (string suffixWord in suffixWords)
                    {
                        string tempWord = AffixUtility.RemovePrefix(suffixWord, entry);
                        if (tempWord != suffixWord)
                        {
                            if (_baseWords.ContainsKey(tempWord))
                            {
                                if (this.VerifyAffixKey(tempWord, rule.Name[0]))
                                {
                                    //System.Diagnostics.Debug.WriteLine("Word Found With Base Words: {0}; Prefix Key: {1}", tempWord, rule.Name[0]);
                                    return(true); // word found
                                }
                            }

                            // saving possible base words for use in generating suggestions
                            _possibleBaseWords.Add(tempWord);
                        }
                    } // suffix word
                }     // prefix rule entry
            }         // prefix rule
            // word not found
            //System.Diagnostics.Debug.WriteLine("Possible Base Words: {0}", _possibleBaseWords.Count);
            return(false);
        }
        /// <summary>
        /// Searches all contained word lists for word.
        /// </summary>
        /// <param name="word" type="string">
        /// The word to search for.
        /// </param>
        /// <returns>
        /// Returns true if word is found.
        /// </returns>
        public ContainsResult Contains(string word)
        {
            word = encode(word);
            // clean up possible base word list
            List <string> possibleBaseWords = new List <string>();

            // Step 2 Search BaseWords
            if (_baseWords.ContainsKey(word))
            {
                return(new ContainsResult(true, null));
            }

            // Step 3 Remove suffix, Search BaseWords

            // save suffixed words for use when removing prefix
            List <string> suffixWords = new List <string>();

            // Add word to suffix word list
            suffixWords.Add(word);

            foreach (AffixRule rule in _suffixRules.Values)
            {
                AffixEntry[] entries    = rule.AffixEntries.ToArray();
                int          entryCount = entries.Length;
                for (int i = 0; i < entryCount; i++)
                {
                    AffixEntry entry    = entries[i];
                    string     tempWord = AffixUtility.RemoveSuffix(word, entry);
                    if (tempWord != null)
                    {
                        if (_baseWords.ContainsKey(tempWord))
                        {
                            if (verifyAffixKey(tempWord, rule.Name[0]))
                            {
                                return(new ContainsResult(true, null));
                            }
                        }

                        if (rule.AllowCombine)
                        {
                            // saving word to check if it is a word after prefix is removed
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            // saving possible base words for use in generating suggestions
                            possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }

            string[] suffixWordsArr = suffixWords.ToArray();
            // saving possible base words for use in generating suggestions
            possibleBaseWords.AddRange(suffixWords);

            // Step 4 Remove Prefix, Search BaseWords
            foreach (AffixRule rule in _prefixRules.Values)
            {
                AffixEntry[] entries    = rule.AffixEntries.ToArray();
                int          entryCount = entries.Length;
                for (int i = 0; i < entryCount; i++)
                {
                    AffixEntry entry            = entries[i];
                    int        suffixWordsCount = suffixWordsArr.Length;
                    for (int j = 0; j < suffixWordsCount; j++)
                    {
                        string tempWord = AffixUtility.RemovePrefix(suffixWordsArr[j], entry);
                        if (tempWord != null)
                        {
                            if (_baseWords.ContainsKey(tempWord))
                            {
                                if (verifyAffixKey(tempWord, rule.Name[0]))
                                {
                                    return(new ContainsResult(true, null));
                                }
                            }

                            possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }

            return(new ContainsResult(false, possibleBaseWords.ToArray()));
        }