Programming Languages

What is Spinning and a Spin Function (PHP)

dreamstimesmall_2869597

Spinning Basics 101

Spinning generally refers to randomly generating articles, content and data based on option sets. Typically a template or as I call it a “boil plate” is created identify the set. I will address the use of spinning for generating articles and other sentence content but as you will easily derive the content does not need to be limited to just words and sentences, you can spin html, CSS, PHP code, data and more.
Say we have a sentence such as “I love my car.” How else could we say this?

I love my automobile.

I like my car.

I really like my vehicle.

These sentences roughly all portray the same meaning but each is distinctly different. When it comes to unique article generation this technique can be invaluable. All we have to do get all these variations and other combinations is generate a boiler plate for the sentence like so.

I {really |}{love|like} my {car|automobile|vehicle}.

So as you can see above all versions of the sets of words are contained in the curly brackets each version separated by the pipe character (the straight vertical line.) Pipe us used because it one has been a standard data delimiter and two because it also a shortcut for OR in programming logic. There is no so called standardization for spinning but it is widely used.

In our example we can see that when spun the set {love|like} means there is equally likely chance that either word will be chosen and used in this spot. For {really|} we see there is only one word, but there is still a pipe followed by a bracket, this indicates there is a chance that really or “nothing” will be selected. Also the last set contains car, automobile and vehicle all with an equal chance to be chosen.

But say we wanted the word ”love” to have a greater chance then like to be chosen? If each of the two words has a 50/50 chance we can increase the odds for the word like by going {like|love|love} now “love” has 2 in 3 chances or 66% probability of being chosen. Same thing can be applied if we want to reduce the odds of the word “really” we can go {really|||} now we have 3 blank spaces to the one choice for “really” giving the word only a 25% chance of being used.

Nested Spinning

With recursive functions such as the one at the end of this article, you can create boiler plates with nested spins, that is spins within spins.
For example:

Spinning is a {{great|wonderful|good} tool|hot {topic|subject}} for SEO.
When nesting it can start to become confusing to decipher the text but if you break it down like so it is easier to see.

{
	{great|wonderful|good} tool
	|
	hot {topic|subject}}
}

If the first element of the top spin is chosen followed by the second element of the nested spin we get “Spinning is a wonderful tool for SEO.”
Spinning has no restrictions on the levels of nesting you want to add.

Spinning Function for PHP

This has been a very introductory article on the concepts of spinning, most spinning also incorporates deeper elements such as dynamic variables, synonym spinning, adjective and grammar injection and more.

If you are ready to get spinning using some simple PHP code here you go.

function spin($s)
	{
	preg_match('#\{(.+?)\}#is',$s,$m);
	if(empty($m)) return $s;
	$t = $m[1];
	if(strpos($t,'{')!==false)
		{
		$t = substr($t, strrpos($t,'{') + 1);
		}
	$parts = explode("|", $t);
	shuffle($parts);
	$s = preg_replace("+\{".preg_quote($t)."\}+is", $parts[array_rand($parts)], $s, 1);
	return spin($s);
	}

Of course you can create similar functions in almost any language including C++,C#, Perl, Python and so on.

Kicking the Hash Bucket (PHP)

 

If you have ever studied or taken a class that discusses searching and sorting algorithms and their efficiencies you will be familiar with sequential search, binary search and others. However was bucket search ever mentioned? As a PHP programmer you might be using bucket search not even realizing it. Bucket search is absolutely the fastest most efficient and less complex method available. When using a language PHP that supports hash variables it gets even better and more beneficial.

A normal array uses only integers for the keys.

A[0],A[1],A[2]…

A hash array allows you to use strings as the keys.

A[apple],A[orange],A[banana]…

In traditional methods when you want to search an array for an element to be retrieved you must iterate through the array until you find key and retrieve the value.  However bucket searches allow you to instantly retrieve the information based on the key value with no iteration, as the key is a pointer to the location in memory where the value is stored. That is the power of bucket search and I welcome any comments of another method that proves otherwise.

Here is some code with bucket search in action.

$fruit_color = array('apple' => 'red',
				'orange' => 'orange',
				'banana' => 'yellow');

echo 'An apple is '.$fruit_color[apple].'.';

 

There are however limitations and problems that can arise depending on the type of strings you utilize as the keys in your array, such as the use of special characters, binary characters, utf-8 characters, string is too long, white space, unknown upper and lower case conditions, and others.

Using base64_encode and a few other methods to normalize your key almost all of these issues can be resolved.

$quotes[myKey("This is Sparta!!!\n")] = 'The Movie 300';

function myKey($k)
	{
	return base64_encode(strtolower(preg_replace("/[^A-Za-z0-9]/", '', $k)));
	}