1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I need some help with Python and Spintax output please

Discussion in 'Other Languages' started by matt1972, Sep 24, 2013.

  1. matt1972

    matt1972 Regular Member

    Joined:
    Nov 19, 2009
    Messages:
    499
    Likes Received:
    206
    Occupation:
    To make money
    Location:
    192.168.0.1
    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.
     
  2. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    190
    Likes Received:
    79
    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.
     
    • Thanks Thanks x 6
  3. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    190
    Likes Received:
    79
    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.
     
    • Thanks Thanks x 2
  4. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    190
    Likes Received:
    79
    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.
     
    • Thanks Thanks x 3
  5. xgerman404

    xgerman404 Newbie

    Joined:
    Apr 5, 2015
    Messages:
    5
    Likes Received:
    0
    weedsmoker you're awesome
     
  6. bhpao

    bhpao Junior Member

    Joined:
    Nov 4, 2015
    Messages:
    120
    Likes Received:
    11
    Occupation:
    Advertising Agency
    Location:
    Italy
    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:)
     
  7. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    190
    Likes Received:
    79
    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 Thanks x 1
  8. bhpao

    bhpao Junior Member

    Joined:
    Nov 4, 2015
    Messages:
    120
    Likes Received:
    11
    Occupation:
    Advertising Agency
    Location:
    Italy
    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: Dec 9, 2015
  9. judaculla

    judaculla Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 11, 2014
    Messages:
    279
    Likes Received:
    90
    Location:
    USA
    @weedsmoker I know this thread is old but gdamn man, thanks a ton. This has been helping me out a LOT.