Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- file1 = input("Enter file name >>> ")
- firstWords = []
- dict1 = {}
- flag = 1
- count = 0
- with open(file1, "r") as f1:
- for line in f1:
- tempList = line.split() #Splits the line into a list
- if len(tempList) > 0:
- firstWords.append(tempList[0])
- for i in range(0, len(tempList)):
- if tempList[i] not in dict1:
- dict1[tempList[i]] = []
- if tempList[i] != '.' and tempList[i] != ',' and i < len(tempList) - 1:
- dict1[tempList[i]].append(tempList[i + 1])
- string = random.choice(firstWords)
- while flag == 1 and count < 100:
- count = count + 1
- print(string, end=' ')
- if len(dict1[string]) != 0:
- string = random.choice(dict1[string])
- elif len(dict1[string]) == 0:
- flag = 0
- print('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement