public override bool isNextFor(NGram previous, int checkWords = 2)
        {
            if (checkWords != 1 && checkWords != 2)
            {
                throw new NotImplementedException();
            }

            if (isStart() || previous.isEnd())
            {
                return(false);
            }

            if (previous is BiGram)
            {
                return(this.Previous == (previous as BiGram).Current && (checkWords == 1 || this.PrePrevious == (previous as BiGram).Previous));
            }
            else if (previous is TriGram)
            {
                return(this.Previous == (previous as TriGram).Current && (checkWords == 1 || this.PrePrevious == (previous as TriGram).Previous));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Получение случайной следующей триграммы за текущей
        /// </summary>
        /// <param name="previous">Текущая триграмма, за которой будет получена следующая</param>
        /// <param name="biIfMoreThanOneTri">Шанс генерации биграммы вместо триграммы, если найдено более 1 триграммы, следующей за текущей</param>
        /// <param name="biIfOnlyOneTri">Шанс генерации биграммы вместо триграммы, если найдена лишь одна подходящая триграмма</param>
        /// <returns></returns>
        private NGram GetNext(NGram previous, double biIfMoreThanOneTri = 0.2, double biIfOnlyOneTri = 0.85)
        {
            var bi  = Ngrams.Where(t => t.isNextFor(previous, 1)).ToArray();
            var tri = bi.Where(t => t.isNextFor(previous, 2)).ToArray();

            var count3 = tri.Length;
            var count2 = bi.Length;

            if (count2 == 0)
            {
                return(null);
            }

            if (count3 == 1)
            {
                if (Rnd.NextDouble() > biIfOnlyOneTri)
                {
                    return(tri[0]);
                }
            }

            if (count3 > 1)
            {
                if (Rnd.NextDouble() > biIfMoreThanOneTri)
                {
                    return(tri[Rnd.Next(tri.Length)]);
                }
            }

            return(bi[Rnd.Next(bi.Length)]);
        }
        /// <summary> Продолжение генерации текста после n-граммы <paramref name="start"/> </summary> <param name="start">Начальная N-грамма</param>
        protected override string ContinueGeneratingText(NGram start)
        {
            var sb      = new StringBuilder();
            var curword = start;

            sb.Append(curword.Current.StartWithUpper());
            while (curword.Current != null)
            {
                curword = Ngrams.Where(w => w.isNextFor(curword, 1)).OrderBy(w => Rnd.Next()).FirstOrDefault();

                if (curword.Current != null)
                {
                    switch (curword.Divider)
                    {
                    case '-':
                        sb.Append(" - " + curword.Current);
                        break;

                    case ',':
                        sb.Append(", " + curword.Current);
                        break;

                    case '.':
                        sb.Append(". " + curword.Current.StartWithUpper());
                        break;

                    case ';':
                        sb.Append("; " + curword.Current);
                        break;

                    case ' ':
                        sb.Append(' ' + curword.Current);
                        break;

                    case '\n':
                        sb.Append(". " + curword.Current.StartWithUpper());
                        break;

                    default:
                        sb.Append(curword.Divider + curword.Current);
                        break;
                    }
                }
            }

            if (curword.Divider != ' ')
            {
                sb.Append(curword.Divider);
            }

            return(sb.ToString());
        }
        public override bool isNextFor(NGram previous, int checkWords = 1)
        {
            if (checkWords != 1)
            {
                throw new NotImplementedException();
            }

            if (previous is BiGram)
            {
                return(this.Previous == (previous as BiGram).Current);
            }
            else if (previous is TriGram)
            {
                return(this.Previous == (previous as TriGram).Current);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #5
0
        /// <summary> Продолжение генерации текста после n-граммы <paramref name="start"/> </summary> <param name="start">Начальная N-грамма</param>
        protected override string ContinueGeneratingText(NGram start)
        {
            var sb      = new StringBuilder();
            var curword = start;

            while (!curword.isEnd())
            {
                //Вставляем слово
                if (curword.isStart())
                {
                    sb.Append(curword.Current.StartWithUpper());
                }
                else
                {
                    sb.Append(curword.Current);
                }

                // Берём следующее слово
                curword = GetNext(curword);

                // Вставляем разделитель
                if (curword.Divider == '-')
                {
                    sb.Append(' ');
                }
                if (curword.Divider != ' ')
                {
                    sb.Append(curword.Divider.ToString() + ' ');
                }
                else if (!curword.isEnd())
                {
                    sb.Append(' ');
                }
            }

            return(sb.ToString());
        }
Beispiel #6
0
 protected abstract string ContinueGeneratingText(NGram curword);
 public abstract bool isNextFor(NGram previous, int checkWords);