Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import random
  2. file1 = input("Enter file name >>> ")
  3. firstWords = []
  4. dict1 = {}
  5. flag = 1
  6. count = 0
  7. with open(file1, "r") as f1:
  8. for line in f1:
  9. tempList = line.split() #Splits the line into a list
  10. if len(tempList) > 0:
  11. firstWords.append(tempList[0])
  12. for i in range(0, len(tempList)):
  13. if tempList[i] not in dict1:
  14. dict1[tempList[i]] = []
  15. if tempList[i] != '.' and tempList[i] != ',' and i < len(tempList) - 1:
  16. dict1[tempList[i]].append(tempList[i + 1])
  17. string = random.choice(firstWords)
  18. while flag == 1 and count < 100:
  19. count = count + 1
  20. print(string, end=' ')
  21. if len(dict1[string]) != 0:
  22. string = random.choice(dict1[string])
  23. elif len(dict1[string]) == 0:
  24. flag = 0
  25. print('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement