RiverJustice

Hi Guys,

I need your help on this. If you were to create a

markov

  generator in

ASP

 , how would you do it? I've seen numerous

markov

  scripts in

PHP

 , so Im thinking that it must be possible to write one in

ASP

 .

Thanks!  Applause

nutballs

how i would do it is i would get one of the

PHP

  scripts and figure out how it works, which might require

learn

 ing  a bit of

PHP

 . Then i would convert it Applause
Really, it already exists in

PHP

 , so just start down that path of reading it, figuring out how it actually works, and then try recoding it in

ASP

 . Its not that difficult of a function. I think though that alot of the existing

markov

 

PHP

  functions out there have alot more complexity added to them than what you might want as a starting point.

I actually dont do

markov

  to my "other" stuff, so i actually dont know what would be required. I know the concept, but never built it.

of course its possible in

ASP

 . There is nothing that can be done in any language that cant be in another, its just a matter of efficiency.

StephenBauer

From pygmalion over at Syndk8 forums:


class

Markov

 
    {
        string rawText = "";
        int G = 2;
        int O = 1000;

        public bool inputStr(string input)
        {
            this.rawText += input;

            return true;
        }

        public FrequencyTable createFrequencyTable(Array wordArray)
        {
            int tableMaxSize = wordArray.Length + 1 - this.G;

            FrequencyTable table = new FrequencyTable();

            for (int i = 0; i < tableMaxSize; i++)
            {
                string keyStr = "";
                string keyword = "";


                for (int j = i; j < i + this.G; j++)
                {
                    keyStr += wordArray.GetValue(j) + " ";
                }

                if (wordArray.Length > i + this.G)
                    keyword = " " + wordArray.GetValue(i + this.G);
                else
                    keyword = "";

                table.add(keyStr, keyword);
            }

            return table;
        }

        public string generate()
        {
            Array wordArray =

Regex

 .Split( this.rawText, @"s+");
           
            string output = "";
           
            FrequencyTable table = this.createFrequencyTable(wordArray);
            string[] lastWords = new string[this.G];


            for (int i = 0; i < this.G; i++)
            {
                output += wordArray.GetValue(i) + " ";
                lastWords.SetValue(wordArray.GetValue(i), i);
            }

            string debug = "";

            for (int i = 0; i < this.O; i++)
            {
                string keyStr = this.lastWordsString(lastWords);
               
                string nextWord = table.randomWord( keyStr );
                output += " " + nextWord;

                try
                {
                    lastWords = this.lastWordsStack(lastWords, nextWord);
                }
                catch (IndexOutOfRangeException e)
                {
                    //do nothing
                    continue;
                }

                debug += " " + i;
            }

            return

Regex

 .Replace(output, @"s+", " ");
        }

        public string[] lastWordsStack(string[] lastWords, string newWord)
        {
            for (int i = 0; i < this.G-1; i++)
            {
                lastWords.SetValue(lastWords.GetValue(i + 1), i);
            }

            lastWords.SetValue(newWord, this.G-1);

            return lastWords;
        }

        public string lastWordsString(string[] lastWords)
        {
            string output = "";

            for (int i = 0; i < this.G; i++)
                output += lastWords.GetValue(i) + " ";

            output =

Regex

 .Replace(output, @"s+", " ");

            return output;
        }

class FrequencyTable
    {
        Hashtable table = new Hashtable();

        public bool add(string key, string value)
        {
            if (this.table[key] == null)
                this.table.Add(key, "");

            this.table[key] += value + " ";

            return true;
        }

        public string toString()
        {
            //return this.table.ToString();

            return "";
        }

        public string randomWord(string keyStr)
        {
            string tmp = "" + this.table[keyStr];

            string[] arr =

Regex

 .Split(tmp, @"s+");

            Random r = new Random();
           
            // return random entry from Array
            return "" + arr.GetValue(r.Next(arr.Length-1));
        }
    }


As was noted there, you may want to replace strings with StringBuilders.

SB


Perkiset's Place Home   Politics @ Perkiset's