Skip to content

Instantly share code, notes, and snippets.

@mariack
Created March 31, 2015 20:19
Show Gist options
  • Save mariack/f64b5bbf3c94acbfe1b3 to your computer and use it in GitHub Desktop.
Save mariack/f64b5bbf3c94acbfe1b3 to your computer and use it in GitHub Desktop.
## LAB7.py
## Name: Maria Kalusz ID: tuc31890
## Date: 3/31/2015
## Lab 7 CS1051
## Reads files of words and output sentences that randomly combine the words
##****************************************************************************
import os
from random import randint
import datetime as dt
def read(fname, ftype):
fh = open(fname)
if ftype == 0:
a = fh.readlines()
return a
elif ftype == 1:
a = []
b = fh.readlines()
for e in b:
e2 = e.split()
a.extend(e2)
return a
elif ftype == 2:
a = []
b = fh.readlines()
for e in b:
e2 = e.split(',')
a.extend(e2)
return a
fh.close()
def pick(x):
j = randint(0,len(x)-1)
w = x[j].strip()
return w
def main():
fpath = r'C:\Users\tuc31890\Downloads'
fn1 = 'nouns.txt'
fn2 = 'verbs.txt'
fn3 = 'adjective.txt'
fn4 = 'adverbs.txt'
fn5 = 'out1.txt'
fn6 = 'out2.txt'
os.chdir(fpath)
nouns = read(fn1, 0)
verbs = read(fn2, 1)
adjectives = read(fn3, 2)
adverbs = read(fn4, 2)
sentences = []
fh5 = open(fn5, 'w')
for j in range(0,5):
anoun = pick(nouns)
averb = pick(verbs)
anadjective = pick(adjectives)
anadverb = pick(adverbs)
s = 'The %s %s %s %s. ' % (anadjective, anoun, averb, anadverb)
print(s)
fh5.write(s)
sentences.append(s)
print(' '.join(sentences))
fh5.close()
fh6 = open(fn6, 'w')
fh6.write(' \n'.join(sentences))
fh6.close()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment