/// <summary> /// Поиск существительного по неточному совпадению с автоматическим определением рода, падежа и числа. /// Возвращает null если слово не было найдено. /// </summary> /// <param name="word">Существительное в любом роде, числе и падеже.</param> /// <param name="foundWord">Существительное, найденное в словаре.</param> /// <param name="number">Число найденного существительного.</param> /// <param name="case">Падеж найденного существительного.</param> /// <returns></returns> public CyrNoun GetOrDefault(string word, out string foundWord, out CasesEnum @case, out NumbersEnum number) { CyrNoun noun = this.GetOrDefault(word, out @case, out number); if (noun != null) { foundWord = word; return(new CyrNoun(noun)); } foundWord = this.GetSimilar(word, this.NounMinSameLetters, this.NounMaxSameLetters); if (string.IsNullOrEmpty(foundWord)) { return(null); } noun = this.GetOrDefault(foundWord, out @case, out number); if (noun != null) { noun = new CyrNoun(noun); noun.SetName(word, @case, number); return(noun); } return(null); }
/// <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 CyrNoun(CyrNoun source) { this.Name = source.Name; this.Gender = source.Gender; this.Animate = source.Animate; this.WordType = source.WordType; this.rules = source.rules; }
protected CyrNoun GetNextPreviousNoun(List <object> Words, int Index) { CyrNoun noun = this.GetNextNoun(Words, Index); if (noun == null) { noun = this.GetPreviousNoun(Words, Index); } return(noun); }
/// <summary> /// Поиск существительного по точному совпадению с указанием рода, падежа и числа. /// Выбрасывает <see cref="CyrWordNotFoundException"/> если слово не было найдено. /// </summary> /// <param name="word">Существительное.</param> /// <param name="gender">Род, в котором указано существительного.</param> /// <param name="case">Падеж, в котором указано существительного.</param> /// <param name="number">Число, в котором указано существительного.</param> /// <returns></returns> public CyrNoun Get(string word, GendersEnum gender, CasesEnum @case, NumbersEnum number) { CyrNoun noun = this.GetOrDefault(word, gender, @case, number); if (noun == null) { throw new CyrWordNotFoundException(word, gender, @case, number, 0); } return(noun); }
/// <summary> /// Поиск существительного по неточному совпадению с автоматическим определением рода, падежа и числа. /// Выбрасывает <see cref="CyrWordNotFoundException"/> если слово не было найдено. /// </summary> /// <param name="word">Существительное в любом роде, числе и падеже.</param> /// <param name="foundWord">Существительное, найденное в словаре.</param> /// <param name="number">Число найденного существительного.</param> /// <param name="case">Падеж найденного существительного.</param> /// <returns></returns> public CyrNoun Get(string word, out string foundWord, out CasesEnum @case, out NumbersEnum number) { CyrNoun noun = this.GetOrDefault(word, out foundWord, out @case, out number); if (noun == null) { throw new CyrWordNotFoundException(word, 0, @case, number, 0); } return(noun); }
/// <summary> /// Поиск существительного по неточному совпадению с указанием рода, падежа и числа. /// Возвращает null если слово не было найдено. /// </summary> /// <param name="word">Существительное.</param> /// <param name="foundWord">Существительное, найденное в словаре.</param> /// <param name="gender">Род, в котором указано существительного.</param> /// <param name="case">Падеж, в котором указано существительного.</param> /// <param name="number">Число, в котором указано существительного.</param> /// <returns></returns> public CyrNoun GetOrDefault(string word, out string foundWord, GendersEnum gender, CasesEnum @case, NumbersEnum number) { CyrNoun noun = this.GetOrDefault(word, gender, @case, number); if (noun != null) { foundWord = word; return(new CyrNoun(noun)); } DictionaryKey key = default; foundWord = null; if (this.EnableCache) { key = new DictionaryKey(word, gender, @case, number); if (this.similarSearchByKeyCache.TryGetValue(key, out foundWord)) { noun = this.GetOrDefault(foundWord, gender, @case, number); noun = new CyrNoun(noun); noun.SetName(word, @case, number); return(noun); } } foreach (string candidate in this.cyrData.GetSimilars(word, this.wordCandidates, this.NounMinSameLetters, this.NounMaxSameLetters)) { noun = this.GetOrDefault(candidate, gender, @case, number); if (noun == null) { continue; } noun = new CyrNoun(noun); noun.SetName(word, @case, number); foundWord = candidate; break; } if (this.EnableCache) { this.similarSearchByKeyCache.Add(key, foundWord); } return(noun); }
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); }
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); }
public Item(CyrNoun Noun) { this.noun = Noun; this.singular = noun.Decline(); this.plural = noun.DeclinePlural(); }