I need some help with Python and Spintax output please

matt1972

Power Member
Joined
Nov 19, 2009
Messages
503
Reaction score
206
I'm wanting to take my folder of articles and randomly import into python and then output a new article. My folder has say 10 articles and in each text file is a heavily, multi spun article. I want to import randomly one article and have a new article saved out to text/html file. How would I go about starting this? Right now my python code I have will create text files off of randomly getting one line of text from text files but it's static lines and not spinning the spintax, that is what I need to accomplish. Where do I begin with doing this? Thanks.
 
You probably solved this, but in case someone needs this. I use following functions for spinning tasks.

Code:
import random
import re

def get_random(arr):
    return arr[random.randrange(0,len(arr))]

def _select(m):
    choices = m.group(1).split('|')
    return choices[random.randint(0, len(choices)-1)]


def spin(text, tokens=None):
    r = re.compile('{([^{}]*)}')
    while True:
        text, n = r.subn(_select, text)
        if n == 0: break
    if tokens:
        text = multi_replace(text, tokens)
    return text.strip()


def multi_spin(text, tokens=None, delimiter= '\n'):
    lines = text.strip().split(delimiter)
    line = array.get_random(lines)
    return spin(line, tokens)


def multi_replace(text, dic):
    pattern = "|".join(map(re.escape,dic.keys()))
    return re.sub(pattern,lambda m: dic[m.group()],text)

To spin spintax use spin(text), if you have tokens in spintax u can use spin(text, {'%URL%': url, '%TITLE%': title}). If you have variations of spyntax in same file/text then use multi_spin, choose delimiter for variations (usually newlines), multi_spin will split text by delimiter and choose randomly one variation and then spin it.

I forgot to post contents of array.get_random() method, here it is:
Code:
def get_random(arr):
    return arr[randrange(0,len(arr))]
It's just simple method to return random element of a list.

Another correction, line = array.get_random(lines) should be replaced with line = get_random(lines). get_random method was already there, just eliminate importing from array module because get_random method is already in script.
 
Excuse me, weedsmoker, I know this thread is old, I run your code but I really can't understand this function: multi_replace(text, dic).

Can you explain better what tokens are for?

Thanks. (Your code rocks!:super:)
 
Hi, token is just some text string you have in your spintax that needs to be replaced with some other text after spining. For example if you have some url but you don't wont to hard code it in spintax because your code is replacing it with urls from some list of urls or some random url, you just put token string in spintax and it will be replaced.
I use %URL% or %TITLE% for example, it just some pattern which is easy for you to spot it in spintax and is easy for python to replace it , but you can use whatever you like, like [], _TITLE_ ... (just don't use cur... link [URL="http://www.bing.com"]www.bing.com
Click this link www.google.com
Check this link www.bing.com
And so on ...
 
Thanks a lot weedsmoker. Playing with spintax code made me think of another useful feature. What if from a single random choice two or more expressions would change synchronously, as in the following snippet:
PHP:
"{{Sometimes@>1|When|If} you are in need of } a solution {and@<1|} you can find one..."
'@>' is a string that indicates that the adjacent previous string has a relationship with the string adjacent to '@<', while '1' is the identifier that makes this relationship unique.
I'll start to work on this. If you want to contribute you're more than welcome!
 
Last edited:
Hi guys.
Thanks for your code @weedsmoker , I have a problem when I execute the code i get the following error:
Code:
print spin(text)
  File "C:\Users\test.py", line 30, in spin
    text, n = r.subn(_select, text)
TypeError: expected string or buffer
Can someone tell me where I am wrong?

thanks
 
Back
Top