示例#1
0
        public static void Train(MarkovData context, string data)
        {
            Settings settings = Settings.Instance;

            foreach (string r in settings.RowbreakChars)
            {
                data = data.Replace(r, " ");
            }
            data = data.Replace(System.Environment.NewLine, " ");
            string[] words = data.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            Word   cur, prev = null;
            bool   isStarter = true, isEnding;
            string w;

            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i];
                w        = word.ToLowerInvariant();
                isEnding = false;
                foreach (char c in settings.TerminationChars)
                {
                    if (w.EndsWith(c.ToString()))
                    {
                        isEnding = true;
                        break;
                    }
                }

                if (i == words.Length - 1)
                {
                    isEnding = true;
                }

                if (prev != null && !isStarter)
                {
                    prev.Children.Add(new ChildWord(w, isEnding));
                }

                if ((cur = context.Find(Strip(w))) != null)
                {
                    cur.Prevalence++;
                }
                else
                {
                    cur = new Word(context, Strip(w), isStarter, isEnding);
                    context.Words.Add(cur);
                }

                prev = cur;
                if (isEnding)   // determines if next word is starter
                {
                    isStarter = true;
                }
                else
                {
                    isStarter = false;
                }
            }
        }
示例#2
0
 public Word(MarkovData source, string data, bool isStarting, bool isEnding)
 {
     Source     = source;
     Data       = data;
     IsStarting = isStarting;
     IsEnding   = isEnding;
     Children   = new ObservableCollection <ChildWord>();
     Children.CollectionChanged += Children_CollectionChanged;
 }
示例#3
0
 private void InitContext(bool clear = true)
 {
     if (clear)
     {
         LearnTextBox.Text = OutputTextBox.Text = "";
     }
     Context     = new MarkovData();
     DataContext = Context;
     updateContextValues();
 }
示例#4
0
        private void OpenCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBoxResult sresult = AskForSave();

            switch (sresult)
            {
            case MessageBoxResult.Cancel:
                return;

            case MessageBoxResult.Yes:
                SaveCommandBinding_Executed(sender, e);
                break;

            case MessageBoxResult.No:
                break;
            }

            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter           = "Markov Suite Chain|*.chain";
            openDialog.Title            = "Open chain";
            openDialog.InitialDirectory = AppSavePath;

            bool?result = openDialog.ShowDialog();

            if (result == true && openDialog.FileName != "")
            {
                IFormatter formatter = new BinaryFormatter();
                Stream     stream    = new FileStream(openDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); /////// add try/catch clause
                MarkovData data      = (MarkovData)formatter.Deserialize(stream);
                stream.Close();

                Context     = data;
                DataContext = Context;

                Log("Successfully opened file " + Context.FilePath);
            }
        }
示例#5
0
        public static string Generate(MarkovData context)
        {
            if (m_Rnd == null)
            {
                m_Rnd = new Random();
            }

            List <Word> _starters = context.GetStarters();
            List <Word> starters  = new List <Word>();

            foreach (Word w in _starters)
            {
                for (int i = 0; i < w.Prevalence; i++)
                {
                    starters.Add(w);
                }
            }
            Word   prev = starters[m_Rnd.Next(0, starters.Count)];
            string rtn  = prev.Data + " ";

            while (true)
            {
                ChildWord w = prev.Children[m_Rnd.Next(0, prev.Children.Count)];
                rtn += w.Data;
                prev = context.Find(Strip(w.Data));
                if (w.IsEnding)
                {
                    break;
                }
                else
                {
                    rtn += " ";
                }
            }
            return(rtn);
        }