'''
Created on Apr 12, 2011

@author: ola
'''
import random,sys

def printout(st,lsize):
    """
    print the string st using a line-width
    specified by int lsize
    """
    ln = 0
    for ch in st:
        sys.stdout.write(ch)
        ln += 1
        if ln > lsize:
            sys.stdout.flush()
            ln = 0
    sys.stdout.flush()

def generate_text(d,size):
    """
    Generate random text from dictionary d,
    generate size characters and return a string
    of these characters
    """
    
    #TODO assign a random key from d.keys() to seed
    
    seed = ''
    
    text = ""
    for x in range(0,size):
        #TODO create a value and concatenate it to text
       
       
        seed = seed[1:] + next
        
    return text


def make_dictionary(subs):
    """
    Create dictionary for order-3 Markov process
    """
    d = {}
    return d


def make_substrings(text):
    """
    Return all length-3 substrings from text in a list,
    add the first two characters of text to the end to allow
    for wrapping, e.g., "dogfish" should have as the last substring
    "hdo"
    """
    
    textlen = len(text)
   
    # create wrap-around list
    text = text + text[:3]
    
    #TODO create list of 3-letter substrings, the last element
    #of the list is the last 3-letter substring of text
    
    subs = []
    return subs
    
def main():
    filename = "short.txt"
    f = open(filename)
    text = f.read().strip()
    subs = make_substrings(text)
    d = make_dictionary(subs)
    f.close()
    rand_text = generate_text(d,200)

    printout(rand_text,60)

    
if __name__ == "__main__":
    main()