コード例 #1
0
ファイル: WordDictionary.cs プロジェクト: cdc-dpbrown/EWAV
        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);
        }
コード例 #2
0
ファイル: WordDictionary.cs プロジェクト: cdc-dpbrown/EWAV
        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();
        }