#------------------------------------------------------------------------------ # # RandomInput5.py # Creates a random input file for cse 101 pa5. This is identical to the input # file generator from pa1. # # An input file is created by selecting random words from file English.txt, # and random characters from the delimiter string # # " \t\\\"\',<.>/?;:[{]}|`~!@#$%^&*()-_=+0123456789" # # and creating random lines from these objects. Lines may be blank at random. # Words will be capitalized at random. The probabilities for these choices # and other parameters can be changed below. # # To run this program on Linux do: # # python3 RandomInput1.py # #------------------------------------------------------------------------------ from random import uniform, randint, choice, choices blankLineProbability = 0.20 delimProbability = 0.30 capitalizeProbability = 0.25 minLineLength = 20 maxLineLength = 1000 numLines = 200 wordfile = open('English.txt','r') # file English.txt must exist # get file name and open fileName = input('Enter name of input file to be created: ') file = open(fileName,'w') # define delimiter chars, leave out newline D = list(" \t\\\"\',<.>/?;:[{]}|`~!@#$%^&*()-_=+0123456789") # read lines of wordfile, create a list of words with repetition F = wordfile.readlines() wordfile.close() W = [word.strip() for word in F] W = choices(W, k=len(W)//50) # a subset W = choices(W, k=len(W)*20) # a subset, with repetitions # create random input file for i in range(int(numLines)): # chose blank or number of chars in this line if uniform(0,1) < blankLineProbability: file.write('\n') continue else: # non-blank line numchars = randint(minLineLength, maxLineLength) # create random line line = [choice(W)] # pick a random word count = len(line[0]) # number of chars in this line while count < numchars: if uniform(0,1) < delimProbability: line.append(choice(D)) count += 1 else: word = choice(W) if uniform(0,1) < capitalizeProbability: char_list = list(word) char_list[0] = char_list[0].upper() word = ''.join(char_list) item = ' '+word line.append(item) count += len(item) if count > numchars: item = line.pop() count -= len(item) # write line to file file.write(''.join(line)+'\n') # close file file.close()