Ejemplo n.º 1
0
        protected CyrResult Decline(string Phrase, GetConditionsEnum Condition, NumbersEnum Number)
        {
            if (Phrase.IsNullOrEmpty())
            {
                return(new CyrResult());
            }

            List <object> words = new List <object>();

            string[]         parts   = Phrase.Split(' ').Select(val => val.Trim()).Where(val => val.IsNotNullOrEmpty()).ToArray();
            List <CyrResult> results = new List <CyrResult>();

            foreach (string w in parts)
            {
                SpeechPartsEnum speech = this.DetermineSpeechPart(w);

                switch (speech)
                {
                case SpeechPartsEnum.Adjective:
                    CyrAdjective adj = this.adjCollection.Get(w, Condition);
                    words.Add(adj);
                    break;

                case SpeechPartsEnum.Noun:
                    CyrNoun noun = this.nounCollection.Get(w, Condition);
                    words.Add(noun);
                    break;

                default:
                    throw new ArgumentException("This speech part is not supported yet. Speech part: " + speech.ToString());
                }
            }

            for (int i = 0; i < words.Count; i++)
            {
                CyrNoun noun = words[i] as CyrNoun;

                if (noun != null)
                {
                    if (Number == NumbersEnum.Plural)
                    {
                        results.Add(noun.DeclinePlural());
                    }
                    else
                    {
                        results.Add(noun.Decline());
                    }

                    continue;
                }

                CyrAdjective adj = words[i] as CyrAdjective;

                noun = this.GetNextPreviousNoun(words, i);

                if (Number == NumbersEnum.Plural)
                {
                    if (noun != null)
                    {
                        results.Add(adj.DeclinePlural(noun.Animate));
                    }
                    else
                    {
                        results.Add(adj.DeclinePlural(AnimatesEnum.Animated));
                    }
                }
                else
                {
                    if (noun != null)
                    {
                        results.Add(adj.Decline(noun.Animate));
                    }
                    else
                    {
                        results.Add(adj.Decline(AnimatesEnum.Animated));
                    }
                }
            }

            CyrResult result = results.First();

            for (int i = 1; i < results.Count; i++)
            {
                result = result + results[i];
            }

            return(result);
        }
Ejemplo n.º 2
0
        protected virtual void AddWordToDictionary
        (
            string line,
            List <KeyValuePair <DictionaryKey, CyrAdjective> > adjectives,
            List <string>[] masculineWordCandidates,
            List <string>[] feminineWordCandidates,
            List <string>[] neuterWordCandidates,
            List <string>[] pluralWordCandidates
        )
        {
            string[] parts = line.Split(' ');

            int ruleIndex = int.Parse(parts[1]);

            CyrRule[]    rules     = this.rules[ruleIndex];
            CyrAdjective adjective = new CyrAdjective(parts[0], rules);

            // Женский и средний род склоняются одинаково для одушевленных и неодушевленных предметов.
            {
                CyrResult result = adjective.Decline(GendersEnum.Feminine, AnimatesEnum.Animated);

                foreach (CasesEnum @case in cases)
                {
                    feminineWordCandidates[(int)@case - 1].Add(result[(int)@case]);

                    foreach (AnimatesEnum animate in this.animates)
                    {
                        DictionaryKey key = new DictionaryKey(result[(int)@case], GendersEnum.Feminine, @case, NumbersEnum.Singular, animate);
                        adjectives.Add(new KeyValuePair <DictionaryKey, CyrAdjective>(key, adjective));
                    }
                }
            }
            {
                CyrResult result = adjective.Decline(GendersEnum.Neuter, AnimatesEnum.Animated);

                foreach (CasesEnum @case in cases)
                {
                    neuterWordCandidates[(int)@case - 1].Add(result[(int)@case]);

                    foreach (AnimatesEnum animate in this.animates)
                    {
                        DictionaryKey key = new DictionaryKey(result[(int)@case], GendersEnum.Neuter, @case, NumbersEnum.Singular, animate);
                        adjectives.Add(new KeyValuePair <DictionaryKey, CyrAdjective>(key, adjective));
                    }
                }
            }

            // Мужской род и множественное число склоняются по-разному для одушевленных и неодушевленных предметов.
            foreach (AnimatesEnum animate in animates)
            {
                CyrResult result = adjective.Decline(GendersEnum.Masculine, animate);

                foreach (CasesEnum @case in cases)
                {
                    DictionaryKey key = new DictionaryKey(result[(int)@case], GendersEnum.Masculine, @case, NumbersEnum.Singular, animate);
                    adjectives.Add(new KeyValuePair <DictionaryKey, CyrAdjective>(key, adjective));
                    masculineWordCandidates[(int)@case - 1].Add(key.Name);
                }

                result = adjective.DeclinePlural(animate);

                foreach (CasesEnum @case in cases)
                {
                    DictionaryKey key = new DictionaryKey(result[(int)@case], 0, @case, NumbersEnum.Plural, animate);
                    adjectives.Add(new KeyValuePair <DictionaryKey, CyrAdjective>(key, adjective));
                    pluralWordCandidates[(int)@case - 1].Add(key.Name);
                }
            }
        }