Tag Archives: article rewriting software

Content Spinning Algorithm

Someone asked me recently to develop a content spinner algorithm which can take a document and produce variations of that document. I thought it was an interesting thing to think about, and very rarely do I get to think about algorithms like this in my normal day-to-day programming.

The document contains variation options with a special syntax.  for example:

{Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.

The algorithm will recursively go through the string and generate a new string by choosing an option provided in curly braces separated by pipes.

```import random   def spin(content): """takes a string like   {Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.   and randomly selects from the options in curly braces to produce unique strings. """ start = content.find('{') end = content.find('}')   if start == -1 and end == -1: #none left return content elif start == -1: return content elif end == -1: raise "unbalanced brace" elif end < start: return content elif start < end: rest = spin(content[start+1:]) end = rest.find('}') if end == -1: raise "unbalanced brace" return content[:start] + random.choice(rest[:end].split('|')) + spin(rest[end+1:])   if __name__=='__main__': print spin('{Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.')```