Example #1
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();
        }
Example #2
0
        public static void EncodeRule(string ruleText, PhoneticRule rule)
        {
            for (int i = 0; i < rule.Condition.Length; i++)
            {
                rule.Condition[i] = 0;
            }

            bool group = false;
            bool end   = false;

            char[] memberChars = new char[200];
            int    numMember   = 0;

            foreach (char cond in ruleText)
            {
                switch (cond)
                {
                case '(':
                    group = true;
                    break;

                case ')':
                    end = true;
                    break;

                case '^':
                    rule.BeginningOnly = true;
                    break;

                case '$':
                    rule.EndOnly = true;
                    break;

                case '-':
                    rule.ConsumeCount++;
                    break;

                case '<':
                    rule.ReplaceMode = true;
                    break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    rule.Priority = int.Parse(cond.ToString());
                    break;

                default:
                    if (group)
                    {
                        memberChars[numMember] = cond;
                        numMember++;
                    }
                    else
                    {
                        end = true;
                    }
                    break;
                }

                if (end)
                {
                    if (group)
                    {
                        for (int j = 0; j < numMember; j++)
                        {
                            int charCode = (int)memberChars[j];
                            rule.Condition[charCode] = rule.Condition[charCode] | (1 << rule.ConditionCount);
                        }

                        group     = false;
                        numMember = 0;
                    }
                    else
                    {
                        int charCode = (int)cond;
                        rule.Condition[charCode] = rule.Condition[charCode] | (1 << rule.ConditionCount);
                    }
                    end = false;
                    rule.ConditionCount++;
                }
            }
        }