Jump to content

Markov Chain Text Generator


Go to solution Solved by .josh,

Recommended Posts

Hi I was given a assignment in college to write a "markov text generator". My code was working the other day when the errors on the server were shut off, they have however put these errors back on and I am receiving the following error:

 

Notice: Undefined offset: 104 in /users/2017// on line 32
d
Notice: Undefined offset: 134 in /users/2017/ on line 32

Notice: Undefined offset: 206 in /users/2017/on line 32
e
Notice: Undefined offset: 304 in /users/2017//markov.php on line 32
e
Notice: Undefined offset: 400 in /users/2017//markov.php on line 32
h

 

My code is as follows:

 

 

 
$textarea = trim ( $_GET['textarea'] );
$textsize = $_GET['textsize'];
$textarea = strtolower ( $textarea );
 
$string =  rand(0, strlen($textarea)-1);
 
$string = $textarea[$string];
 
$textarray = str_split( $textarea );
 
$count = 1;
$newstr = "";
$position = 0;
 
while ( $count < 200 )
{
         foreach ( $textarray as $char )
         {
              if ( $char == $string )
               {
                 $string = $textarray[$position++];
                 $newstr .= $string;
               }
               $position++;
          }
            echo $string = $newstr[rand(0, strlen($newstr)-1)];
            $count++;
}
 
Line 32 is :   $string = $textarray[$position++];
 
Any help would be greatly appreciated :)
Link to comment
https://forums.phpfreaks.com/topic/285555-markov-chain-text-generator/
Share on other sites

ok so updated my code:

 

$textarea = trim ( $_GET['textarea'] );
$textsize = $_GET['textsize'];
$textarea = strtolower ( $textarea );
 
$string =  rand(0, strlen($textarea)-1);
 
$string = $textarea[$string];
 
$textarray = str_split( $textarea );
 
$count = 1;
 
while ( $count < 200 )
{
foreach ( $textarray as $k => $char  )
{
if ( $char == $string && array_key_exists($k++, $textarray) )
{
$tempArr[]=$textarray[$k++];
}
 
}
 
if (count($tempArr) > 0)
{
echo $string=$tempArr[rand(0, count($tempArr)-1)]; 
}
$tempArr=array();
$count++;
}
 
still geting errors this is the output :
 
Notice: Undefined offset: 753 in /users/markov.php on line 30
Edited by Bendoon

foreach ( $textarray as $k => $char  )
{
if ( $char == $string && array_key_exists($k++, $textarray) )
{
$tempArr[]=$textarray[$k++];
}
On the last iteration of the foreach loop, you will get that error, since $textarray[$k++] won't exist. For example, let's say $textarray has 10 elements (0-9). Well on the last iteration of that loop, you are attempting to assign $textarray[10] to $tempArr[] which doesn't exist.

 

I'm not sure what the actual intent of the logic is, so I don't know if this will work for you logic-wise, but for instance to get rid of that error, you would do something like this:

 

if (isset($textarray[$k++])) $tempArr[]=$textarray[$k++];
This will cause the code to NOT assign a new element to $tempArr on last iteration of the foreach loop, though. So like I said, I dunno if that messes with the overall goal, so maybe you need to do something else. Point is that you are attempting to access an element of $textarray that doesn't actually exist.

Hi, thanks for the reply! I tried what you said :

 

foreach ( $textarray as $k => $char  )
{
         if ( $char == $string && array_key_exists($k++, $textarray) )
         {
             if (isset($textarray[$k++]))
             {
                 $tempArr[]=$textarray[$k++];
             }
        }
}
 
But it gives Notice: Undefined variable: tempArr in /users/markov.php on line 37 
 
And
 
Notice: Undefined offset: 5 in /users/markov.php on line 32
 
The overall goal is create a chat bot that will take any input of text and that will chat back with random text taken from the sentences entered at the start :)

okay so overall, these notices are telling you that you are attempting to use an index of an array that doesn't (yet) exist.

 

So for this one:

 

But it gives Notice: Undefined variable: tempArr in /users/markov.php on line 37

So this one looks to be coming from first iteration of your loop, that attempts to use $tempArr before you've initialized it. I see that in the bottom of your script you have $tempArr=array(); which both serves to wipe the current value and initialize it. Well you don't do that for your very first iteration. So add that to somewhere at the top of your script just before your loop. Or else move that line to inside your loop to pop when it's first used, instead of popping it after it's first used. IOW pop it at the beginning of each iteration instead of the end so that it's initialized in the beginning.

oh also, i failed to point something out from first response:

 

foreach ( $textarray as $k => $char  )
{
         if ( $char == $string && array_key_exists($k++, $textarray) )
         {
             if (isset($textarray[$k++]))
             {
                 $tempArr[]=$textarray[$k++];
             }
        }
}
This: $textarray[$k++] - don't do that. Instead use $textarray[$k+1] The difference is that $k++ increments $k which is screwing with your loop, seeing as how that's part of the foreach iterator. $k+1 on the other hand just takes the current value +1 instead of actually assigning a new value to $k.
$textarea = trim ( $_GET['textarea'] );

$ordernumber = $_GET['ordernumber'];

$textarea = strtolower ( $textarea );

 

$string =  rand(0, strlen($textarea)-1);

 

$string = $textarea[$string];

 

$textarray = str_split( $textarea );

 

$count = 1;

$tempArr=array();

 

while ( $count < 200 )

{

foreach ( $textarray as $k => $char  )

{

if ( $char == $string && array_key_exists($k++, $textarray) )

{

if (isset($textarray[$k++]))

{

$tempArr[]=$textarray[$k++];

}

}

}

 

if (count($tempArr) > 0)

{

echo $string=$tempArr[rand(0, count($tempArr)-1)]; 

}

$tempArr=array();

$count++;

}

 

So this is what i have now, but the error still persists that i began with :(. Initializing the variable did indeed get rid of the other error

  • Solution

yayyyy :) thanks a million! you fixed it :). I thought that ++ and +1 were exactly the same :P. Thanks for helping me really appreciate it! Ill be using this site more often :P

Sweet.. Yeah it's only the same when you're actually assigning something. Example

 

$k=$k+1; // <-- this increments value of $k because you are assigning $k+1 back to $k
$k++; // <-- this is shorthand for the above
$k+=1; // <-- also shorthand for the above
++$k; // *sort of the same.
Those are all the same. But $k+1 all by itself doesn't actually assign a new value to $k.

 

*sort of the same - Although $k++ and ++$k both increment $k, there is a subtle difference. $k++ increments after the reference to the $k is used, whereas ++$k increments before the reference to $k is used.

 

Consider this:

 

$x = 5;
$y = 5;
echo 'x: '.$x.'<br/>'; // output: 5
echo 'y: '.$y.'<br/>'; // output: 5
$a = ++$x; // this will first increment $x and then assign the value of $x to $a
$b = $y++; // this will first assign the value of $y to $b and then increment $y
echo 'x: '.$x.'<br/>'; // output: 6
echo 'y: '.$y.'<br/>'; // output: 6
echo 'a: '.$a.'<br/>'; // output: 6
echo 'b: '.$b.'<br/>'; // output: 5
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.