The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. January 08, 2009, 12:11:28 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: [Help needed] ASP and Markov  (Read 603 times)
RiverJustice
n00b
*
Offline Offline

Posts: 1


View Profile
« on: June 27, 2007, 01:52:34 AM »

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!  Mobster
Logged
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3525


View Profile
« Reply #1 on: June 27, 2007, 09:33:44 AM »

how i would do it is i would get one of the PHP scripts and figure out how it works, which might require learning a bit of PHP. Then i would convert it Smiley
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 markovPHP 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.

Logged
StephenBauer
Rookie
**
Offline Offline

Posts: 36


View Profile
« Reply #2 on: June 27, 2007, 10:57:44 AM »

From pygmalion over at Syndk8 forums:

Code:
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
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!