Example #1
0
 public void Append(DialogRecord other)
 {
     if (other.Sender != this.Sender)
     {
         return;
     }
     this.Message += other.Message;
 }
Example #2
0
        /// <summary>
        /// Извлекает все записи диалога из файла
        /// </summary>
        /// <param name="path"></param>
        /// <param name="speaker1"></param>
        /// <param name="speaker2"></param>
        /// <returns></returns>
        public static List<DialogRecord> GetRecordsFromFile(string path, string speaker1, string speaker2)
        {
            var buffer = new List<string>();
            using (StreamReader streamReader = new StreamReader(path))
            {
                while (!streamReader.EndOfStream)
                {
                    buffer.Add(streamReader.ReadLine());
                }
            }

            List<DialogRecord> wholeDialog = new List<DialogRecord>();

            List<string> recbuffer = new List<string>();
            foreach (var s in buffer)
            {
                if (string.IsNullOrEmpty(s) || string.IsNullOrWhiteSpace(s))
                {
                    if (recbuffer.Count > 1)
                    {
                        var record = new DialogRecord(recbuffer);
                        if (wholeDialog.Count > 0 && record.Sender == wholeDialog.Last().Sender)
                        {
                            wholeDialog.Last().Message += " ";
                            wholeDialog.Last().Append(record);
                        }
                        else
                        {
                            wholeDialog.Add(record);
                        }

                        if (wholeDialog.Count > 0 && record.Sender != speaker1 && record.Sender != speaker2)
                        {
                            wholeDialog.Last().Message += " ";
                            wholeDialog.Last().Append(recbuffer);
                            wholeDialog.Remove(record);
                        }
                    }
                    recbuffer = new List<string>();
                }
                else
                {
                    recbuffer.Add(s);
                }
            }

            var record1 = new DialogRecord(recbuffer);
            if (wholeDialog.Count > 0 && record1.Sender == wholeDialog.Last().Sender)
            {
                wholeDialog.Last().Message += " ";
                wholeDialog.Last().Append(recbuffer);
            }
            else
            {
                wholeDialog.Add(record1);
            }

            return wholeDialog;
        }