Author Topic: (SOLVED) Markov chainer returning only first word and infinite EOL codes  (Read 1432 times)

I tried to write a markov chainer but it's loving up and returning infinite "!eol" and the first word.
Please help me.
Code: [Select]
if(!isObject(MarkovSO)){new ScriptObject(MarkovSO);}
if(!isObject(MarkovFO)){new FileObject(MarkovFO);}

function getChar(%text,%index) // why isn't this implemented in TGE?
{
    return getSubStr(%text,%index,1);
}

function stripNonAlphanumeric(%text)
{
    for(%i=0;%i<=strLen(%text) - 1;%i++)
    {
%letter = getChar(%text,%i);

if(stripos("abcdefghijklmnopqrstuvwxyz1234567890",%letter) != -1)
   %current = %current @ %letter;
    }
    
    return %current;
}

function stripNonAlphanumericKeepSpaces(%text) //why isn't this implemented either?
{
    for(%i=0;%i<=strLen(%text) - 1;%i++)
    {
%letter = getChar(%text,%i);

if(stripos("abcdefghijklmnopqrstuvwxyz1234567890 ",%letter) != -1)
   %current = %current @ %letter;
    }
    
    return %current;
}

function MarkovSO::Read(%this,%line)
{
    %line = strLwr(stripNonAlphanumericKeepSpaces(trim(stripMLControlChars(%line)))); //strip any bull
    echo(%line);
    
    
    //start of chain handling
    if(MarkovSO.word["!sol"] $= "")
   MarkovSO.word["!sol"] = getWord(%line,0);
else
   MarkovSO.word["!sol"] = MarkovSO.word["!sol"] SPC getWord(%line,0);
    
    //mid-chain handling
    for(%i=1;%i<=getWordCount(%line);%i++)
    {
echo(%i);
if(%i == getWordCount(%line)) //end of chain handling
{
   if(MarkovSO.word[getWord(%line,%i)] $= "")
  MarkovSO.word[getWord(%line,%i)] = "!eol";
   else
MarkovSO.word[getWord(%line,%i)] = MarkovSO.word[getWord(%line,%i)] SPC "!eol";
}
else
{
   if(MarkovSO.word[getWord(%line,%i - 1)] $= "")
  MarkovSO.word[getWord(%line,%i - 1)] = getWord(%line,%i);
   else
MarkovSO.word[getWord(%line,%i - 1)] = MarkovSO.word[getWord(%line,%i - 1)] SPC getWord(%line,%i);
}
    }
}

function MarkovSO::Chain(%this)
{
    %SOX = %this.word["!sol"];
    
    echo(%sox);
    
    %currentChain = getWord(%SOX, getRandom(0,getWordCount(%SOX) - 1));
    echo(%currentChain);
    
    %loopCurrently = 1;
    
    for(%i=1;%i<=25;%i++) //this is put in because while loops are extremely hard to use, and to limit markov chaining
    {
if(%loopCurrently)
{
%currentStub = %this.word[lastWord(%currentChain)];
%currentStubGot = getWord(%currentStub, getRandom(0,getWordCount(%currentStub) - 1));


echo(%currentStub);
echo(%currentStubGot);

if(%currentStubGot = "!eol")
   %loopCurrently = 0;
else
   %currentChain = %currentChain SPC %currentStubGot;
}
    }
    
    return %currentChain;
}

function MarkovFO::importIntoMarkov(%file,%path)
{
    %file.openForRead(%path);
    while(!%file.isEoF())
    {
   %line = %file.readLine();
   MarkovSO.Read(%line);

   %linesRead++;
    }
    %file.close();
    %file.delete();
}
« Last Edit: September 03, 2012, 03:09:49 PM by Axolotl2 »

The Wikipedia page for Markov Chains describes something completely unlike what you've posted, so I can't help with that.

What I can help with is some of your comments.

Code: [Select]
function getChar(%text,%index) // why isn't this implemented in TGE?
It's not implemented because of exactly what you're returning, getSubStr(%text,%index,1);.

Code: [Select]
    while(1) //this is put in because slik tote me wiles
    {
%currentStub = %this.word[lastWord(%currentChain)];
%currentStubGot = getWord(%currentStub, getRandom(0,getWordCount(%currentStub) - 1));


echo(%currentStub);
echo(%currentStubGot);

if(%currentStubGot = "!eol")
    break;
else
    %currentChain = %currentChain SPC %currentStubGot;
    }

The Wikipedia page for Markov Chains describes something completely unlike what you've posted, so I can't help with that.

What I can help with is some of your comments.

Code: [Select]
function getChar(%text,%index) // why isn't this implemented in TGE?
It's not implemented because of exactly what you're returning, getSubStr(%text,%index,1);.

Code: [Select]
    while(1) //this is put in because slik tote me wiles
    {
%currentStub = %this.word[lastWord(%currentChain)];
%currentStubGot = getWord(%currentStub, getRandom(0,getWordCount(%currentStub) - 1));


echo(%currentStub);
echo(%currentStubGot);

if(%currentStubGot = "!eol")
    break;
else
    %currentChain = %currentChain SPC %currentStubGot;
    }
I mean a bot like this:

I tell it "roses are red" and "roses are blue"

It knows that roses will preceed are, and are preceeds with either red or blue.

It picks one of those branches so it randomly returns "roses are red" or "roses are blue"

I mean a bot like this:

I tell it "roses are red" and "roses are blue"

It knows that roses will preceed are, and are preceeds with either red or blue.

It picks one of those branches so it randomly returns "roses are red" or "roses are blue"
You will never be able to write an efficient english parser in TQS. Sorry. You can try but it won't really work. Anyway, unless I misread your code or just didn't properly understand what it was doing it looks like you made a lot of mistakes.

You will never be able to write an efficient english parser in TQS. Sorry. You can try but it won't really work. Anyway, unless I misread your code or just didn't properly understand what it was doing it looks like you made a lot of mistakes.
You can write it in torque, what are you talking about?

You will never be able to write an efficient english parser in TQS. Sorry. You can try but it won't really work. Anyway, unless I misread your code or just didn't properly understand what it was doing it looks like you made a lot of mistakes.
Nooooooo

Do you know how Monty works?

I want to do a monty chatbot except on Torque.

You have no idea what a first-order markov chainer is.

You can write it in torque, what are you talking about?
Quote me where I said you can't write it in torque. Or reread my message and see that I never said that.

Nooooooo

Do you know how Monty works?

I want to do a monty chatbot except on Torque.

You have no idea what a first-order markov chainer is.

I don't know what Monty is, so no I don't know how it works.
« Last Edit: September 03, 2012, 03:05:39 PM by TripNick »

I'm going to attempt to fix this mess myself, this is the same situation as that hermite interpolator I asked you guys to help me with.

lol holy forget I found the error

%word = "!eol" should be %word $= "!eol"

the script is fixed, but since you guys always pick on me to not lock topics, I am keeping this unlocked.
« Last Edit: September 03, 2012, 03:09:34 PM by Axolotl2 »

MarkovSO.word[getWord(%line,%i - 1)] = MarkovSO.word[getWord(%line,%i - 1)] SPC getWord(%line,%i);
Just a warning, but using this method will result is very large string; once these strings get really long, execing the file will cause a buffer overflow

Just a warning, but using this method will result is very large string; once these strings get really long, execing the file will cause a buffer overflow
I'm going to use strStrW to check for duplicates, and do a for loop that maxes out at 25.

The second part is....decent, you're just limiting a lot of vocabulary, The first part, you lose the idea of a word that is used more often will be chosen more often, which is a big aspect of markov chains.

The second part is....decent, you're just limiting a lot of vocabulary, The first part, you lose the idea of a word that is used more often will be chosen more often, which is a big aspect of markov chains.
well I won't strStrW it then, but I am keeping the 25 limit but setting it at 40.

WAIT

I could make 2 occurences detect as bla.word["stuff"] = "word"; bla.occurences["word"] = 2;
nvm too complicated
« Last Edit: September 03, 2012, 03:37:49 PM by Axolotl2 »