Beispiel #1
0
        /// <summary>
        /// Добавляет слово в <see cref="words"/> словарь.
        /// </summary>
        /// <param name="line">
        /// Строка со словом в формате словаря.
        /// Смотри /Cyriller/App_Data/nouns.txt.
        /// </param>
        protected virtual void AddWordToTheCollection(string line, List <string>[] singularWordCandidates, List <string>[] pluralWordCandidates)
        {
            string[] parts = line.Split(' ');

            CyrNounCollectionRow row = CyrNounCollectionRow.Parse(parts[1]);

            CyrRule[] rules = this.rules[row.RuleID];
            CyrNoun   noun  = new CyrNoun(parts[0], (GendersEnum)row.GenderID, (AnimatesEnum)row.AnimateID, (WordTypesEnum)row.TypeID, rules);

            CyrResult singular = noun.Decline();
            CyrResult plural   = noun.DeclinePlural();

            foreach (CasesEnum @case in this.cases)
            {
                if (!string.IsNullOrEmpty(singular.Get(@case)))
                {
                    DictionaryKey key = new DictionaryKey(singular.Get(@case), noun.Gender, @case, NumbersEnum.Singular);
                    singularWordCandidates[(int)@case - 1].Add(singular.Get(@case));
                    this.words[key] = noun;
                }

                if (!string.IsNullOrEmpty(plural.Get(@case)))
                {
                    DictionaryKey key = new DictionaryKey(plural.Get(@case), noun.Gender, @case, NumbersEnum.Plural);
                    pluralWordCandidates[(int)@case - 1].Add(plural.Get(@case));
                    this.words[key] = noun;
                }
            }
        }
        public static CyrNounCollectionRow Parse(string value)
        {
            string[] parts = value.Split(',');

            if (parts.Length != 4)
            {
                throw new ArgumentException($"{value} is not a valid representation of {nameof(CyrNounCollectionRow)}!");
            }

            CyrNounCollectionRow row = new CyrNounCollectionRow();

            row.GenderID  = int.Parse(parts[0]);
            row.AnimateID = int.Parse(parts[1]);
            row.TypeID    = int.Parse(parts[2]);
            row.RuleID    = int.Parse(parts[3]);

            return(row);
        }
Beispiel #3
0
        public CyrNoun Get(string Word, GetConditionsEnum Condition, GendersEnum?GenderID = null, AnimatesEnum?AnimateID = null, WordTypesEnum?TypeID = null)
        {
            string        t    = Word;
            List <string> list = this.GetDetails(t);

            if (list == null || !list.Any())
            {
                t    = Word.ToLower();
                list = this.GetDetails(t);
            }

            if (list == null || !list.Any())
            {
                t    = Word.ToLower().UppercaseFirst();
                list = this.GetDetails(t);
            }

            if (list == null || !list.Any())
            {
                List <int> indexes = new List <int>();
                string     lower   = Word.ToLower();

                for (int i = 0; i < lower.Length; i++)
                {
                    if (lower[i] == 'е')
                    {
                        indexes.Add(i);
                    }
                }

                foreach (int index in indexes)
                {
                    t    = lower.Substring(0, index) + "ё" + lower.Substring(index + 1);
                    list = this.GetDetails(t);

                    if (list != null && list.Any())
                    {
                        break;
                    }
                }
            }

            if ((list == null || !list.Any()) && Condition == GetConditionsEnum.Similar)
            {
                list = this.GetSimilarDetails(Word, out t);
            }

            if (list == null || !list.Any())
            {
                throw new CyrWordNotFoundException(Word);
            }

            IEnumerable <CyrNounCollectionRow> rows   = list.Select(x => CyrNounCollectionRow.Parse(x));
            IEnumerable <CyrNounCollectionRow> filter = rows;

            if (GenderID.HasValue)
            {
                filter = filter.Where(x => x.GenderID == (int)GenderID);
            }

            if (AnimateID.HasValue)
            {
                filter = filter.Where(x => x.AnimateID == (int)AnimateID);
            }

            if (TypeID.HasValue)
            {
                filter = filter.Where(x => x.TypeID == (int)TypeID);
            }

            CyrNounCollectionRow row = filter.FirstOrDefault();

            if (row == null && Condition == GetConditionsEnum.Similar)
            {
                row = rows.FirstOrDefault();
            }

            if (row == null)
            {
                throw new CyrWordNotFoundException(Word);
            }

            string[] parts = this.rules[row.RuleID].Split(new char[] { ',', '|' });

            CyrRule[] rules = parts.Select(val => new CyrRule(val)).ToArray();
            CyrNoun   noun  = new CyrNoun(Word, t, (GendersEnum)row.GenderID, (AnimatesEnum)row.AnimateID, (WordTypesEnum)row.TypeID, rules);

            return(noun);
        }