Exemplo n.º 1
0
        private bool NotInBaseWords(string word)
        {
            List <string> suffixWords = new List <string>();

            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]))
                            {
                                return(true);
                            }
                        }

                        if (rule.AllowCombine)
                        {
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            _possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }
            _possibleBaseWords.AddRange(suffixWords);

            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]))
                                {
                                    return(true);
                                }
                            }

                            _possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public List <string> ExpandWord(Word word)
        {
            List <string> suffixWords = new List <string>();
            List <string> words       = new List <string>();

            suffixWords.Add(word.Text);
            string prefixKeys = "";

            foreach (char key in word.AffixKeys)
            {
                if (_suffixRules.ContainsKey(key.ToString()))
                {
                    AffixRule rule     = _suffixRules[key.ToString()];
                    string    tempWord = AffixUtility.AddSuffix(word.Text, rule);
                    if (tempWord != word.Text)
                    {
                        if (rule.AllowCombine)
                        {
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            words.Add(tempWord);
                        }
                    }
                }
                else if (_prefixRules.ContainsKey(key.ToString()))
                {
                    prefixKeys += key.ToString();
                }
            }

            foreach (char key in prefixKeys)
            {
                AffixRule rule = _prefixRules[key.ToString()];

                foreach (string suffixWord in suffixWords)
                {
                    string tempWord = AffixUtility.AddPrefix(suffixWord, rule);
                    if (tempWord != suffixWord)
                    {
                        words.Add(tempWord);
                    }
                }
            }

            words.AddRange(suffixWords);

            return(words);
        }
Exemplo n.º 3
0
        public void Load(Stream dictionary)
        {
            StreamReader    sr = new StreamReader(dictionary);
            string          tempLine;
            Regex           _spaceRegx = new Regex(@"[^\s]+", RegexOptions.None);
            MatchCollection partMatches;
            string          currentSection = "";
            AffixRule       currentRule    = null;

            while (sr.Peek() >= 0)
            {
                tempLine = sr.ReadLine().Trim();

                if (tempLine.Length > 0)
                {
                    switch (tempLine)
                    {
                    case "[Copyright]":
                    case "[Try]":
                    case "[Replace]":
                    case "[Prefix]":
                    case "[Suffix]":
                    case "[Phonetic]":
                    case "[Words]":
                        currentSection = tempLine;
                        break;

                    default:
                        switch (currentSection)
                        {
                        case "[Copyright]":
                            break;

                        case "[Try]":
                            this.TryCharacters += tempLine;
                            break;

                        case "[Replace]":
                            this.ReplaceCharacters.Add(tempLine);
                            break;

                        case "[Prefix]":
                        case "[Suffix]":
                            partMatches = _spaceRegx.Matches(tempLine);

                            if (partMatches.Count == 3)
                            {
                                currentRule = new AffixRule();

                                currentRule.Name = partMatches[0].Value;
                                if (partMatches[1].Value == "Y")
                                {
                                    currentRule.AllowCombine = true;
                                }

                                if (currentSection == "[Prefix]")
                                {
                                    this.PrefixRules.Add(currentRule.Name, currentRule);
                                }
                                else
                                {
                                    this.SuffixRules.Add(currentRule.Name, currentRule);
                                }
                            }
                            else if (partMatches.Count == 4)
                            {
                                if (currentRule.Name == partMatches[0].Value)
                                {
                                    AffixEntry entry = new AffixEntry();

                                    if (partMatches[1].Value != "0")
                                    {
                                        entry.StripCharacters = partMatches[1].Value;
                                    }
                                    entry.AddCharacters = partMatches[2].Value;
                                    AffixUtility.EncodeConditions(partMatches[3].Value, entry);

                                    currentRule.AffixEntries.Add(entry);
                                }
                            }
                            break;

                        case "[Phonetic]":
                            partMatches = _spaceRegx.Matches(tempLine);
                            if (partMatches.Count >= 2)
                            {
                                PhoneticRule rule = new PhoneticRule();
                                PhoneticUtility.EncodeRule(partMatches[0].Value, rule);
                                rule.ReplaceString = partMatches[1].Value;
                                _phoneticRules.Add(rule);
                            }
                            break;

                        case "[Words]":
                            string[] parts    = tempLine.Split('/');
                            Word     tempWord = new Word();
                            tempWord.Text = parts[0];
                            if (parts.Length >= 2)
                            {
                                tempWord.AffixKeys = parts[1];
                            }
                            if (parts.Length >= 3)
                            {
                                tempWord.PhoneticCode = parts[2];
                            }

                            this.BaseWords.Add(tempWord.Text, tempWord);
                            break;
                        }
                        break;
                    }
                }
            }

            sr.Close();
        }